Lecture: Learning Python 8-Classes and Objects ver. 8

Last Updated on July 9, 2022 by shibatau

The link to Python 6-If Statements is added.

I. What will you learn?

These posts are Python tutorials for beginners and used for my lectures.

Learning Python 0-Cheat Sheets

Learning Python 1-Google Colaboratory

Learning Python 2-Sample data

Learning Python 3-Lists and Comprehensions

Learning Python 4-Dictionaries and Data frames

Learning Python 5-Functions

Learning Python 6-If Statements

Learning Python 7-For Loops

Learning Python 8-Classes and Objects

II. Classes and objects

  • Class: The class is a user-defined data structure that binds the data and methods into a single unit. You can create objects with Class.
  • Object: An object is an instance of a class. It is a collection of attributes and methods.

III. Create classes and objects

# create a class
class Student:
  def __init__(self, id, english, math):
        self.id = id
        self.english = english
        self.math = math
  def __repr__(self):
    return str((self.id, self.english, self.math))
  def average(self):
    # calculate the mean of english and math
    return (self.english + self.math)/2
# create a list of objects
students = [Student(2, 50, 60),
            Student(1, 60, 70),
            Student(3, 40, 50)]
# sort objects by id numbers
print(sorted(students, key=lambda x: x.id))

III. Create a class with Dataclasses

Dataclasses significantly decrease the amount of boilerplate codes required to write.

# create a class
class Student:
  def __init__(self, id, english, math):
        self.id = id
        self.english = english
        self.math = math
  def __repr__(self):
    return str((self.id, self.english, self.math))
  def average(self):
    # calculate the mean of english and math
    return (self.english + self.math)/2
# create a list of objects
students = [Student(2, 50, 60),
            Student(1, 60, 70),
            Student(3, 40, 50)]
# sort objects by id numbers
print(sorted(students, key=lambda x: x.id))

You can see all the scripts here:

https://colab.research.google.com/drive/1E4gyjQ8Wb683KQf4IEU_qQSGApU6sEI0?usp=sharing

About shibatau

I was born and grown up in Kyoto. I studied western philosophy at the University and specialized in analytic philosophy, especially Ludwig Wittgenstein at the postgraduate school. I'm interested in new technology, especially machine learning and have been learning R language for two years and began to learn Python last summer. Listening toParamore, Sia, Amazarashi and MIyuki Nakajima. Favorite movies I've recently seen: "FREEHELD". Favorite actors and actresses: Anthony Hopkins, Denzel Washington, Ellen Page, Meryl Streep, Mia Wasikowska and Robert DeNiro. Favorite books: Fyodor Mikhailovich Dostoyevsky, "The Karamazov Brothers", Shinran, "Lamentations of Divergences". Favorite phrase: Salvation by Faith. Twitter: @shibatau

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.