Learn Python 3_ Classes Cheatsheet _ Codecademy
Learn Python 3_ Classes Cheatsheet _ Codecademy
Classes
def __repr__(self):
return self.name
john = Employee('John')
print(john) # John
In Python, methods are functions that are defined as part # Dog class
of a class. It is common practice that the first argument of
class Dog:
any method that is part of a class is the actual object
calling the method. This argument is usually called self. # Method of the class
def bark(self):
print("Ham-Ham")
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-classes/cheatsheet 1/5
12/3/24, 3:19 PM Learn Python 3: Classes Cheatsheet | Codecademy
# Class Instantiation
ferrari = Car()
x = my_class()
y = my_class()
print(x.class_variable) #I am a Class
Variable!
print(y.class_variable) #I am a Class
Variable!
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-classes/cheatsheet 2/5
12/3/24, 3:19 PM Learn Python 3: Classes Cheatsheet | Codecademy
dog = Animal('Woof')
print(dog.voice) # Output: Woof
a = 1.1
print(type(a)) # <class 'float'>
a = 'b'
print(type(a)) # <class 'str'>
a = None
print(type(a)) # <class 'NoneType'>
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-classes/cheatsheet 3/5
12/3/24, 3:19 PM Learn Python 3: Classes Cheatsheet | Codecademy
Python class
In Python, a class is a template for a data type. A class can # Defining a class
be defined using the class keyword.
class Animal:
def __init__(self, name,
number_of_legs):
self.name = name
self.number_of_legs = number_of_legs
def print_name(self):
print("Hi, I'm " + self.name)
print(dir())
# ['Employee', '__builtins__', '__doc__',
'__file__', '__name__', '__package__',
'new_employee']
print(dir(Employee))
# ['__doc__', '__init__', '__module__',
'print_name']
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-classes/cheatsheet 4/5
12/3/24, 3:19 PM Learn Python 3: Classes Cheatsheet | Codecademy
__main__ in Python
Print Share
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-classes/cheatsheet 5/5