Python3 Intermediate Level Topics
Last Updated :
27 Sep, 2019
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:
class MyClass:
number = 0
name = "noname"
def Main():
me = MyClass()
me.number = 1337
me.name = "Harssh"
print (me.name + " " + str (me.number))
if __name__ = = '__main__' :
Main()
|
Output :
Harssh 1337
- 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.
class Vector2D:
x = 0.0
y = 0.0
def Set ( self , x, y):
self .x = x
self .y = y
def Main():
vec = Vector2D()
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’
As shown in the figure the Derived class can inherit features from its base class, also it can define its own features too.
class derived - classname(superclass - name)
|
class Pet:
def __init__( self , name, age):
self .name = name
self .age = age
class Cat(Pet):
def __init__( self , name, age):
super ().__init__(name, age)
def Main():
thePet = Pet( "Pet" , 1 )
jess = Cat( "Jess" , 3 )
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)))
print (jess.name)
if __name__ = = '__main__' :
Main()
|
Output :
Is jess a cat? True
Is jess a pet? True
Is the pet a cat? False
Is thePet a Pet? True
Jess
- Iterators
Iterators are objects that can be iterated upon.
- Python uses the __iter__() method to return an iterator object of the class.
- The iterator object then uses the __next__() method to get the next item.
- for loops stops when StopIteration Exception is raised.
class Reverse:
def __init__( self , data):
self .data = data
self .index = len (data)
def __iter__( self ):
return self
def __next__( self ):
if self .index = = 0 :
raise StopIteration
self .index - = 1
return self .data[ self .index]
def Main():
rev = Reverse( 'Drapsicle' )
for char in rev:
print (char)
if __name__ = = '__main__' :
Main()
|
Output :
e
l
c
i
s
p
a
r
D
- Generators
- Another way of creating iterators.
- Uses a function rather than a separate class
- Generates the background code for the next() and iter() methods
- Uses a special statement called yield which saves the state of the generator and set a resume point for when next() is called again.
def Reverse(data):
for index in range ( len (data) - 1 , - 1 , - 1 ):
yield data[index]
def Main():
rev = Reverse( 'Harssh' )
for char in rev:
print (char)
data = 'Harssh'
print ( list (data[i] for i in range ( len (data) - 1 , - 1 , - 1 )))
if __name__ = = "__main__" :
Main()
|
Output :
h
s
s
r
a
H
['h', 's', 's', 'r', 'a', 'H']
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...