print('hello word')hello word
student = 'sam'student'sam'
student = 'matt'student'matt'
a1 = 10
a110
type(a1)int
a1 = 3.14
a13.14
type(a1)float
a1 = True
a1True
type(a1)bool
a1 = 'hello word'
a1'hello word'
type(a1)str
a1 = 3+4j
a1(3+4j)
type(a1)complex
# +,-,*,/a=20
b=30a,b(20, 30)
a+b50
a-b-10
b-a10
a*b600
a/b0.6666666666666666
#Relational operators
# <,>,==,!=a=50
b=70a<bTrue
a>bFalse
a==bFalse
a!=bTrue
# &,|a=True
b=Falsea & bFalse
a & aTrue
b & aFalse
b & bFalse
a | bTrue
a | aTrue
b | aTrue
b | bFalse
#keywods
#True, class, if,yield, while, false, finally, is, return, try, none, continue, for, lambda, try, def, from, nonlocal, while, and, del, global, not, with, as, eelif, if, orStudent = 'sartha'student ='dinesh'Student'sartha'
student'dinesh'
#literals - Do not change
a ='hello'a'hello'
#stringsstr1 ="this is my first string"str1'this is my first string'
str2 ="this is my second string"str2'this is my second string'
str3 = '''
this
is
my
third
string
'''str3'\nthis\nis \nmy \nthird\nstring\n\n'
my_string ="My name OM"my_string'My name OM'
my_string[0]'M'
my_string[-2]'O'
my_string[4:10]'ame OM'
len(my_string)10
#string function
my_string.lower()'my name om'
my_string.upper()'MY NAME OM'
my_string.replace('M','P')'Py name OP'
my_string.count('name')1
ste_new ='sparta sparta sparta 2 2 2 22 2 2 2 2 2 22'ste_new'sparta sparta sparta 2 2 2 22 2 2 2 2 2 22'
ste_new.count('2')12
s1 ='this iss om's1'this iss om'
s1.find('om')9
fruit ='I like banna,apple,mango'
fruit'I like banna,apple,mango'
fruit.split(',')['I like banna', 'apple', 'mango']
# 1Tuple 2.list 3.Dictionary 4.Settup1 =(1,True,'OM',2.34,2+4j)tup1(1, True, 'OM', 2.34, (2+4j))
tup1[0]1
tup1[2]'OM'
tup1[-1](2+4j)
tup1[0:4](1, True, 'OM', 2.34)
type(tup1)tuple
tup1[2]='hello'---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_5212/764570895.py in <module>
----> 1 tup1[2]='hello'
TypeError: 'tuple' object does not support item assignment
len(tup1)5
tup2=(1,False,2.3,'hello',2+8j)tup2(1, False, 2.3, 'hello', (2+8j))
tup1(1, True, 'OM', 2.34, (2+4j))
tup1+tup2(1, True, 'OM', 2.34, (2+4j), 1, False, 2.3, 'hello', (2+8j))
tup2+tup1(1, False, 2.3, 'hello', (2+8j), 1, True, 'OM', 2.34, (2+4j))
tup1,tup2((1, True, 'OM', 2.34, (2+4j)), (1, False, 2.3, 'hello', (2+8j)))
tup3=(1,2,3)tup3*8(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)
tup1*3+tup2*1(1,
True,
'OM',
2.34,
(2+4j),
1,
True,
'OM',
2.34,
(2+4j),
1,
True,
'OM',
2.34,
(2+4j),
1,
False,
2.3,
'hello',
(2+8j))
min(tup3)1
max(tup3)3
l1=[1,'a',False]l1[1, 'a', False]
type(l1)list
a1=[1]type(a1)list
l1[0]1
l1[-1]False
l1[0:3][1, 'a', False]
l1[1, 21, False]
l2 = [1,'a',2,3,'OM']l2
l2[1, 'a', 2, 3, 'OM']
l2[0]=100l2[100, 'a', 2, 3, 'OM']
l2.pop()'OM'
l2[100, 'a', 2, 3]
l2.append('this is strata')l2[100, 'a', 2, 3, 'this is strata']
l2.reverse()l2['this is strata', 3, 2, 'a', 100]
l2.insert(2,2+4j)l2['this is strata', 3, (2+4j), 2, 'a', 100]
l3=['mango','banna','apple','grapes']
l3['mango', 'banna', 'apple', 'grapes']
l3.sort()l3['apple', 'banna', 'grapes', 'mango']
l3*3+l2*5['apple',
'banna',
'grapes',
'mango',
'apple',
'banna',
'grapes',
'mango',
'apple',
'banna',
'grapes',
'mango',
'this is strata',
3,
(2+4j),
2,
'a',
100,
'this is strata',
3,
(2+4j),
2,
'a',
100,
'this is strata',
3,
(2+4j),
2,
'a',
100,
'this is strata',
3,
(2+4j),
2,
'a',
100,
'this is strata',
3,
(2+4j),
2,
'a',
100]
l1+l2+l3['this is strata',
3,
(2+4j),
2,
'a',
100,
'apple',
'banna',
'grapes',
'mango']
l1,l2,l3([],
['this is strata', 3, (2+4j), 2, 'a', 100],
['apple', 'banna', 'grapes', 'mango'])
d1={'applee':50,'mango':100,'banna':40}d1{'applee': 50, 'mango': 100, 'banna': 40}
type(d1)dict
d1.keys()dict_keys(['applee', 'mango', 'banna'])
d1.values()dict_values([50, 100, 40])
d1['grapes']=50
d1{'applee': 50, 'mango': 100, 'banna': 40, 'grapes': 50}
d1['applee']=200d1{'applee': 200, 'mango': 100, 'banna': 40, 'grapes': 50, 'apple': 200}
d2={'guava':80,'orange':70}d2{'guava': 80, 'orange': 70}
d1.update(d2)d1{'applee': 200,
'mango': 100,
'banna': 40,
'grapes': 50,
'apple': 200,
'guava': 80,
'orange': 70}
d2.update(d1)d2{'guava': 80,
'orange': 70,
'applee': 200,
'mango': 100,
'banna': 40,
'grapes': 50,
'apple': 200}
d2.pop('banna')40
d2{'guava': 80,
'orange': 70,
'applee': 200,
'mango': 100,
'grapes': 50,
'apple': 200}
a=10
b=20if b>a:
print('b is greateer then a')b is greateer then a
if a>b:
print('b is greater theb a')if a>b:
print('a is greater then b')
else:
print('b is greater then a')b is greater then a
a=10
b=20
c=30if (a>c) & (a>b):
print('a is the greatest')
elif (b>a) &(b>c):
print('b is greatest')
else:
print('c is greatest')
c is greatest
#if with tupletup1=('a','b','c')if 'a' in tup1:
print('value a is present in tup1')value a is present in tup1
if 'd' in tup1:
print('value d is present in tup1')#if with listl1=[1,2,3]
l1[1, 2, 3]
if l1[2]==3:
l1[2]=4
l1[1, 2, 4]
#if with dictionaaryd1={'k1':20,'k2':40,'k3':50}d1{'k1': 20, 'k2': 40, 'k3': 50}
if d1['k3']==50:
d1['k3']=100d1{'k1': 20, 'k2': 40, 'k3': 100}
i=1
while i<=10:
print(i)
i=i+1
1
2
3
4
5
6
7
8
9
10
i=100
while i<=120:
print(i)
i=i+3100
103
106
109
112
115
118
i=1
n=2
while i<=10:
print(n ," * ",i," = ", n*i)
i=i+1
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
#while with listl1=[1,2,3,4]i=0while i<len(l1):
l1[i]=l1[i]+100
i=i+1l1[101, 102, 103, 104]
l1 = ['mango','graps','oranga','banana']for i in l1:
print(i)mango
graps
oranga
banana
#multiple for loopl1=['book','laptop','chair','table']
l2=['black','orange','white']for i in l1:
for j in l2:
print(i,j)book black
book orange
book white
laptop black
laptop orange
laptop white
chair black
chair orange
chair white
table black
table orange
table white
def hello():
print('hello word')hello()hello word
def add_10(x):
return x+10add_10(16)26
add_10(10)20
def even_odd(x):
if x%2==0:
print(x, 'is even')
else:
print(x,'is oddd')even_odd(10)10 is even
even_odd(5)5 is oddd
g=lambda x: x*x*xg(7)343
g(10)1000
#lambda with filterl1 = [1,23,45,67,98,56]
final_list=list(filter(lambda x:(x%2!=0), l1))final_list[1, 23, 45, 67]
#lambda with mapl1 =[1,2,3,4,5,6,7,8]list_final=list(map(lambda x:x*2, l1))list_final[2, 4, 6, 8, 10, 12, 14, 16]
from funktools import reducel1=[1,2,3,4,5,6,7,8]sum=reduce(lambda x,y: x+y,l1)---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_3556/3401324226.py in <module>
----> 1 sum=reduce(lambda x,y: x+y,l1)
NameError: name 'reduce' is not defined
sum<function sum(iterable, /, start=0)>
#creating the first class class Phone:
def make_call(self):
print('making call')
def play_game(self):
print('playing game')p1 = Phone()p1.make_call()making call
p1.play_game()playing game
#adding parameterss to the class class Phone:
def set_color(self,color):
self.color=color
def set_cost(self,cost):
self.cost=cost
def show_color(self):
return self.color
def show_cost(self):
return self.cost
def make_call(self):
print('making call')
def play_game(self):
print('playing game')p2 = Phone()p2.set_color('black')p2.set_cost(10000)p2.show_color()'black'
p2.show_cost()10000
p2.make_call()making call
p2.play_game()playing game
class Employee:
def __init__(self,name,age,salary,gender):
self.name = name
self.age = age
self.salary = salary
self.gender = gender
def show_employee_details(self):
print('name of employee is',self.name)
print('age of employee is',self.age)
print('salary of employee is',self.salary)
print('gender of employee is',self.gender)e1 = Employee('Om',18,50000,'male')e1.show_employee_details()name of employee is Om
age of employee is 18
salary of employee is 50000
gender of employee is male
class vehicle:
def __init__(self,mileage,cost):
self.mileage = mileage
self.cost = cost
def show_vehicle_details(self):
print('mileage of vihicle is',self.mileage)
print('cost of vehicle is',self.cost)c1 = vehicle(50,5000000)c1.show_vehicle_details()mileage of vihicle is 50
cost of vehicle is 5000000
class car(vehicle):
def show_car_details(seif):
print('I am a car')c1 = car(200,1200000)c1.show_vehicle_details()mileage of vihicle is 200
cost of vehicle is 1200000
c1.show_car_details()I am a car
class car(vehicle):
def __init__(self,mileage,cost,tyers,hp):
self.mileage = mileage
self.cost = cost
self.tyers = tyers
self.hp = hp
def show_car_details(self):
print('tyers of vihicle is',self.tyers)
print('hp of vehicle is',self.hp)
print('i am a car')c1 = car(300,2000000,4,400)c1.show_car_details()tyers of vihicle is 4
hp of vehicle is 400
i am a car
class perent1:
def assign_string_one(self,str1):
self.ster1 = str1
def assign_string_one(self):
return self.str1class perent2:
def assign_string_two(self,str2):
self.str2 = str2
def assign_string_two(self):
return self.str2class child(perent1,perent2):
def assign_string_three(self,str3):
self.str3 = str3
def assign_string_three(self):
self.str3my_child = child()
#some work is dueclass parent:
def get_name(self,name):
self.name = name
def show_name(self):
return self.nameclass child(parent):
def get_age(self,age):
self.age = age
def show_age(self):
return self.ageclass grandchild(child):
def get_gender(self,gender):
self.gender = gender
def get_gender(self):
return self.gender
gc = grandchild()gc.get_name('om')gc.get_age(18)gc.show_name()'om'
gc.show_age()18