Here is the python for loop example code
cars= ["BMW", "AUDI", "TESLA"]
for x in cars :
print(x)
Here is the python for loop example2
for x in "python": print(x)
OutPut:
p
y
t
h
o
n
Tutorialshore
Here is the python for loop example code
cars= ["BMW", "AUDI", "TESLA"]
for x in cars :
print(x)
Here is the python for loop example2
for x in "python": print(x)
OutPut:
p
y
t
h
o
n
Inheritance in python is the procedure in which one class inherits the properties from another class or in simple one class inherits the methods and attributes of another class.The class whose property being inherited is called parent class(base class) and the class which inherited property form parent class is called child class.Inheritance is one of the great example of code reusability in object-oriented programming language.
class Employee: # init method or constructor def __init__(self, name,employee_id): self.employee_id = employee_id self.name = name # Sample Method def welome_emp(self): print('Hello, welcome', self.employee_id, 'Your EMployee Id is', self.name) class mechanical_depart(Employee): def depart_time(self): self.start_time = '10 am' self.close_time = '7 pm' print('Hello,', self.name ,' we will start at', self.start_time , 'and close at', self.close_time) mech = mechanical_depart('Anamika','Emp1009') mech.welome_emp() mech.depart_time()
In the above example , employee is the parent class and the mechanical_depart is the child class.so here when we create an object of mechanical department mech = mechanical_depart(‘Anamika’,’Emp1009′) .It actually using the parent class __init__ methos to assign the value of employee name and employee id.In the above mech is actually an object of mechanical_depart class but still we can use that object to call welome_emp, method inside his parent class Employee.
mech.welome_emp() is printing Hello, welcome Emp1009 Your Employee Id is Anamika out from the parent class Employee.
Like other OOPs language Python support different kind of Inheritance , Single Inheritance,Multi-level Inheritance, Multiple Inheritance and Hierarchical Inheritance
When more than one child class are inherited from the same parent class it is called Hierarchical Inheritance.In the blow image A is the parent class and B,C ,D and E are the child class derived from the parent class B this kind of inheritance is called Hierarchical Inheritance in python.
class Employee: # init method or constructor def __init__(self, name,employee_id): self.employee_id = employee_id self.name = name # Sample Method def welome_emp(self): print('Hello, welcome', self.name, 'Your EMployee Id is', self.employee_id) class mechanical_depart(Employee): def depart_time(self): self.start_time = '10 am' self.close_time = '7 pm' print('Hello,', self.name ,' we will start at', self.start_time , 'and close at', self.close_time) class IT_depart(Employee): def depart_time(self): self.start_time = '9.30 am' self.close_time = '7 pm' print('Hello,', self.name ,' we will start at', self.start_time , 'and close at', self.close_time) class HR_depart(Employee): def depart_time(self): self.start_time = '8 am' self.close_time = '5 pm' print('Hello,', self.name ,' we will start at', self.start_time , 'and close at', self.close_time) mech = mechanical_depart('Anamika','Emp1009') mech.welome_emp() mech.depart_time() hr = HR_depart('Monica','Emp1009') hr.welome_emp() hr.depart_time() Output Hello, welcome Anamika Your EMployee Id is Emp1009 Hello, Anamika we will start at 10 am and close at 7 pm Hello, welcome Monica Your EMployee Id is Emp1009 Hello, Monica we will start at 8 am and close at 5 pm
When a child class is derived from more than one parent class that type of Inheritance is called Multiple Inheritance in python.In the below image A and B are parent class and C is the child class.So C inherit the property of both A and B.
class Employee: # init method or constructor def __init__(self, name,employee_id): self.employee_id = employee_id self.name = name # Sample Method def welome_emp(self): print('Hello, welcome', self.name, 'Your EMployee Id is', self.employee_id) class IT_depart(): def depart_time(self): self.start_time = '10 am' self.close_time = '7 pm' class system_depart(Employee,IT_depart): def depart_process(self): pass system = system_depart('Joy','Emp1019') system.welome_emp() system.depart_time() Output Hello, welcome Joy Your EMployee Id is Emp1019
As the name suggest Multi-level Inheritance in python is the way Y is inherited from X, then Z is inherited from Y, so Z is indirectly Z is derived from X and Y.
class Employee: # init method or constructor def __init__(self, name,employee_id): self.employee_id = employee_id self.name = name # Sample Method def welome_emp(self): print('Hello, welcome', self.name, 'Your EMployee Id is', self.employee_id) class IT_depart(Employee): def depart_time(self): self.start_time = '10 am' self.close_time = '7 pm' class system_depart(IT_depart): def depart_process(self): pass system = system_depart('Joy','Emp1019') system.welome_emp() system.depart_time() Output Hello, welcome Joy Your EMployee Id is Emp1019
This is the simple Inheritance , in which child class derived from the one parent class
class and object are basic concept in object-oriented programming languages.a class is a collection of functions and variables, class is written by a programmer and should followed by a standard structure. object is simple an instance of a class.In this article we are going elaborate Python class and object tutorial with example.class in python is created by the keyword class.
So now we know the basic of a class,still wondering why we are using it and what are the advantages of class in python.
class Name_of_the_class: # Statement-1 . . . # Statement-N
class calculator: @staticmethod def sum(a, b): return a + b @staticmethod def multip(a, b): return a * b @staticmethod def div(a,b): return b / a cal= calculator() print(cal.sum(2,3)) print(cal.multip(2,3)) print(cal.div(2,3)) Output 5 6 1.5
We know that object is an instance of a class, in simple Object of class in python is the copy of a class with actual values in it.In the above example cal is the object of the class calculator.we can create n number of object for a single class .example cal1= calculator() is the other instance of the class calculator() like this we can create n object for the class calculator().
__init__ method is a method in python ,it is like exactly like constructor concept in PHP or Java,__init__ is automatically called when an object of that class is created.__init__ method is normally used to initialize the data in a class.
Example of __init__ method in python
class Employee: # init method or constructor def __init__(self, employee_id,name): self.employee_id = employee_id self.name = name # Sample Method def welome_emp(self): print('Hello, welcome', self.employee_id, 'Your EMployee Id id', self.name) p = Employee('Anamika','Emp1009') p.welome_emp()
To get the last value in an list using python is a very simple ,we can use Negative indexes , it is the index from the end of the List.
arr_even = [2, 4, 6, 8, 10, 12, 14 ,16 ,18]
print(arr_even[-1])
Output 18