0% found this document useful (0 votes)
6 views20 pages

03 Three

Python

Uploaded by

Shahin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views20 pages

03 Three

Python

Uploaded by

Shahin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Defining

our
own
Classes
Think of a Class as a Blueprint
A Class:
Is a Blueprint/Template for creating objects

Class Object

Contains a general The final Object


overview this type of created from the Class
object using the description
An Object (instance) of a Class:

instance = Class()
Object A class is always
Capitalized and followed
by parentheses
An Object is a Variable
equal to a Class Remember -> t1 = Turtle()

Object Class
Examples of Objects:
They are All Animals sharing some of the same properties. They
all have additional properties and actions of their own

Animals

-Owl -Raccoon -Lion -Giraffe


-Can Fly -Four Legs -Four Legs -Four Legs
-Feathers -Tail -Tail -Tail
-North America -Africa -Africa
Examples of Objects:
They are all Phones sharing some of the same properties. They all have additional
properties and actions of their own

Telephone

-Phone -Phone -Phone -Phone


-Black -Black -Black -Black
-Wireless -Mobile -Mobile
-Texting -Smartphone
Creating a Class:
Before creating any Objects, we must first build our class.
A Class is where we will create any Properties (variables)
and Methods (functions) our objects will need

An Object
Animals A Class (Family)

-Owl -Raccoon -Lion -Giraffe


-Can Fly -Four Legs -Four Legs -Four Legs
-Feathers -Tail -Tail -Tail
-North America -Africa -Africa
The Constructor Function:
It's a method (function) that is automatically ran when an
object is created. It creates an instance of a specific class

Constructor

Construction

Used to Build
The Constructor Function:
It's a method (function) that is automatically ran when an
object is created. It creates an instance of a specific class

Constructor
__init__()

This is the name we Construction


must use for our
constructor function
__init__ -> Initialize -> Start Used to Build
Creating our Classes: Creating a basic class named, My_class.
class My_class():
Here we create our Constructor method
def __init__(self, parameter1, parameter2):
(__init__). This is where we define the
Properties our class will need
self.property1 = parameter1
self.property2 = parameter2
def drive(self): Here is a Method named, drive. This
method uses the values from our two
x = self.property1 + self.property2 properties and gets the sum.

print("The speed is:" , x)

car = My_class(50 , 60) Here we create an object named, car. The


value of an object is a Class()
car.drive() Your Arguments must match your parameters

self -> Is basically a "key". It unlocks the Calling/Using the drive method we built in the class

class so we can use the properties and


methods throughout
Creating our Classes: Creating a basic class named, My_class.
class My_class():
Here we create our Constructor method
def __init__(self, parameter1, parameter2):
(__init__). This is where we define the
Properties our class will need
self.property1 = parameter1
self.property2 = parameter2
def drive(self): Here is a Method named, drive. This
method uses the values from our two
x = self.property1 + self.property2 properties and gets the sum.

print("The speed is:" , x)

car = My_class(50 , 60) Here we create an object named, car. The


value of an object is a Class()
car.drive() Your Arguments must match your parameters

Calling/Using the drive method we built in the class


Creating our Classes:
class My_class():
def __init__(self, parameter1, parameter2):
self.property1 = parameter1
self.property2 = parameter2
def drive(self):
x = self.property1 + self.property2
print("The speed is:" , str(x), "km/h") Output in Terminal

car = My_class(50 , 60)


The speed is: 110 km/h

car.drive()
Looking at another Class:
class App():
def __init__(self, users, storage, username):
self.users = users
self.storage = storage product_one = App(35, 256, "owner")
product_one.login()
self.username = username
product_one.increase_capacity(50)
def login(self):
if self.username == "owner" and self.users >= 1: product_two = App(35, 128, "josh")
print("Welcome,", self.username) product_two.login()

print("Your storage is:", self.storage)


def increase_capacity(self, number):
What do we think will have with
self.storage += number
product_one and product_two
return self.storage above?

What is the name of my class and object(s)


Looking at another Class:
class App(): Class
def __init__(self, users, storage, username): Objects
self.users = users
self.storage = storage product_one = App(35, 256, "owner")
product_one.login()
self.username = username
product_one.increase_capacity(50)
def login(self):
if self.username == "owner" and self.users >= 1: product_two = App(35, 128, "josh")
print("Welcome,", self.username) product_two.login()

print("Your storage is:", self.storage)


def increase_capacity(self, number):
What do we think will have with
self.storage += number
product_one and product_two
return self.storage above?

What are the value of my properties?


Looking at another Class:
class App():
def __init__(self, users, storage, username):
self.users = users
self.storage = storage product_one = App(35, 256, "owner")
product_one.login()
self.username = username
product_one.increase_capacity(50)
def login(self):
if self.username == "owner" and self.users >= 1: product_two = App(35, 128, "josh")
print("Welcome,", self.username) product_two.login()

print("Your storage is:", self.storage)


def increase_capacity(self, number):
What do we think will have with
self.storage += number
product_one and product_two
return self.storage above?

Where are the Arguments that'll be given to my Class?


Looking at another Class:
class App():
These are our arguments
def __init__(self, users, storage, username): (users, storage and
username)
self.users = users
self.storage = storage product_one = App( 35, 256, "owner" )
product_one.login()
self.username = username
product_one.increase_capacity(50)
def login(self):
if self.username == "owner" and self.users >= 1: product_two = App( 35, 128, "josh" )
print("Welcome,", self.username) product_two.login()

print("Your storage is:", self.storage)


def increase_capacity(self, number):
What do we think will have with
self.storage += number
product_one and product_two
return self.storage above?

Where are the Arguments that'll be given to my Class?


ClassBreakdown:
class App(): Creating a Class called App
def __init__(self, users, storage, username):
self.users = users
self.storage = storage
A condition within the login function which
checks the properties against a set value
self.username = username
def login(self):
if self.username == "owner" and self.users >= 1:
print("Welcome,", self.username)
A method which increases
print("Your storage is:", self.storage)
the current value of
def increase_capacity(self, number): self.storage
self.storage += number
return self.storage
product_one = App(35, 256, "owner") Creating an object called
product_one.login()
product_one and calling both
product_one.increase_capacity(50)
methods
product_two = App(35, 128, "josh")
product_two.login()
Class Breakdown:
class App(): Creating a Class called App
def __init__(self, users, storage, username):
self.users = users Setting up the constructor
self.storage = storage method with the
self.username = username properties we'll use
def login(self):
if self.username == "owner" and self.users >= 1: A condition within the login function which
checks the properties against a set value
print("Welcome,", self.username)
print("Your storage is:", self.storage)
A method which increases
def increase_capacity(self, number):
the current value of
self.storage += number
self.storage
return self.storage
Creating an object called
product_one = App(35, 256, "owner") product_one and calling both
product_one.login() methods
product_one.increase_capacity(50)
Creating an object called product_two
product_two = App(35, 128, "josh")
and calling the login method
product_two.login()
Class Breakdown:
class App():
def __init__(self, users, storage, username):
self.users = users
self.storage = storage
self.username = username
def login(self):
if self.username == "owner" and self.users >= 1: product_one Terminal Output
print("Welcome,", self.username)
print("Your storage is:", self.storage) Welcome, owner
else: Your storage is: 256
print("Login is denied!") Updated storage: 306
def increase_capacity(self, number):
self.storage += number
print("Updated storage:", self.storage)

product_one = App(35, 256, "owner")


product_two Terminal Output
product_one.login()
product_one.increase_capacity(50)
Login is denied!
product_two = App(35, 128, "josh")
product_two.login()
Flashback pre-object:
A class is a big group of methods that all contain various elements of python,
such as, Conditions, Loops, Lists, Dictionaries, Etc...

Conditional Statements While Loops For Loops

for every item inside something


if something is true, do this. Other While something is true, keep
do something with the current
wise do something else. repeating otherwise stop
iteration item

if name != "done" or name != "quit": count = int(input("Enter count: ")) ages = [33, 25, 62, 15, 19, 33]
print("Hello,", name)
elif name == "josh": while count != 0: for age in ages:
print("It's me Josh") if count >= 10: if age <= 17:
else: count -= 2 ages.remove(age)
print("Removed, under 18")
print("Goodbye") else:
else:
count -= 1
print("Person Age:", age)
print("Current count:", count) ages.sort()
count = int(input("Enter count: print("Ages list:", ages)
"))
Challenge #1 in VS code:
DO NOT GO BACK!

Try to recreate the code from previous slide!

Use it as an example to build your own

One Base Class - (App)

Three methods(init, login, increase_capacity)

login -> check if username == "owner"

increase_capacity -> add a number to current storage

Create two objects, both objects call both methods

You might also like