Object Oriented Code in Python

Object Oriented Code in Python

This is time to go through the code to demonstrate the concept of OOP in Python. This is well known fact that OOP can not be done without Class and Object. So here is the example of Class and Object in Python.

Code of OOP in Python

Output of above code is –

Output of two Students Object

Now lets start understanding the code line by line. First line to Fourth line nothing but class level comment. Make it as clear and understandable as you can. Line no 8 is the beaning of class structure [class student(object): ]. It says that we are going start a class named Student and (object) is saying that this is new style of writing class and at last : is for starting the block statement. This is similar to { } sign in Java or C++

Next is [def __init__(self, name, marks):]. Here init is a function to initialise the object of class. If you are adding it well and good, if you left it don’t worry Python will insert it with default argument self on behalf of you. But here I used two extra parameters (name and marks), that means when ever user will try to create the object of Student class he/she have to pass these two parameters. After that self.name and self.marks are two instance variables which are getting initialised by parameters of init method. Remember the style of writing init method, it starts with two underscore sing and ends with the same two underscore sign.

Now you can write N numbers of methods a per your business requirement. Remember that if the function takes self as parameter are instance method and will get called by the object directly. [def getStudent(self):] this is the only function we are heving right now.

Lastly we need to create object and call method associated with that. [s1 = student(‘Robin’, 98)] will create one object s1 of student class. As my init method takes two parameters except self. So need to pass. and lastly call getStudent() on s1. You will get output as above. I created two Objects, you can create as much as you want.

That’s it. You are done with basic example of Class and Object in Python. Comments and feedback are always welcome. Bye bye…

Saving Python object to File (serialization)

Saving Python object to File (Serialization)

Serialization and de-serialization is the process to store python object structure into file and viceversa. All because of using pickle module this process is known as pickling (serialization) and un-pickling(de-serialization). Pickling can get achieved by dump() method and un-pickling can be achieved by load() method of pickle module. But using this module in your program first you need to import pickle in your file. Here is the example of pickling(serialization).

Dump Student Object to file

This code will create a class called Student and at line no 29 Object of Student is created stu1. After that data is being loaded using readMarks() method at line no 31. One data is loaded, created file object f1 of ‘student.log’ file at line no 34. Later on used pickle.dump() method to dump the data into file and at last closed() the file object.

GCD and LCM in Python

GCD and LCM in Python

The logic to find the GCD and LCM is quite simple. First write a function to get GCD and then write function to get LCM of the number. GCD can get calculated by the logic to find reminder after dividing one number by another, next time dividing the divisor by reminder left out. Continue the process till divisor is greater then zero. Now to get LCM we need to apply logic to divide the multiplication of numbers by the GCD value of numbers. Like if a and b are two numbers then the LCM will (aXb)/gcd(a,b). Lets look into the program.

GCD and LCM calculation in Python.

Output will be like this –

Enter first number – 4
Enter second number – 12
GCD of 4 and 12 is = 4
LCM of 4 and 12 is = 12.0

Finding largest in list elements in Python.

Largest in List of Python

This program is to find the largest among the list elements using for loop. Logic here is to store first element in variable called large and iterate the rest of the elements using for loop from 1 to length of list. Now check if the element found is greater then the variable large then assign the current element value in large and continue the same.

Program to find largest among the list elements.

Output of this will look like this. –

List elements are – [4, 5, 3, 2, 5, 7, 9, 1]
Largest element in list is – 9