Python Notes
Python Notes
Tuesday
Subtitle
Title
To get started right away, just click any
placeholder text (such as this) and start typing
to replace it with your own. Want to insert a
picture from your files or add a shape? You got
it! On the Insert tab of the ribbon, just tap the
option you need.
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
00311 00311 00311 00311 00311 00311 00311
00311 00311
View and edit this document in Word on your computer, tablet, or phone. You can
edit text; easily insert content such as pictures, shapes, and tables; and seamlessly
save the document to the cloud from Word on your Windows, Mac, Android, or iOS
device.
Python Notes Day/Datewise:
1st September
input and output statements or functions
=================================
==> input is data or information given to the program. in input data is flow inside
the program. input is given to the program from different
source(keyboard,file,database,...).
==> process is performing some operations.
==> output is result, this output is displayed on monitor,file,database or any dest.
eg:
print() function
============
==> print function available in builtins module. this is default module imported by
any python program.
print(100,200,300)
print(100,200,300,sep=',')
print(101,"Naresh","Python",4000.0,sep=":")
What is output?
1. print(100,200,300,sep='\n')
100
200
300
2. print(100,200,300,sep='$')
100$200$300
3. print("Python",sep="%")
Python
4. print(100,200,300,end='$')
print("Python")
eg:
f=open("d:\\file1","w")
print("Hello Python")
print("Hello Python",file=f,flush=True)
input()
=====
==> input() is predefined function in python.
==> this function is available in builtins module.
==> this function is used to read data from keyboard at runtime.
==> this function return input value as string type.
==> it read only one value.
syntax: input([prompt])
eg:
a=input()
b=input()
c=input()
d=a+b+c
print(a,b,c,d)
print(type(a),type(b),type(c),type(d))
eg:
int()
===
==> this function is used to create an integer object using int value,float value and
integer value represented as string.
==> int is conversion function.
syntax-1: int(value)
syntax-2: int(value,base=10)
2nd September
nt()
===
==> this function is used to create an integer object using int value,float value and
integer value represented as string.
==> int is conversion function.
syntax-1: int(value)
syntax-2: int(value,base=10)
s1="65"
x=int(s1)
s2="0o45"
y=int(s2) ==> Error
y=int(s2,base=8)
s3="0xab"
z=int(s3) ==> Error
z=int(s3,base=16)
s4="0b101"
p=int(s4) ==> Error
p=int(s4,base=2)
# find output
x="65"
y=int(x,base=8)
print(x,y)
# Find output
s3="65"
a=int(s3)
b=int(s3,base=8)
c=int(s3,base=16)
#d=int(s3,base=2)
print(a,b,c)
eg:
n1=int(True)
n2=int(False)
print(n1,n2)
float()
=====
==> this function is used to convert int to float,float to float and string to float.
float(value)
==> value can be int,float or float value represented as string.
eg:
a=float(15)
b=float(1.5)
c=float("17.65")
print(a,b,c)
eg:
a="1.5"
b="15e-1"
x=float(a)
y=float(b)
print(a,b)
print(x,y)
print(type(a),type(b))
print(type(x),type(y))
eg:
base=float(input("Enter base"))
height=float(input("Enter height"))
area=0.5*base*height
print(base,height,area)
complex()
========
bool()
=====
==> it is used to convert int to boolean
x=bool("True")
y=bool("False")
print(x,y) ==> True,True
z=bool("0")
print(z) ==> True
a=bool("nit")
print(a) ==> True
a=bool(0)
b=bool(1)
print(a,b) ==> False,True
Operators
=========
Q: What is operator?
Types of operators
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
Arithmetic Operators
=================
+ Add
- sub
* multiply
/ float division
// floor division
% modulas
** expo/pow of operator
+
=
In python + operator is used to perform two operations.
1. adding numbers
2. concat strings
=> if two operands are numbers it perform addition
=> if two operands are strings it perform concat
eg:
res1=100+200
res2=1.4+2.4
res3=1+2j+1+3j
res4="nit"+"Python"
res5="15"+"25"
res6="1.5"+"2.5"
print(res1,res2,res3,res4,res5,res6,sep='\n')
output:
300
3.8
(2+5j)
nitPython
1525
1.52.5
3rd September
==> every operator in python is bind with function.
>>> n1=10
>>> n2=20
>>> n3=n1.__add__(n2)
>>> print(n1,n2,n3)
10 20 30
>>> n4=n1+n2
>>> n4
30
n1=100
n2=50
n3=n1-n2
f1=2.5
f2=1.2
f3=f1-f2
c1=1+2j
c2=2+5j
c3=c1-c2
print(n1,n2,n3)
print(f1,f2,f3)
print(c1,c2,c3)
Find Output?
x=True+True
y=True+False
z=False-True
print(x)
print(y)
print(z)
output : 2 1 -1
Find output?
z=True+10
x=10-True
print(x)
print(z)
Output: 11 9
n1=10
n2=5
n3=n1*n2
f1=1.5
f2=2.3
f3=f1*f2
print(n1,n2,n3)
print(f1,f2,f3)
====================================
str1="python"
str2=str1*3
print(str1)
print(str2)
list1=[10,20,30,40,50]
list2=list1*2
print(list1)
print(list2)
==> this operator perform division operation and return result in float type.
int/int=float
float/float=float
int/float=float
float/int=float
x=5
y=2
z=x/y
print(x,y,z)
n1=4
n2=2
n3=n1/n2
print(n1,n2,n3)
p=4.5
q=2
r=p/q
print(p,q,r)
// floor division
==> this operator perform division and return result as int type.
n1=5
n2=2
n3=n1//n2
print(n1,n2,n3)
a=5
b=2.0
c=a//b
print(a,b,c)
x=9
y=4
z=x//y
print(x,y,z)
p=x/y
print(x,y,p)
m=9.0
n=4
o=m//n
print(m,n,o)
5/2=2.5
5//2=2
5//2.0=2.0
5%2=1
4%2=0
9%3=0
9%2=1
num**p
res1=5**2
res2=6**2
print(res1,res2)
===========================================================
==========
rno=int(input("Enter Rollno"))
name=input("Enter name")
sub1=int(input("Enter subject1"))
sub2=int(input("Enter subject2"))
sub3=int(input("Enter subject3"))
total=sub1+sub2+sub3
avg1=total/3
avg2=total//3
print("*"*40)
print("Rollno ",rno)
print("Name ",name)
print("Subject1 ",sub1)
print("Subject2 ",sub2)
print("Subject3 ",sub3)
print("*"*40)
print("Total ",total)
print("Avg ",avg1,avg2)
print("*"*40)
===========================================================
=======
temp conversion
c=(f-32)*5/9
print(c)
+ - * / // % **
===========================================================
===========
Relational Operators
=================
==> Relational operators are used to find relation between two operands.
==> Relational operators are binary operators, which required two opeands.
4th September
Relational Operators
=================
==> Relational operators are used to find relation between two operands.
==> Relational operators are binary operators, which required two opeands.
a=10
b=5
c=a>b
d=a<b
print(a,b,c,d)
e=b<a
print(e)
x=100
y=100
z=x>y
print(x,y,z)
p=x>=y
print(p)
================================================
a=100
b=200
c=100
d=a==b
e=a==c
print(a,b,c,d,e)
=================================================
Logical operator
==> Logical operators are used to combine two conditions or boolean expressions.
==> Logical operators are represented using three keywords.
1. and
2. or
3. not
eg:
a=True or False
b=False or True
c=True or True
d=False or False
print(a,b,c,d)
a=10>20 or 20>10
print(a)
b=20>10 or 10>20
print(b)
not operator
==========
truth table of not operator
x=not True
y=not False
exists=True
z=not exists
exists=False
p=not exists
print(p)
lock=True
l1=not lock
lock=False
l2=not lock
print(l1,l2)
===========================================================
Bitwise operator
==============
shift operators
leftshift operator is used to add bits at right side by shift bits towards leftside. this
allows to increment value by adding bits.
right shift operator is used to remove bits at right side by shifting bits towards
rightside. this allows to decrement value by removing bits.
x=12
y=x>>1
print(x,y)
z=x<<1
print(x,z)
b1=0b1010
b2=b1<<2
print(b1,b2)
print(bin(b1),bin(b2))
b3=0b1010
b4=b3>>1
print(b3,b4)
print(bin(b3),bin(b4))
num=5
x=num>>2
bitwise & (AND)
bitwise | (OR)
bitwise ^ (XOR)
bitwise ~ (complement)
truth table
1 0 0
0 1 0
1 1 1
0 0 0
x=5 ==> 1 0 1
y=5 ==> 1 0 1
z=x&y ==> 1 0 1
print(x,y,z)
x=5 ==> 1 0 1
y=6 ==> 1 1 0
z=x&y ==> 1 0 0
print(x,y,z) ==> 5 6 4
1 0 1
0 1 1
1 1 1
0 0 0
x=5 ==> 1 0 1
y=4 ==> 1 0 0
z=x|y ==> 1 0 1
print(x,y,z) ==> 5 4 5
Relational Operators
=================
==> Relational operators are used to find relation between two operands.
==> Relational operators are binary operators, which required two opeands.
a=10
b=5
c=a>b
d=a<b
print(a,b,c,d)
e=b<a
print(e)
x=100
y=100
z=x>y
print(x,y,z)
p=x>=y
print(p)
================================================
a=100
b=200
c=100
d=a==b
e=a==c
print(a,b,c,d,e)
=================================================
Logical operator
==> Logical operators are used to combine two conditions or boolean expressions.
==> Logical operators are represented using three keywords.
1. and
2. or
3. not
eg:
a=True or False
b=False or True
c=True or True
d=False or False
print(a,b,c,d)
a=10>20 or 20>10
print(a)
b=20>10 or 10>20
print(b)
not operator
==========
truth table of not operator
x=not True
y=not False
exists=True
z=not exists
exists=False
p=not exists
print(p)
lock=True
l1=not lock
lock=False
l2=not lock
print(l1,l2)
===========================================================
Bitwise operator
==============
shift operators
leftshift operator is used to add bits at right side by shift bits towards leftside. this
allows to increment value by adding bits.
right shift operator is used to remove bits at right side by shifting bits towards
rightside. this allows to decrement value by removing bits.
x=12
y=x>>1
print(x,y)
z=x<<1
print(x,z)
b1=0b1010
b2=b1<<2
print(b1,b2)
print(bin(b1),bin(b2))
b3=0b1010
b4=b3>>1
print(b3,b4)
print(bin(b3),bin(b4))
num=5
x=num>>2
bitwise & (AND)
bitwise | (OR)
bitwise ^ (XOR)
bitwise ~ (complement)
truth table
1 0 0
0 1 0
1 1 1
0 0 0
x=5 ==> 1 0 1
y=5 ==> 1 0 1
z=x&y ==> 1 0 1
print(x,y,z)
x=5 ==> 1 0 1
y=6 ==> 1 1 0
z=x&y ==> 1 0 0
print(x,y,z) ==> 5 6 4
1 0 1
0 1 1
1 1 1
0 0 0
x=5 ==> 1 0 1
y=4 ==> 1 0 0
z=x|y ==> 1 0 1
print(x,y,z) ==> 5 4 5
0 0 0
0 1 1
1 0 1
1 1 0
x=5 ==> 1 0 1
y=4 ==> 1 0 0
z=x^y ==> 0 0 1
print(x,y,z) ==> 5 4 1
operator
+=
-=
*=
/=
//=
%=
**=
>>=
<<=
&=
|=
^=
x=100
x=x+1 ==> x+=1 ==> 101
y=200
y=y+20 ==> 220
y+=20 ==> y=y+20 ==> 220
a,b,c,d,e,f,g,h=10,20,30,40,50,60,70,80
print(a,b,c,d,e,f,g,h)
a+=5
b-=5
c*=5
d/=5 # float division
e//=5 # floor division
f%=5
g**=5
h>>=2
print(a,b,c,d,e,f,g,h)
output:
10 20 30 40 50 60 70 80
15 15 150 8.0 10 0 1680700000 20
identity operators
===============
list1=[10,20,30,40]
list2=[10,20,30,40]
t=list1==list2
print(t)
print(id(list1))
print(id(list2))
t1=list1 is list2
print(t1)
list3=list1
t2=list1 is list3
print(t2)
t3=list1 is not list3
print(t3)
t4=list1 is not list2
print(t4)
list1=['java','python','.net','c','oracle']
t1="python" in list1
t2="aws" in list1
t3="oracle" in list1
print(t1,t2,t3)
t4="python" not in list1
t5="aws" not in list1
print(t4,t5)
Operator precedence
==================
Operator Description
(expressions...),
[expressions...], {key: value...}, {expressions...}
syntax:
x=5
y=2
z=x if x>y else y
7th September
# write a program to find given number is even or odd
name=input("Enter name")
age=int(input("Enter age"))
print(name,"is elg") if age>=18 else print(name,"is not elg")
x=10
y=20
z=x+y
print(x,y,z)
z=(x:=100)+y
print(z)
print(x)
===========================================================
=
a=100
b=50
c=200
d=100
'''e=a+b
f=c-d
g=e-f '''
g=(e:=a+b)-(f:=c-d)
print(e,f,g)
=====================================================
x=10+5*5-2 # 10+25-2 ==> 35-2 ==> 33
y=5-2+3-1 # 3+3-1 ==> 6 -1 ==> 5
z=10+(a:=5+2)
print(x,y,z,a)
x=0b1010
b1=x>>2+5 # x>>7 # 0b1010>7 # 0
print(b1)
b2=x<<5-3 # x<<2 # 0b1010<<2 # 0b101000
print(b2)
n1=100
n2=50
n3=60
b1=n1>n2 and n1>n3 or n2>n3 and n2>n1
# True and True
# True or n2>n3 and n2>n1
# True or False # True
print(b1)
==========================================================
string formatting
==============
"string %s "%(variable1,variable2,variable3,..)
===========================================================
======
# find simple intrest
p=float(input("Enter amount"))
r=float(input("Enter rate"))
t=int(input("Enter time"))
si=(p*t*r)/100
print("Amount=%f,Rate=%f,Time=%d,SimpleIntrest=%f"%(p,r,t,si))
print("Amount=%.2f,Rate=%.2f,Time=%d,SimpleIntrest=%.2f"%(p,r,t,si))
===========================================================
========
print("%d%d%d%d"%(100,200,300,400))
print("name is %s age is %d"%("naresh",50))
8th September
string format method
=================
str.format()
"{},{}".format(100,200)
===========================================================
==
a=100
b=200
print("{},{},{}".format(a,b,a+b))
print("{0},{1},{2}".format(a,b,a-b))
===========================================================
==========
# write a program to find area of circle
r=float(input("Enter r"))
area=3.147*r*r
print("area of cirlce is {} with r value {}".format(area,r))
===========================================================
=========
rollno=int(input("Enter rollno"))
name=input("Enter name")
sub1=int(input("Enter subject1"))
sub2=int(input("Enter subject2"))
sub3=int(input("Enter subject3"))
print("Rollno {}".format(rollno))
print("Name {}".format(name))
print("Subject1 {0} : Subject2 {1} : Subject3 {2}".format(sub1,sub2,sub3))
print("Total {t} Avg {a}".format(t=sub1+sub2+sub3,a=(sub1+sub2+sub3)/2))
print("Result is {r}".format(r="Pass" if sub1>40 and sub2>40 and sub3>40 else
"Fail"))
===========================================================
==========
formatting characters
==================
d ==> decimal integer
o ==> octal integer
x ==> hexa decimal integer
f ==> float in fixed
e ==> float in expo
b ==> binary integer
{1 : 2 }
=========================================================
a=65
print("{:d},{:o},{:x},{:b}".format(a,a,a,a))
print("{0:d},{1:o},{2:x},{3:b}".format(a,a,a,a))
print("{p:d},{q:o},{r:x},{s:b}".format(p=a,q=a,r=a,s=a))
==========================================================
a=10
b=100
c=1000
d=10000
e=100000
print(a)
print(b)
print(c)
print(d)
print(e)
print("{:8d}".format(a))
print("{:8d}".format(b))
print("{:8d}".format(c))
print("{:8d}".format(d))
print("{:8d}".format(e))
===========================================================
a=1.56
b=23.434
c=3.567
d=.78
print(a)
print(b)
print(c)
print(d)
print("{:8.2f}".format(a))
print("{:8.2f}".format(b))
print("{:8.2f}".format(c))
print("{:8.2f}".format(d))
===========================================================
==
Alignment characters
a,b,c=100,200,300
p,q,r=10,20,30
m,n,o=1,2,3
print(a,b,c)
print(p,q,r)
print(m,n,o)
print("{:5d}{:5d}{:5d}".format(a,b,c))
print("{:5d}{:5d}{:5d}".format(p,q,r))
print("{:5d}{:5d}{:5d}".format(m,n,o))
print("{:5d}{:5d}{:5d}".format(a,b,c))
print("{:5d}{:5d}{:5d}".format(p,q,r))
print("{:5d}{:5d}{:5d}".format(m,n,o))
9th September
f-string or string interpolation
========================
amt=float(input("Enter amount"))
t=int(input("Enter time"))
rate=float(input("Enter rate"))
si=amt*t*rate/100
print(f'Amt={amt},Time={t},Rate={rate},SimpleIntrest={si}')
output:
Enter amount5000
Enter time12
Enter rate2.5
Amt=5000.0,Time=12,Rate=2.5,SimpleIntrest=1500.0
===========================================================
========
n1=50
n2=7
print(f'sum of {n1} and {n2} is {n1+n2}')
print(f'diff of {n1} and {n2} is {n1-n2}')
print(f'prod of {n1} and {n2} is {n1*n2}')
output:
sum of 50 and 7 is 57
diff of 50 and 7 is 43
prod of 50 and 7 is 350
===========================================================
==========
n1=65
print(f'{n1:d},{n1:o},{n1:x},{n1:b}')
n2=4.5678
print(f'{n2:f},{n2:e}')
print(f'{n2:.2f}')
===========================================================
==========
a=100
b=1000
c=10000
d=100000
print(f'{a:8d}\n{b:8d}\n{c:8d}\n{d:8d}')
===========================================================
==========
# temperature conversion
Control Statements
================
==> Control statements are used to control the flow of execution of program.
==> By default the program is executed in sequential order.
==> Inorder to change the flow of execution of program we use control statements.
1. Condtional Statements
2. Looping Statements or Iterational Statement
if statement
==========
Types of if statements
==================
1. simple if
2. if..else
3. if..elif
4. nested if
simple if
=======
syntax:
if <test/condition>:
statement-1
statement-2
statement-3
==> if test is True, it execute statement-1 and statement-2 and continue with
statement-3.
==> if test is False,it exeute statement-3.
==> we cannot define empty block. a block required at least one statement.
pass
====
10thSeptember
f-string or string interpolation
========================
amt=float(input("Enter amount"))
t=int(input("Enter time"))
rate=float(input("Enter rate"))
si=amt*t*rate/100
print(f'Amt={amt},Time={t},Rate={rate},SimpleIntrest={si}')
output:
Enter amount5000
Enter time12
Enter rate2.5
Amt=5000.0,Time=12,Rate=2.5,SimpleIntrest=1500.0
===========================================================
========
n1=50
n2=7
print(f'sum of {n1} and {n2} is {n1+n2}')
print(f'diff of {n1} and {n2} is {n1-n2}')
print(f'prod of {n1} and {n2} is {n1*n2}')
output:
sum of 50 and 7 is 57
diff of 50 and 7 is 43
prod of 50 and 7 is 350
===========================================================
==========
n1=65
print(f'{n1:d},{n1:o},{n1:x},{n1:b}')
n2=4.5678
print(f'{n2:f},{n2:e}')
print(f'{n2:.2f}')
===========================================================
==========
a=100
b=1000
c=10000
d=100000
print(f'{a:8d}\n{b:8d}\n{c:8d}\n{d:8d}')
===========================================================
==========
# temperature conversion
Control Statements
================
==> Control statements are used to control the flow of execution of program.
==> By default the program is executed in sequential order.
==> Inorder to change the flow of execution of program we use control statements.
1. Condtional Statements
2. Looping Statements or Iterational Statement
if statement
==========
Types of if statements
==================
1. simple if
2. if..else
3. if..elif
4. nested if
simple if
=======
syntax:
if <test/condition>:
statement-1
statement-2
statement-3
==> if test is True, it execute statement-1 and statement-2 and continue with
statement-3.
==> if test is False,it exeute statement-3.
Q: What is indent in python?
==> we cannot define empty block. a block required at least one statement.
pass
====
11thSeptember
if..else
=====
Syntax:
if <condition>:
statement-1
statement-2
else:
statement-3
statement-4
statement-5
if num%2==0:
print(f'{num} is even')
else:
print(f'{num} is odd')
# find output
if True:
print("Hello")
else:
print("Bye")
The above script/code display syntax error because else: block is placed inside if, it
should use the same indent of if. The correct code is,
if True:
print(“Hello”)
else:
print(“Bye”)
# write a program read name and age person and find elg to vote or not
name=input("Enter name")
age=int(input("Enter age"))
if age>=18:
else:
user=input("Enter UserName")
pwd=input("Enter Password")
print(f'{user},welcome to my application')
else:
print(f'{ch} is vowel')
else:
if ch in "aeiouAEIOU":
print(f'{ch} is vowel')
else:
if num1>num2:
print(f'{num1} is max')
else:
print(f'{num2} is max')
x,y=a.split(",")
print(a)
print(x)
print(y)
print(type(a))
print(type(x))
print(type(y))
x,y,z=a.split(" ")
sum=int(x)+int(y)+int(z)
s=int(x)+int(y)+int(z)
print(f'sum of {x},{y},{z} is {s}')
print(f'Rollno {rno}')
print(f'Name {name}')
else: