Python
Python
Python Programing
INTRODUCTION TO PYTHON
Python Programing
• Python is an interpreted high Level
Programing Language
What is Python?
Rossum and its first release was in
1991
Python Programing
• Python is more productive than other
Why Python?
programing languages
• Large community
Python Programing
Scope of Python
Information
AI Big Data
Securities
Python Programing
PYTHON SETUP
Python Programing
Software
Download Python
https://www.python.org/downloads/
Python Programing
Getting Started With Python
Python Programing
Simple Basics
Operations
Python Programing
Simple Basics
Operations
Python Programing
Simple Basics
Operations
Python Programing
Variable Name
(Glass)
Variables Value
(Water)
Python Programing
Variables
Python Programing
Variables
Python Programing
-9 -8 -7 -6 -5 -4 -3 -2 -1
HARMINDER
0 1 2 3 4 5 6 7 8
Variables
Python Programing
LISTS
Python Programing
Defining Lists
nums = [23,34,46,67,89]
LISTS
Python Programing
Accessing Elements
-5 -4 -3 -2 -1
nums = [23,34,46,67,89]
LISTS
0 1 2 3 4
>>nums[1]
34
>>nums[4]
89
>>nums[2:]
[46,67,89]
>>nums[-2]
67
Python Programing
Accessing Elements
names = [‘Vipul’,’Surender’,’Anup’,’Shubham’]
LISTS >>names
[‘Vipul’,’Surender’,’Anup’,’Shubham’]
>>names[3]
Shubham
>>names[2:]
[‘Anup’,’Shubham’]
>>names[-2]
Anup
Python Programing
Lists can have heterogeneous values
Python Programing
Multi Dimensional Lists
names = [‘Vipul’,’Surender’,’Anup’,’Shubham’]
value = [1,2,3,4]
mi = [names,value]
LISTS >>mi
[[‘Vipul’,’Surender’,’Anup’,’Shubham’] , [1,2,3,4]]
>>mi[0][3]
Shubham
>>mi[1]
[1,2,3,4]
Python Programing
Lists are Mutable
(Appending a Element)
LISTS
>> nums = [1,2,3,4,5]
>>nums.append(34)
>>nums
[1,2,3,4,5,34]
Python Programing
Lists are Mutable
(Inserting a Element)
LISTS
>> nums = [1,2,3,4,5]
>>nums.insert(3,45)
>>nums
[1,2,3,45,4,5]
Python Programing
Lists are Mutable
(Removing a Element)
LISTS
>> nums = [1,2,3,4,5]
>>nums.remove(5)
>>nums
[1,2,3,4]
Python Programing
Lists are Mutable
(Removing a Element using index)
LISTS
>> nums = [1,2,3,4,5]
>>nums.pop(2)
>>nums
[1,2,4,5]
Python Programing
Lists are Mutable
(Removing a Element from Last)
LISTS
>> nums = [1,2,3,4,5]
>>nums.pop()
5
>>nums
[1,2,3,4]
Python Programing
Lists are Mutable
(Removing multiple Elements)
LISTS
>> nums = [1,2,3,4,5]
>>del nums[0:2]
>>nums
[3,4,5]
Python Programing
Lists are Mutable
(Adding multiple Elements)
LISTS
>> nums = [1,2,3]
>>nums.extend([4,5,6])
>>nums
[1,2,3,4,5,6]
Python Programing
Lists are Mutable
(Searching Min Value in a List)
LISTS
>> nums = [23,19,85,13]
>>min(nums)
13
Python Programing
Lists are Mutable
(Searching Max Value in a List)
LISTS
>> nums = [23,19,85,13]
>>max(nums)
85
Python Programing
Lists are Mutable
(Calculate Sum of a List)
LISTS
>> nums = [23,19,85,13]
>>sum(nums)
140
Python Programing
Lists are Mutable
(Sorting a List)
LISTS
>> nums = [23,19,85,13]
>>nums.sort()
>>nums
[13,19,23,85]
Python Programing
Lists are Mutable
(Sorting a List Descending)
LISTS
>> nums = [23,19,85,13]
>>nums.sort(reverse=true)
>>nums
[85,23,19,13]
Python Programing
TUPLE
Python Programing
Defining a Tuple
nums = (23,34,46,67,89)
TUPLE
Python Programing
Accessing Elements
-5 -4 -3 -2 -1
nums = (23,34,46,67,89)
TUPLE
0 1 2 3 4
>>nums[1]
34
>>nums[4]
89
>>nums[2:]
[46,67,89]
>>nums[-2]
67
Python Programing
Accessing Elements
names = (‘Vipul’,’Surender’,’Anup’,’Shubham’)
TUPLE >>names
(‘Vipul’,’Surender’,’Anup’,’Shubham’)
>>names[3]
Shubham
>>names[2:]
(‘Anup’,’Shubham’)
>>names[-2]
Anup
Python Programing
Tuple can have heterogeneous values
Python Programing
Multi Dimensional TUPLE
names = (‘Vipul’,’Surender’,’Anup’,’Shubham’)
value = (1,2,3,4)
mi = (names,value)
TUPLE >>mi
((‘Vipul’,’Surender’,’Anup’,’Shubham’) , (1,2,3,4))
>>mi[0][3]
Shubham
>>mi[1]
(1,2,3,4)
Python Programing
Tuples are Immutable
TUPLE
>> tup = (1,2,3,4,5)
>>tup[1] = 36
Python Programing
SETS
Python Programing
Defining a SET
nums = {23,34,46,67,89}
SETS
Python Programing
SETS can have heterogeneous values
Python Programing
Check if element exists in SET
SETS
values = {8.2,’Surender’,34}
print(“surender” in values)
Python Programing
Adding Element to SETS
SETS
values = {8.2,’Surender’,34}
Values.add(“hello”)
Python Programing
Adding Multiple Element to SETS
SETS
values = {8.2,’Surender’,34}
values.update([3,4,5])
Python Programing
Removing Element From SETS
(Gives an Error when Item is not in Set)
Python Programing
Removing Element From SETS
(No Error when Item is not in Set)
Python Programing
Removing Random Element From SETS
Python Programing
Clearing SET
Python Programing
SETTING PATH FOR WINDOWS
Python Programing
Checking If already Set
Python Programing
Copy Following Paths
Python Programing
Go to Following Path
Python Programing
Go to Following Path
Python Programing
Go to Following Path
Python Programing
Go to Following Path
Python Programing
Go to Following Path
Python Programing
Verification
Python Programing
Variable Memory Concept
Python Programing
Variable Storage
num=5
Variable
(Memory Concept) 5 num
<Memory Address>
Python Programing
Getting Address
>>num=5
Variable
>>Id(num)
1936155808
(Memory Concept)
5 num
<1936155808>
Python Programing
Variables with Same value has same
memory Address
Variable
>>a=5
>>b=5
>>id(a)
1936155808
(Memory Concept) >>id(b)
1936155808
b 5 a
<1936155808>
Python Programing
Variables with Same value has same
memory Address
Variable
>>a=5
>>b=5
>>id(a)
1936155808
(Memory Concept) >>id(b)
1936155808
b 5 a
<1936155808>
Python Programing
Concept of Garbage Value
Variable
>>a=5 5
>>b=5
>>k=a <1936155808>
8 b
<1936155856>
Python Programing
Type of a Variable
>>a=5
Variable >>type(a)
<class ‘int’>
(Memory Concept) >>b=4.6
<class
‘float’>
Python Programing
Data Types
Python Programing
None
Data Types
Numeric
Sequence
Dictionary
Python Programing
Int
Complex
Python Programing
Numeric Examples
INT FLOAT
>>num=5 >>num=5.7
Data Types
>>type(num) >>type(num)
<class ‘Int’> <class ‘float’>
Complex BOOL
>>a=5
>>num = 6+9j
>>b=6
>>type(num)
>>a<b
<class ‘complex’>
True
Python Programing
Data Types Conversions
INT FLOAT FLOAT INT
>>num=5 >>num=5.7
>>float(num) >>int(num)
Data Types
>>num >>num
5.0 5
>>a = 6 >>a=5
>>b = 7 >>b=6
>>c = complex(a,b) >>c = a<b
>>c >>int(c)
6+7j 1
Python Programing
LISTS
STRING
Python Programing
Sequence Examples
LISTS TUPLE
Data Types
>>type(a) >>type(a)
<class ‘List’> <class ‘Tuple’>
STRING RANGE
Python Programing
Dictionary
Definition Accessing Keys
>>a= {‘name’:’Harminder’,’class’:’1st’}
Data Types
>>a= {‘name’:’Harminder’,’class’:’1st’}
>>a.keys()
>>type(a)
dict_keys{[‘name’,’class’]}
<class ‘Dict’>
Python Programing
OPERATORS
Python Programing
Operators
Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Python Programing
Addition
Operators Division
Arithmetic
Operators
Subtraction
Multiplication
Python Programing
Arithmetic Operators
Addition Subtraction
>>a=5 >>a=5
>>b=6 >>b=6
Operators
>>a+b >>b-a
11 1
Multiplication Division
>>a=5 >>a=30
>>b=6 >>b=5
>>a*b >>a/b
30 6
Python Programing
Addition
Assignment
Operators Division
Assignment
Assignment
Operators
Subtraction
Assignment
Multiplication
Assignment
Python Programing
Assignment Operators
Addition Assignment Subtraction Assignment
>>a=5 >>a=5
>>a += 2 >>a-=2
Operators
>>a >>a
7 3
Multiplication
Division Assignment
Assignment
>>a=5 >>a=25
>>a*=3 >>a /= 5
>>a >>a
15 5.0
Python Programing
Assignment Operators
Operators
>>a,b=5,8
>>a
5
>>b
8
Python Programing
>
!= <
Operators Relational
Operators
== >=
<=
Python Programing
Relational Operators
< >
>>a=5 >>a=5
>>b=2 >>b=2
Operators
>>a<b >>a>b
False True
>= <=
>>a=5 >>a=5
>>b=2 >>b=2
>>a>=b >>a<=b
True False
Python Programing
Relational Operators
Operators
== !=
>>a=5 >>a=5
>>b=5 >>b=2
>>a==b >>a!=b
True True
Python Programing
AND
Operators Logical
Operators
OR
Python Programing
Logical Operators
Operators
AND OR
>>a=5 >>a=5
>>b=2 >>b=2
>>a>5 and b=2 >>a>5 or b=2
False True
Python Programing
BITWISE OPERATOR
Python Programing
AND (&)
Bitwise OR (|)
Operators
XOR (^)
Python Programing
12 1100
Decimal to Binary Conversion
Bitwise
Operators
2 12
2 6 0
2 3 0
1 1
Python Programing
1100 12
Binary to Decimal Conversion
Bitwise
Operators 1 1 0 0
2 3 + 22 + 2 1 + 2 0
8+4=12
Python Programing
12 & 13 = 12
Bitwise (AND)
Bitwise
Operators
00001100 -> 12
00001101 -> 13
00001100 -> 12
Python Programing
12 | 13 = 13
Bitwise (OR)
Bitwise
Operators
00001100 -> 12
00001101 -> 13
00001101 -> 13
Python Programing
12 ^ 13 = 1
Bitwise (XOR)
Bitwise
Operators
00001100 -> 12
00001101 -> 13
00000001 -> 1
Python Programing
10 << 2 = 40
Left Shift (<<)
Bitwise
Operators 00001010.000 -> 10
0000101000.0 -> 40
Python Programing
10 >> 2 = 2
Right Shift (>>)
Bitwise
Operators 00001010.000 -> 10
000010.10000 -> 2
Python Programing
Math Module
Python Programing
Importing Math Module
Python Programing
Finding Square Root
>>Import math
Math Module >>x=math.sqrt(25)
>>x
5
Python Programing
Math Functions
Ceiling
5 Floor Ceil
Math Module
>>x=math.floor(4.9)
>>x=math.ceil(4.1)
>>x
>>x
4
5
Power
>>x=math.pow(4,2)
4 >>x
16
Floor
Python Programing
Alice Math Module
>>Import math as m
Math Module >>x=m.sqrt(25)
>>x
5
Python Programing
Importing Specific functions of Math
Module
Python Programing
Creating & Running Python Files
Python Programing
Write a Program on Notepad/IDE
Creating &
Running Py Files
Python Programing
Save file with PY Extension
Creating &
Running Py Files
Python Programing
Open CMD and Change path to file’s
location
Creating &
Running Py Files
Python Programing
Call the python file
Creating &
Running Py Files
Python Programing
User Input
Python Programing
Input Function
Python Programing
Input Function only accept strings
User Input
Print(type(a))
Python Programing
Input Function only accept strings
User Input
c=x+y
print(c)
Python Programing
Passing Argument Input in CMD
Import sys
User Input
x=sys.argv[1]
y=sys.argv[2]
c=x+y
print(c)
Python Programing
Passing Argument Input in CMD
User Input
[0] [1] [2]
Python Programing
Control Flow Statements
Python Programing
Central Processing Unit
Control Flow
Control Unit
Statements
Arithmetic/Logical Unit
Memory Unit
Python Programing
IF Statement
Control Flow
Suite
X=5
Statements
If x==5:
print(“equal to five”)
Python Programing
IF Statement Needs Indentation
Control Flow
X=5
If x==3:
Statements
print(“equal to five”)
print(“hello”)
Python Programing
Else Statement
Control Flow
X=5
If x==5:
Statements
print(“equal to five”)
else:
print(“Not Equal”)
Python Programing
Nested IF Statement
Control Flow
X=5
If x>=5:
Statements
print(“x is greater”)
if x==5:
print(“x is equal”)
else:
print(“x is smaller”)
Python Programing
Nested IF Statement
X=5
Control Flow
If x==1:
print(“One”)
Statements
elif x==2:
print(“Two”)
elif x==3:
print(“Three”)
else:
print(“Wrong Input”)
Python Programing
Loops
Python Programing
While Loop
x=0 Initialization
Loops
while x<=5:
print(i)
x=x+1
Condition
Increment
Python Programing
While Loop (Reverse)
x=5 Initialization
Loops
while x>=0:
print(i)
x=x-1
Condition
Increment
Python Programing
While Loop (Nested)
x=0
while x<=5:
Loops
print("Python",end="")
j=0
while j<=5:
print("Rocks",end="")
j=j+1
x=x+1
print()
Python Programing
For Loop with List
a = [“Harminder”,1,”Surender”]
Loops for i in a:
print(i)
Python Programing
For Loop with String
a = “Harminder”
Loops for i in a:
print(i)
Python Programing
For Loop with Tuple
Loops
a = (“hi”,”harminder”,”surender”)
for i in a:
print(i)
Python Programing
For Loop with Sets
Loops
a = {“hi”,”harminder”,”surender”}
for i in a:
print(i)
Python Programing
For Loop with Range
Loops
for i in range(10):
print(i)
Python Programing
For Loop with Range
Loops
for i in range(10,21,1):
print(i)
Python Programing
For Loop with Range
Loops
for i in range(20,0,-1):
print(i)
Python Programing
Nested For Loop
for i in range(5):
Python Programing
Break Statement
for i in range(1,10,1):
Loops if i==5:
break
print(i)
Python Programing
Continue Statement
for i in range(1,10,1):
Loops if i==5:
continue
print(i)
Python Programing
Pass Statement
for i in range(1,100,1):
Loops if i%2!=0:
pass
else:
print(i)
Python Programing
For Else
a=[1,2,3,4,6,7]
Loops for i in a:
if i%5=0:
break
else:
print(“Not Found”)
Python Programing
Functions
Python Programing
Function Definition
Functions
def greet():
print(“Hello”)
print(“Good Morning”)
Python Programing
Passing Parameter to function
Formal Arguments
Functions
def add(x,y):
c=x+y
print(c)
add(4,5)
Actual Argument
Python Programing
Types of arguments
Position
Functions
Keyword
Default
Variable length
Python Programing
Position Argument
def person(name,age):
Functions print(name)
print(age)
person(“Harminder”,29)
Python Programing
Keyword Argument
Functions
def person(name,age):
print(name)
print(age)
person(age=11,name="Harminder")
Python Programing
Default Argument
Functions
def person(name,age):
print(name)
print(age)
person(age=11,name="Harminder")
Python Programing
Variable Length Argument
def sum(a,*b):
Functions
for i in b:
c=a+i
print(c)
sum(2,4,6)
Python Programing
Keyworded Variable Length Argument
def person(a,**b):
Functions
print(a)
print(b)
person("Harminder",city="faridabad",age=19)
Python Programing
Keyworded Variable Length Argument
def person(a,**b):
Functions
print(a)
for i,j in b.items():
print(i,j)
person("Harminder",city="faridabad",age=19)
Python Programing
Returning values from function
def add(x,y):
Functions c=x+y
return c
a=add(4,5)
print(a)
Python Programing
Returning multiple values from function
def add_sub(x,y):
Functions c=x+y
d=x-y
return c,d
a,b=add_sub(4,5)
print(a,b)
Python Programing
Global & Local Variables
a=10
def hello():
Functions
a=15
print(a)
hello()
print(a)
Python Programing
Local Variables can only be used inside
function
def hello():
Functions
a=15
print(a)
print(a)
Python Programing
Global Variables can be used anywhere in
Program
a=10
Functions
def hello():
print(a)
hello()
Python Programing
Changing Value of a global Variable
a=10
Functions
def hello():
global a
a=15
print(a)
hello()
print(a)
Python Programing
Passing List/tuple/set to a function
def hello(a,b,c):
print(a)
Functions
print(b)
print(c)
a=[1,2,3,4,5]
b=(1,2,3,4,5)
c={1,2,3,4,5}
hello(a,b,c)
Python Programing
Anonymous Function(LAMBDA)
f= lambda a,b:a+b
print(result)
Python Programing
Using Filter with lambda
nums = [2,3,45,6,7,8,80]
for i in r:
print(i)
Python Programing
Using Map with lambda
nums = [2,3,45,6,7,8,80]
for i in r:
print(i)
Python Programing
Using Reduce with lambda
print(r)
Python Programing
Creating Modules in Python
def add(a,b):
c=a+b
return c
Functions def sub(a,b):
c=a-b
return c
def mul(a,b):
c=a*b
return c
Python Programing
Using User Defined Modules in Python
import hello as h
Functions r=h.add(3,4)
print(r)
Python Programing
Special Variable
Python Programing
import hi as h def add():
print("This is add function")
def fun1():
print("This is function 1") def mul():
h.add() print("This is mul function")
Python Programing
main()
Object Oriented Programing
Python Programing
Class Definition
class a:
OOP
def add(self):
print(“This is add function")
obj = a()
a.add(obj)
obj.add()
Python Programing
Multiple Object of a class
class a:
OOP
def add(self):
print(“This is add function")
obj = a()
obj2=a()
obj.add()
obj2.add()
Python Programing
__init__ Method
class a:
OOP
def __init__(self)
print(“This is init function")
obj = a()
Python Programing
Passing Arguments to a Method
class person:
OOP
def a(self,name):
print("Hi",name)
obj = person()
obj.a("Harminder")
Python Programing
Accessing Variable
class person:
OOP
x=1
obj = person()
print(obj.x)
Python Programing
class a:
Binding variable to object def b(self,k=5,n=4):
self.k=k
self.n=n
def c(self):
OOP
print(self.k,self.n)
obj = a()
obj.b(44,67)
obj.c()
obj2 = a()
obj2.b()
obj2.c()
Python Programing
Binding variable to object using class a:
__init__ def __init__(self,k=5,n=4):
self.k=k
self.n=n
OOP
def c(self):
print(self.k,self.n)
obj = a(44,67)
obj.c()
obj2 = a()
obj2.c()
Python Programing
Instance Variable class a:
def __init__(self):
self.b=5
OOP
c1=a()
c2=a()
print(c1.b)
print(c2.b)
c1.b=10
print(c1.b)
print(c2.b)
Python Programing
Class Variable class a:
x=4
def __init__(self):
self.b=5
c1=a()
OOP
c2=a()
print(c1.x)
print(c2.x)
a.x=15
print(c1.x)
print(c2.x)
c1.x=55
print(c1.x)
print(c2.x)
Python Programing
Types of Methods
(Instance Methods) class student:
def __init__(self,m1,m2,m3):
self.m1=m1
self.m2=m2
OOP
self.m3=m3
def avg(self):
return (self.m1+self.m2+self.m3)/3
s1 = student(23,56,44)
s2 = student(90,89,45)
print(s1.avg())
print(s2.avg())
Python Programing
Types of Methods
(Instance Methods)
class student:
def __init__(self):
self.a="Harminder"
Accessors Mutators
def get_a(self):
print(self.a)
OOP
s1 = student()
s1.get_a()
Python Programing
Types of Methods
(Instance Methods)
class student:
def __init__(self):
self.a="Harminder"
Accessors Mutators
def set_a(self):
self.a="surender"
return self.a
OOP
s1 = student()
print(s1.a)
print(s1.set_a())
Python Programing
Types of Methods
(Class Methods)
class student:
school="vsics"
OOP
@classmethod
def get_school(cls):
print(cls.school)
s1=student()
student.get_school()
Python Programing
Types of Methods
(Static Methods)
class student:
@staticmethod
def a():
OOP
print("hi")
s1=student()
s1.a()
Python Programing
Inner Class
class student:
def a(self):
print("hi")
self.obj = self.b()
OOP
self.obj.hello()
class b:
def hello(self):
print("hello")
s1=student()
s1.a()
Python Programing
Inner Class
(using Object of inner class outside main class) class student:
def a(self):
print("hi")
self.obj = self.b()
OOP
class b:
def hello(self):
print("hello")
s1=student()
s1.a()
s1.obj.hello()
Python Programing
Inner Class
(Defining Object of inner class outside main class) class student:
def a(self):
print("hi")
OOP
class b:
def hello(self):
print("hello")
s1=student()
obj=s1.b()
obj.hello()
Python Programing
Inheritance class a:
def feature1(self):
print("Feature 1 is working")
def feature2(self):
print("Feature 2 is working")
def feature4(self):
print("Feature 4 is working")
obj1 = b()
obj1.feature1()
Python Programing
class a:
Multi Level Inheritance def feature1(self):
print("Feature 1 is working")
def feature2(self):
print("Feature 2 is working")
OOP
class b(a):
def feature3(self):
print("Feature 3 is working")
def feature4(self):
print("Feature 4 is working")
class c(b):
def feature5(self):
print("Feature 5 is working")
obj1 = c()
obj1.feature1()
Python Programing
class a:
Multiple Inheritance def feature1(self):
print("Feature 1 is working")
def feature2(self):
print("Feature 2 is working")
OOP
class b:
def feature3(self):
print("Feature 3 is working")
def feature4(self):
print("Feature 4 is working")
class c(a,b):
def feature5(self):
print("Feature 5 is working")
obj1 = c()
obj1.feature1()
Python Programing
class a:
Constructor Behavior in def __init__(self):
OOP
class b(a):
def __init__(self):
super().__init__()
print("Init of b")
def feature3(self):
print("This is feature 3")
def feature4(self):
print("This is Feature 4")
k = b()
Python Programing
Constructor Behavior in Multiple
class a:
def __init__(self):
super().__init__()
Inheritance (MRO)
print("Init of a")
def feature1(self):
print("This is feature 1")
def feature2(self):
print("This is Feature 2")
OOP
class b:
def __init__(self):
super().__init__()
print("Init of b")
def feature3(self):
print("This is feature 3")
def feature4(self):
print("This is Feature 4")
class c(a,b):
def __init__(self):
super().__init__()
print("Init of c")
def feat(self):
print("This is feat")
k = c()
Python Programing
Polymorphism
OOP
Duck Typing
Operator Overloading
Method Overriding
Python Programing
Using methods in other classes
class b:
def k(self):
print("This is k function")
OOP
class a:
def a(self,obj2):
obj2.k()
obj2=b()
obj = a()
obj.a(obj2)
Python Programing
Duck Typing class b:
def k(self):
print("This is k function")
class a:
def a(self,obj2):
OOP
obj2.k()
class d:
def k(self):
print("This is k in d")
obj2=d()
obj = a()
obj.a(obj2)
Python Programing
Operator Overloading
Operators
OOP 5+2
Operands
Python Programing
Operator Overloading
(Everything in python is a class)
a=4
OOP
b=5
c=a+b
print(c)
print(int.__add__(a,b))
Python Programing
Operator Overloading
(Int class has various methods)
+ •__add__()
OOP
- •__sub__()
* •__mul__()
Python Programing
Overloading Addition Operator
class a:
def __init__(self,m1,m2):
self.m1=m1
self.m2=m2
OOP
def __add__(obj1,obj2):
x = obj1.m1+obj2.m1
y = obj1.m2+obj2.m2
z = a(x,y)
return z
s1 = a(3,4)
s2 = a(44,55)
s3 = s1+s2
print(s3.m1)
Python Programing
Overloading Greater than Operator
class a:
def __init__(self,m1,m2):
self.m1=m1
self.m2=m2
def __gt__(obj1,obj2):
OOP
x = obj1.m1+obj1.m2
y = obj2.m1+obj2.m2
if x>y:
return True
else:
return False
s1 = a(3,4)
s2 = a(44,55)
if s1>s2:
print("s1 wins")
else:
print("s2 wins")
Python Programing
Method Overriding
class a:
def greet(self):
OOP
print("Welcome to class a")
class b(a):
def greet(self):
print("Welcome to class b")
obj = b()
obj.greet()
Python Programing
Iterator
OOP
a = [2,33,45,67,890,3]
c = iter(a)
print(c.__next__())
for i in a:
print(c.__next__())
Python Programing
Generators Example 1
def hello():
OOP
yield 1
yield 2
yield 3
values=hello()
print(values.__next__())
print(values.__next__())
print(values.__next__())
Python Programing
Generators Example 2
def sq():
OOP
n=1
while n<=10:
yield n*n
n+=1
values=sq()
print(next(values))
for i in values:
print(i)
Python Programing
Exception Handling
Python Programing
Types of Errors
Exception
Handling
Compile Time
Logical
Runtime Error
Python Programing
Types of Statements
a=25
Exception b=5
Normal Statement
Handling
c=a/b Critical Statement
Python Programing
Runtime Error Example
a=25
Exception b=0
Handling print(a/b)
Python Programing
Runtime Error Example
a=5
Exception
b=0
Handling
try:
print(a/b)
except Exception:
print("You cannot divide a number
by zero")
print("bye")
Python Programing
Try/Except/finally
a=5
b=2
Exception try:
Handling
print("Calculation mode started")
print(a/b)
except Exception:
print("You cannot divide a number by
zero")
finally:
print("Calculation mode closed")
Python Programing
Handling Specific Errors
a=5
b=2
Exception try:
Handling
print("Calculation mode started")
print(a/b)
except ZeroDivisonError:
print("You cannot divide a number by
zero")
finally:
print("Calculation mode closed")
Python Programing
Multi Threading
Python Programing
Multi Threading
from threading import *
from time import sleep
Multi
class hello(Thread):
def run(self):
for i in range(0,50):
Threading
print("hello")
sleep(1)
class hi(Thread):
def run(self):
for i in range(0,50):
print("hi")
sleep(1)
t1=hello()
t2=hi()
t1.start()
sleep(0.2)
t2.start()
Python Programing
Concept of Join
from threading import *
from time import sleep
class hello(Thread):
def run(self):
for i in range(0,10):
Multi
print("hello")
sleep(1)
Threading
class hi(Thread):
def run(self):
for i in range(0,10):
print("hi")
sleep(1)
t1=hello()
t2=hi()
t1.start()
sleep(0.2)
t2.start()
t1.join()
print("bye")
Python Programing
File Handling
Python Programing
Opening a file in Python
open(“filename”,”mode”)
File
Handling
"r" - Read - Default value. Opens a file for reading, error if the file does not exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
Python Programing
Reading Complete file
File f=open("hello.txt","r")
print(f.read())
Handling
Python Programing
Reading bits of a file
File f=open("hello.txt","r")
print(f.read(6))
Handling
Python Programing
Reading one line at a time
File f=open("hello.txt","r")
print(f.readline())
Handling print(f.readline())
Python Programing
Reading Bits of a line
File f=open("hello.txt","r")
print(f.readline())
Handling print(f.readline(4))
Python Programing
Writing a file
File f=open("hello.txt","w")
f.write("hi who are
Handling you??")
Python Programing
Append to a file
File f=open("hello.txt",“a")
f.write("hi who are
Handling you??")
Python Programing
Using for loop with file handler
f=open("hello.txt","r")
File
Handling
f1=open("hi.txt","a")
for i in f:
f1.write(i)
Python Programing
Removing a file
File import os
Handling os.remove("hi.txt")
Python Programing
Removing a file
import os
File
Handling
if os.path.exists("hello.txt"):
os.remove("hello.txt")
else:
print("no file")
Python Programing
DJANGO
Python Programing
Django is a high-level Python Web
framework that encourages rapid
development and clean, pragmatic
What is Django
design. Built by experienced developers,
it takes care of much of the hassle of
Web development, so you can focus on
writing your app without needing to
reinvent the wheel. It’s free and open
source.
Python Programing
• Fast
Why Django • Secure
• Scalable
Python Programing
Firstly we will install virtual environment wrapper
Installation
Python Programing
Firstly we will create a virtual environment for Django
mkvirtualenv axpino
Installation
Python Programing
Now we will install Django into our Environment
Installation
Python Programing
Now we will create a folder to store our projects
and navigate to it
mkdir projects
cd projects
Installation
Python Programing
Now we will create our first project
Installation
Python Programing
Navigate to project folder and start server
cd projectname
python manage.py runserver
Installation
Python Programing
Now lets access the main url of project
Installation
Python Programing
Firstly we will create a app inside our project
Python Programing
Lets check our app folder
Creating views
Python Programing
Lets check our app folder
Creating views
Python Programing
To create a url we need to create a file with name urls in our app folder
Creating views
Python Programing
Let us add a path in urls file in our app
Creating views
from . import views
urlpatterns = [
path('',views.home,name="home")
]
Python Programing
Let create a view function
Creating views
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse("This is my webpage")
Python Programing
Let add path in main project url
Creating views
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('',include('calc.urls’)),
path('admin/', admin.site.urls),
]
Python Programing
Restart the Server
Creating views
Python Programing
Access the project using browser
Creating views
Python Programing