filmov
tv
Python Intermediate level topics like modules ,classes , method , iterators , Decorators ,Generators

Показать описание
Master guide for intermediate level Python coders
#Programmers #python3 #pythonprogramming #pythons #pycoders #Pythonsk #programmer #java #JavaScript #shift #javadeveloper #php #code #coder #engineering #YouTube #data #datascience #analytics #development #developer #engineer #cpp #django #flask #webdesign #machinelearning #ai #language #technology
Python3 Intermediate Level Topics
After going through the basics of python, you would be interested to know more about further and bit more advance topics of the Python3 programming language.
This article covers them.
Please remember that Python completely works on indentation and it is advised to practice it a bit by running some programs. Use the tab key to provide indentation to your code.
This article is divided in following five sections:
Classes
Just like every other Object Oriented Programming language Python supports classes. Let’s look at some points on Python classes.
Classes are created by keyword class.
Attributes are the variables that belong to class.
Attributes are always public and can be accessed using dot (.) operator. Eg.: Myclass.Myattribute
A sample E.g for classes:
# creates a class named MyClass
class MyClass:
# assign the values to the MyClass attributes
number = 0
name = "noname"
def Main():
# Creating an object of the MyClass.
# Here, 'me' is the object
me = MyClass()
# Accessing the attributes of MyClass
# using the dot(.) operator
# str is an build-in function that
# creates an string
# telling python that there is main in the program.
if __name__=='__main__':
Main()
Output :
Methods
Method is a bunch of code that is intended to perform a particular task in your Python’s code.
Function that belongs to a class is called an Method.
All methods require ‘self’ parameter. If you have coded in other OOP language you can think of ‘self’ as the ‘this’ keyword which is used for the current object. It unhides the current instance variable.’self’ mostly work like ‘this’.
‘def’ keyword is used to create a new method.
# A Python program to demonstrate working of class
# methods
class Vector2D:
x = 0.0
y = 0.0
# Creating a method named Set
def Set(self, x, y):
self.x = x
self.y = y
def Main():
# vec is an object of class Vector2D
vec = Vector2D()
# Passing values to the function Set
# by using dot(.) operator.
vec.Set(5, 6)
print("X: " + str(vec.x) + ", Y: " + str(vec.y))
if __name__=='__main__':
Main()
Output :
X: 5, Y: 6
Inheritance
Inheritance is defined as a way in which a particular class inherits features from its base class.Base class is also knows as ‘Superclass’ and the class which inherits from the Superclass is knows as ‘Subclass’
Inheritance
As shown in the figure the Derived class can inherit features from its base class, also it can define its own features too.
# Syntax for inheritance
class derived-classname(superclass-name)
# A Python program to demonstrate working of inheritance
class Pet:
#__init__ is an constructor in Python
def __init__(self, name, age):
# Class Cat inheriting from the class Pet
class Cat(Pet):
def __init__(self, name, age):
# calling the super-class function __init__
# using the super() function
super().__init__(name, age)
def Main():
thePet = Pet("Pet", 1)
jess = Cat("Jess", 3)
# isinstance() function to check whether a class is
# inherited from another class
print("Is jess a cat? " +str(isinstance(jess, Cat)))
print("Is jess a pet? " +str(isinstance(jess, Pet)))
print("Is the pet a cat? "+str(isinstance(thePet, Cat)))
print("Is thePet a Pet? " +str(isinstance(thePet, Pet)))
if __name__=='__main__':
Main()
#Programmers #python3 #pythonprogramming #pythons #pycoders #Pythonsk #programmer #java #JavaScript #shift #javadeveloper #php #code #coder #engineering #YouTube #data #datascience #analytics #development #developer #engineer #cpp #django #flask #webdesign #machinelearning #ai #language #technology
Python3 Intermediate Level Topics
After going through the basics of python, you would be interested to know more about further and bit more advance topics of the Python3 programming language.
This article covers them.
Please remember that Python completely works on indentation and it is advised to practice it a bit by running some programs. Use the tab key to provide indentation to your code.
This article is divided in following five sections:
Classes
Just like every other Object Oriented Programming language Python supports classes. Let’s look at some points on Python classes.
Classes are created by keyword class.
Attributes are the variables that belong to class.
Attributes are always public and can be accessed using dot (.) operator. Eg.: Myclass.Myattribute
A sample E.g for classes:
# creates a class named MyClass
class MyClass:
# assign the values to the MyClass attributes
number = 0
name = "noname"
def Main():
# Creating an object of the MyClass.
# Here, 'me' is the object
me = MyClass()
# Accessing the attributes of MyClass
# using the dot(.) operator
# str is an build-in function that
# creates an string
# telling python that there is main in the program.
if __name__=='__main__':
Main()
Output :
Methods
Method is a bunch of code that is intended to perform a particular task in your Python’s code.
Function that belongs to a class is called an Method.
All methods require ‘self’ parameter. If you have coded in other OOP language you can think of ‘self’ as the ‘this’ keyword which is used for the current object. It unhides the current instance variable.’self’ mostly work like ‘this’.
‘def’ keyword is used to create a new method.
# A Python program to demonstrate working of class
# methods
class Vector2D:
x = 0.0
y = 0.0
# Creating a method named Set
def Set(self, x, y):
self.x = x
self.y = y
def Main():
# vec is an object of class Vector2D
vec = Vector2D()
# Passing values to the function Set
# by using dot(.) operator.
vec.Set(5, 6)
print("X: " + str(vec.x) + ", Y: " + str(vec.y))
if __name__=='__main__':
Main()
Output :
X: 5, Y: 6
Inheritance
Inheritance is defined as a way in which a particular class inherits features from its base class.Base class is also knows as ‘Superclass’ and the class which inherits from the Superclass is knows as ‘Subclass’
Inheritance
As shown in the figure the Derived class can inherit features from its base class, also it can define its own features too.
# Syntax for inheritance
class derived-classname(superclass-name)
# A Python program to demonstrate working of inheritance
class Pet:
#__init__ is an constructor in Python
def __init__(self, name, age):
# Class Cat inheriting from the class Pet
class Cat(Pet):
def __init__(self, name, age):
# calling the super-class function __init__
# using the super() function
super().__init__(name, age)
def Main():
thePet = Pet("Pet", 1)
jess = Cat("Jess", 3)
# isinstance() function to check whether a class is
# inherited from another class
print("Is jess a cat? " +str(isinstance(jess, Cat)))
print("Is jess a pet? " +str(isinstance(jess, Pet)))
print("Is the pet a cat? "+str(isinstance(thePet, Cat)))
print("Is thePet a Pet? " +str(isinstance(thePet, Pet)))
if __name__=='__main__':
Main()