This document provides an agenda and content for an Advance Python Day 3 training. It covers classes, inheritance, polymorphism and includes activities. Key points include: defining classes with attributes like methods and variables; the __init__() function; self parameter; class vs instance variables; creating object instances; inheritance; and polymorphism through different object forms accessing the same method. Activities guide creating classes for animals and workers/CEOs to demonstrate these concepts.
3. QUICK
RECAP
● Python Fundamentals
● Operations
● Data Types
● Lists
● Built-in Function and Methods
● File Built-in Functions
● Loop constructs
● Functions
4. ACTIVITY - OLD SCHOOLYARD PICK
Write a program that will ask the user for
5 names. Using string manipulations, trim
each name which separates the first
name with the last name and store the
trimmed strings to two lists fname and
lname respectively.
5. Class
A user-defined prototype for an object that defines a
set of attributes that characterize any object of the
class. The attributes are data members (class
variables and instance variables) and methods,
accessed via dot notation.
11. self parameter
self parameter is a reference to the class
itself, and is used to access variables that
belongs to the class.
12. self parameter(cont.)
self parameter is not limited to ‘self’ keyword.
Any keyword* would work guided that it has to
be the first parameter of the __init__ method.
14. Instead of using
keyword ‘self’, modify
our Person class by
replacing self by other
keywords and test if
code will still work.
Test it out
15. Class variables vs Instance variables
Class variable is a variable that is shared by all
instances of a class. Class variables are defined within
a class but outside any of the class's methods while
Instance variable is a variable that is defined inside a
method and belongs only to the current instance of a
class.
16. Creating Instance Objects
To create instances of a class, you call the class using
class name and pass in whatever arguments its __init__
method accepts.
18. ACTIVITY - BREAK A LEG
Create a class named Animal having a animal name, animal
color and legs_count. In the same class, create a method
called identify_animal in which you will determine if an
animals is either 2-legged, 4 -legged or limbless or no legs.
If legs_count = 8, it should print out “arthropods”
If legs_count = 4, it should print out “quadruped”
If legs_count = 2, it should print out “biped”
If legs_count = 0, it should print out “apoda”
For other values for legs_count = it should print out “Odd creature”
22. Activity - Super Idol
Create a parent class called Worker which has instance
variables such as name, email_address and class
variable basic_pay.
Create a subclass CEO which will inherit the name,
email_address and class variable basic_pay. Do a
validation check in your child method that if the inherited
value of basic pay is less than 15,000, you should add
5,000 to the basic pay.