Python Introduction
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built-in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development.
-from python.org
Guido Van Rossum developed the python programming language as a successor of the ABC programming language in 1991. It is named after the “Monty python's Flying Circus” comedy series. It is widely used in reputed companies like Google, Dropbox, Netflix, NASA, NSA, Bit torrent, and all over the world. It’s used in software development and data science. It’s good for starting compared to other programming languages because of its simplicity. PyCharm is an open-source IDE for python development.
Oops Concepts
Oops, stands for Object-Oriented Programming.
Class
A class in python is a “blueprint” for creating an object. It helps in building data and functions together to create a new instance.
Syntax:
#Define class
Class class_name:
Var1 = data1
Var2 = data2
Def fun1(self):
return(“Welcome to python”)
#Call class
C = class_name()
c.fun1()
[Output]
“Welcome to python”
Class example (Delivery system):
![](https://static.wixstatic.com/media/48a282_e14fedca345a47de88e8cee14bf07feb~mv2.jpg/v1/fill/w_754,h_437,al_c,q_80,enc_auto/48a282_e14fedca345a47de88e8cee14bf07feb~mv2.jpg)
2. Object
An object is an instance of a class. It contains all the properties of the class. One class can have more than one object.
![Python object](https://static.wixstatic.com/media/48a282_8e005d5399b848f5bca9e3b93cce09ee~mv2.jpg/v1/fill/w_739,h_196,al_c,q_80,enc_auto/48a282_8e005d5399b848f5bca9e3b93cce09ee~mv2.jpg)
3. Method
A method is a function of an object. Method access data within and may also alter an object state.
Example:
#Class Method
Class delivery:
Def cal_price(items):
return(price_data(items))
4. Inheritance
Inheritance is the fundamental property in python. It helps to inherit the features of one class into another class. The properties inherited from the class are called a parent or superclass. The properties inherited from the class are called child or sub-class.
Example:
For example:
#Parent Class
Class shape:
Def set_color(self):
return(“Red”)
#Child Class
Class circle(shape):
Def set_name(self):
return(“Circle one”)
C = circle()
c.set_color()
[Output]
“Red”
![Inheritance in python](https://static.wixstatic.com/media/48a282_397d95deac254903aa77c456733ff64c~mv2.jpg/v1/fill/w_738,h_499,al_c,q_85,enc_auto/48a282_397d95deac254903aa77c456733ff64c~mv2.jpg)
5. Encapsulation
Encapsulation helps in protecting the data in or outside of the class. It is done by using public and private data types. It helps to prevent the use and modification of some data or method in or outside of the class. Public data or methods allow access or modify it from anywhere in or outside of the class. The private data or methods can only be accessed inside the class. The private methods are represented by starting with two underscores followed by the name. Setter and getter function is used for data encapsulation in python.
Setter function:
The setter function is used to set values inside the class without directly accessing the private variable. In the example below, "setPrice" is used as a setter function to set the new price value for the computer.
Getter function:
It is the same as the setter function. The getter function is used to get the value of desired variables inside the class without directly accessing the private variable. In the example below, getPrice is used as a getter function to get the new price value for the computer.
For example:
#Encapsulation
class Computer:
def __init__(self):
self.__price = 150000
def show_price(self):
print(self.__price))
def getPrice(self):
print(self.__price))
def setPrice(self, new_price):
self.__price = new_price
#Creating object
c = Computer()
c.show_price()
#Setter function
c.setPrice(120000)
c.show_price()
#Getter function
c.getPrice(120000)
c.show_price()
[Output]
150000
120000
120000
![Encapsulation](https://static.wixstatic.com/media/48a282_c4fc7a7393dc48999c861e3b28ac69b8~mv2.jpg/v1/fill/w_742,h_438,al_c,q_80,enc_auto/48a282_c4fc7a7393dc48999c861e3b28ac69b8~mv2.jpg)
6. Polymorphism
Polymorphism is used to redefine the method with the same name from the base class. This can be performed using method overloading and method overriding.
Method overloading:
A method with the same name as the base class with a different number of arguments or data types is called method overloading.
#Parent Class
Class shape:
Def set_color(self):
return(“Red”)
#Child Class
Class circle(shape):
Def set_color(self, color=’Yellow’):
return(color)
![Method overloading in python](https://static.wixstatic.com/media/48a282_7ae2365f0b8a44fbb10859eb4e27c135~mv2.jpg/v1/fill/w_724,h_421,al_c,q_80,enc_auto/48a282_7ae2365f0b8a44fbb10859eb4e27c135~mv2.jpg)
Method overriding:
A method with the same name as the base class with added extra behaviors is called method overloading.
#Parent Class
Class shape:
Def set_color(self):
return(“Red”)
#Child Class
Class circle(shape):
Def set_color(self):
return(color)
![Method overriding in python](https://static.wixstatic.com/media/48a282_7ae2365f0b8a44fbb10859eb4e27c135~mv2.jpg/v1/fill/w_724,h_421,al_c,q_80,enc_auto/48a282_7ae2365f0b8a44fbb10859eb4e27c135~mv2.jpg)
7. Data Abstraction
Data abstraction is a way to hide the sensitive or complex part of the code from the real world. It helps to protect unnecessary details of the implementation from the users. This example, shows the image_preprocessing steps are hidden from the external users. Users just need to pass the vehicle or person images to the Image_preprocessing class and output will be generated.
![Data abstraction in python](https://static.wixstatic.com/media/48a282_3add959b8ec249ada55f0a4fed516cc1~mv2.jpg/v1/fill/w_820,h_442,al_c,q_85,enc_auto/48a282_3add959b8ec249ada55f0a4fed516cc1~mv2.jpg)
This is all about python as an object-oriented programing language. In the next blog, I will cover more on comments, docstring, modules in python.
Yorumlar