Writing the first program using a class and object

Lesson Details:
June 29, 2020

I: Introduction

A: Python, a general-purpose object oriented programming (OOP) language that is also considered to be a high-level programming language, is used to develop applications and computer programs. It is one of the most popular programming languages for this purpose.

B: Python is often used in web development for websites and web apps, and it is also used for tasks such as parallel processing and systems development. Not only is it easy to learn, but it is also easy to use, especially for beginners.

C: The language was developed by Guido van Rossum in 1991 and has evolved since then. It is maintained by Python Software Foundation and is used by big companies such as Google and NASA.

II: Body

A: Writing the first program using a class and object

B: First, we will start with the simplest concept of an Object Oriented Programming Language - Class and objects. Class in python is a blueprint from which we can create multiple objects. These objects are instances of classes. We can add methods and attributes to our class and create many objects from it.

C: Syntax: ClassName(object_list=None, *extra): Constructor(object_list=None)

D: Where ‘object_list’ is an optional argument. If not mentioned, it will default to None. A class object can have properties or attributes of its own which can be accessed through the class name directly. For example, say you created a class named ‘Fruit’. You can access all the attributes of this class through ‘Fruit’ i.e., ‘Fruit.color’, ‘Fruit.weight’ etc. This makes it easier to manage data. Say you want to change the color of some fruit, if it had been stored as a property instead of accessing it directly, you would have had to modify each instance of fruit individually. But now you can change it once at the class level and apply the changes to all the instances of fruit. This is called inheritance. Since python is an OOP language, it allows us to use multiple inheritance. We can create classes which inherit from other classes.

E: Inheritance example: class Fruit(): color = "red" weight = 100 # Creating an object f = Fruit() print("f.weight = ", f.weight) print("f.color = ", f.color) f.weight = 120 print("f.weight = ", f.weight) f.color = "gray" print("f.color = ", f.color)

F: Single inheritance example: class Fruit(): color = "red" weight = 100 # Creating an object f = Fruit() # Creating another object g = Fruit() g.color = "blue" print("f.color = ", f.color) print("g.color = ", g.color)

G: Multiple inheritance example: class Fruit(): color = "red" weight = 100 @classmethod def sayhi(self): print("Hey!") @classmethod def sayhello(self): print("Hello!") # Creating an object h = Fruit() h.sayhi() h.sayhello() print("h_sayhi = ", h._sayhi) print("h_sayhello = ", h._sayhello) print(h._sayhi) print(h._sayhello) # When two same methods exists in two classes, they will override # each other, lets check with above code pprint(h._sayhi) pprint(h._sayhello) # Accessing inherited method from super class from Fruit superclass = Fruit() superclass._sayhi() superclass._sayhello() # Accessing inherited method from sub class from Fruit subclass = Fruit() subclass._sayhi() subclass._sayhello() print(subclass._sayhi) print(subclass._sayhello)

H: Data type conversion Example: if x < 0 xss=removed xss=removed>= 0: x += 1 else: x -= 1 if x >= 0: print('x value is positive') else: print('x value is negative') if x == 0: print('x value is zero') else: print('x value is not zero') if x % 2 == 0: print('x value is even') else: print('x value is odd') if x > 0 and x < 10> 5 and x < 10>= 5 and x < 10 xss=removed> -1: print('x value is even and greater than -1') else: print('x value is not between -1 and 0')

Course content