print("hello wold"); #(#) it's use to comment print("hello\nwold")
'''this is devendra'''
''' (5) its called integer data type ("john")its called string data type (2.3)its called float data type (none)its called non data type (true/false)its bulien integer data type'''
name = "ram";
print(name);
name="soniya";
print(name);
rollnombber= 112;
parcentage= 77;
student =True;
print(name,rollnombber,student); print("my name is",name,"my rool numbber is ",rollnombber,"parcentage is ",parcentage,"i am a student",student); #this type we use to change mathimaticaly ; print("my new parcentage",parcentage-1); #(sep="") we use to creat sepretter; print(name,rollnombber,parcentage,student,sep="-");
#chr()function is give you numeric value change in ascii value; chr = "a"; print(ord(chr)); ascii = 68; #print(chr(ascii)); it's part of code but codn;t work;
NAME=input("enter your name");
print(type(NAME));
'''ROLLNUMBER= (input("enter your roll number")); print(type(ROLLNUMBER + 1));'''
#in next line "int" is a type casting ROLLNUMBER= int(input("inter your roll number")); print(type(ROLLNUMBER + 1));
number1= 12; number2= 14;
print(number1+number2);
print(number1-number2);
print(number1*number2);
print(number1/number2);
print( number1** number2);
print("flow division",number1//number2);
print("rimender",number1%number2);
n1=5; n2=5; print(n1+n2);
n2+=n1;
n2*=n1;
n2-=n1;
n2/=n1; print(n1,n2);
#comparision operator #comparision operator give you bulion value (true\false);
print(n1==n2);
print(n1!=n2);
print(n1>n2);
print(n1<n2);
print(n1>=n2);
print(n1<=n2);
#logical operator
expration1=2>1; expration2=5<4;
print(expration1 and expration2);
print(expration1 or expration2);
print( not expration1);
x=5; y=5;
print(x is y); #variable have diffrent object( is not); print(x is not y);
#membership operator fruts=["banana","apple", "mongo","greps"];
print("banana"in fruts);
print("orenge" not in fruts);
a=5; b=3;
print(a&b); #or(|); print(a|b);
print(a^b);
#print(a~b); #left sift( <<);
chapter2
#control statements: they allow us to control the flow of program;
number= float(input(" enter a number")); if number>0: print(" number is possitive");
elif number==0: print("number is zero") else: print(" number is negitive");
n1=float(input("enter first number")); n2=float(input("enterr secound number")); n3=float(input("enter therd number"));
if n1>n2 and n1>=n3: print(n1,"is gretest intiger number"); elif n2>n1 and n3>=n3 : print(n2,"is gretest intiger number"); else : print(n3,"is gretest intiger number");
if n1>n2: #if n1 is gretest intiger number if n1>n3: print(n1,"is gretest intiger number"); else: print(n3,"is gretest intiger number"); else: print(n2," is gretest intiger number");
#match case
num1=int(input("enter first number")); num2=int(input("enter number 2"));
operator=input("enter opreter");
match operator: case "+": print("sum of number=",num1+num2); case "-": print("subtrection of number=",num1-num2) ; case "": print("prodect of number=",num1num2); case "/": print("division of number=",num1/num2); case _ : print("enter velid operator");
a=int(input("enter a number for cheack even or odd"));
output= "even" if a/2==0 else "odd"; print( 'output is',output);
in range first is starting and secound is stoping and third is term of incrige's i value by 1,3,5,2 etc
for i in range(1,11,3): print(i,"hello world"); #and use like this fruts=[ "bannana","mongo", "apple"]; for i in fruts: print(i);
i = 1 while i < 100: print(i) i += 1
n=int(input("enter a row number")) for i in range(1, n+1, 1): print(5 * "*")
for _ in range(1, 5, 1): print("1234")
#print 123456 in 6 row and 6 colam for _ in range(0,7,1): print("123456")
n=int(input("enter number for drow petern")) for i in range(n): # number of row for j in range(1,n+1):# number of colum print(j , end="") print()
n=int(input("enter number for drow petern")) for i in range(1,n+1):# loop for row for j in range(1,i+1): # loop for colum print(j , end="") print()
#print trengal petern
n = int(input("enter number of row number"))
for i in range(1,n+1): # loop for rows print(" " *(n-i), end="")
for j in range(1,2*i-1+1):
print(j , end="")
print()
chapter 3
fruts=[ "apple","banana", "mongo"] # creat the the list print(fruts) # who to print a list print(type(fruts)) # check type of list print(len(fruts)) # check the length of the list
if "banana" in fruts: print(" banana in the list")
if "chery" not in fruts: print(" chery is not in list")
print(fruts[2]) # mongo print(fruts[-3]) # apple print(fruts[0:2]) # apple mongo
fruts.append("chery") print(fruts)
fruts.insert( 2,"greps") print(fruts)
frutss=[ " jaja"] fruts.extend(frutss) print(fruts)
fruts.remove("banana") print(fruts) fruts.pop(2) print(fruts)
fruts[1] = "banana" print(fruts) fruts[1:3] = [ "greps"] print(fruts)
fruts.sort() # this is by difolt acending way print( fruts) fruts.sort(reverse=True) print(fruts)
new_list= [ fruts for fruts in fruts if "a" in fruts] print(new_list)
new__fruts=fruts.copy() print(new__fruts)
new__fruts=new_list+fruts print(new__fruts)
fruts.insert(2,[ "civi,poteto"]) print(fruts)
n = (int (input( "enter a number for number of list aliment")))
list=[] for _ in range(n): num=int(input()) list.append(num)
in1=int(input(" enter index")) in2=int(input(" enter index")) print(list)
temt=list[in1] list[in1] = list[in2] list[in2] = temt
print(list)
color=( "red", "blue", "green")
fruits=("apple",)
print(type(color))
print(len(color))
print(color[0])# this is positive indexing print(color[-1])# this is negitive indexing print(color[0:2])# this is range indexing print(color[-1:-3])# this is negitive indexing
if "green" in color : print("green is part of tuple")
for _ in color: print(_)
new_color=("oreng","pink") color=color+new_color print(color)
color1,color2,color3,color4,color5=color print(color1,color2,color3,color4,color5)
input_tuple=(1,2,3,4,)
list=[] for _ in reversed(input_tuple): list.append(_)
input_tuple=tuple(list) print(input_tuple)
name={ "siya","riya","roshana"} print(name) print(type(name))# check type of name print(len(name))# check length of set
for x in name: print(x)
if "siya" in name : print("siya is part of set")
name.add("shita") print(name)
name_list={ "rohan","mohit"} name.update(name_list) print(name)
name.remove("riya") print(name)
name.discard("ram") print(name)
s1={ 1,2,3,4} s2={ 4,6,7,8} print(s1,s2)
s3=s1.union(s2) print(s3)
s1.update(s2) print(s1)
s1.intersection_update(s2) print(s1)
s1.symmetric_difference_update(s2) print(s1)
#example
ar1={1,5,12,20,40,80} ar2={6,7,20,80,100} ar3={3,4,15,20,30,70,80,120}
ar1.intersection_update(ar2)
set=ar1.intersection_update(ar3) print(set)
phone={ "reya": 124578, "ram": 123654, "sona":32654 } print(phone) print(type(phone))# check a type of phone print(len(phone))# check a length of dictionary
#accesing of item print(phone["ram"]) print(phone.get("ram"))
print(phone.keys())
phone["ram"]=12546 print(phone)
phone["jhon"]=789456 print(phone)
new_phone={ "roma":456321 } phone.update(new_phone) print(phone)
phone.pop("roma") print(phone)
phone.popitem()# this will remove last add item print(phone)
#phone.clear()# this will empty over dict #print(phone)
for x in phone: print(x) '''for n in phone: print(phone(n))'''
for x in phone.items():# x change x,y then x is key and y is value print(x)
phones={ "area1":{ "x":1, "y":5, "z":4 }, "area2":{ "a":8, "b":7, "c":6 } } print(phones["area1"]["x"])
#example dict={ "a":100, "b":200, "c":300 } print("sum of the number is",sum(dict.values()))
input_string = input("Enter a string: ") a = int(input("Enter a number for start mirror operation: "))
alphabets = "abcdefghijklmnopqrstuvwxyz" reversed_alphabets = alphabets[::-1] dict1 = dict(zip(alphabets, reversed_alphabets))
prefix = input_string[0:a-1] suffix = input_string[a-1:]
string = "" for char in suffix:#this is another way "for i in range[0,len(sfix)]:string=string+dict1[sfix(i)]" string += dict1[char] if char in dict1 else char
result = prefix + string print("The result is:", result)
chapter 4
what And why we run same program agen and agen to solve same problem there for we use function to save your time
def printhello(): # which was a body of function print("hell wold")
printhello()
def adding(n1=0,n2=0): sum = n1 + n2 return sum
x=3 y=4 print( " the sum is",adding(x,y))
print( "this is sum of ", adding(5,4))
print( "this is sum of ", adding(n1=5,n2=4))
print( "this is sum of ", adding(5,))
def addallnumber(*arg): sum = 0 for i in arg: sum+=i return sum
output=addallnumber(1,2,3,4,4) print(" addallnumber",output)
#keyword argument def studentinput(**lenvariable): for x,y in lenvariable.items(): print(x,y)
studentinput(name="ram",age=22,city="jaipur")
def ram (n1,): y=n1 def jai (n2): for i in jai: y+=i return sum
def addone(x): x = x+1 print("pass by value x:",x)# its could not change origlan value
x=5 addone(x) print("og value x",x) #pass by reference def modifinglist(list): list.append(4) print("inside function",list)# effect the orignal value
list=[ 1,2,3] modifinglist(list) print( "og value", list)
def factorial(n): ans = 1 if n == 0: ans = 1 else: for i in range(1, n + 1): # Use parentheses for range, not square brackets ans *= i
return ans
n = int(input("Enter a number to calculate its factorial: "))
output = factorial(n) print("The factorial is", output)
chapter 5
def fectorial(n):
#base case
if n==0:
return 1
# recursive case
ans =n * fectorial(n-1)
return ans
n=int(input("enter a number")) print(fectorial(n))
def num(n): #base case if n==0: return
print(n)
num(n-1)
print(num(5))
def sum(n):
# base case
if n == 1:
return 1
# recesive
ans = n+sum(n-1)
return ans
n = int(input("enter a number")) print(sum(n))
def power(a,b):
# base case
if b ==0:
return 1
#recursive case
ans = a*power(a,b-1)
return ans
a=int(input("enter value of a")) b=int(input("enter value of b"))
print("power of a,b:",power(a,b))
def fibunaki(n):
if n == 1: # base case
return 0
elif n==2: # base case
return 1
else : # recursive case
return fibunaki(n-1)+fibunaki(n-2)
n= int(input("enter a num")) for i in range(1,n+1): # if you did not write this row you take thet only value on number print(" fibunaki siriz",fibunaki(i))
chapter 6
name = 'dev' name1 = "ram" name2 = '''shohan'''
print( name,name1,name2) print(type( name)) print(type( name1)) print(type( name2))
peregraph = ''' dfjckd cdkcncjjsndds'''
print(name[1]) print(name1[1])
for i in name : print(i)
list=[ name for name in name] print(list)
print(len(name))
print(name.find("e")) # this retune index value
#use get a part of string print(name[:2]) print(name1[-1:-3]) # negitive idexing
str= name.upper() print(str)
#change uper - lower case str= name.lower() print(str)
str = name.capitalize() print(str)
str = ' sa ' print(str.strip()) print(str)
str="jai ram jai jai ram bolo ram ram shita ram"
ans=str.replace( "ram ","krishna",1)# if i am not give a time then chage the all substring
print(ans)
str1= "riya shiya miya chiya" list=str.split(" ",) # if i give a number then only thet time split it print(list)
str2= " hello world" str3= " what a great place this is"
print(str2+str3)
#format() student_name="pallawi" student_mark= 98
str="student name is {f1} and mark is {f2}".format(f1=student_name,f2=student_mark) print(str)
chapter 7
#class from typing import Any
class student:
def set_name(self,name):
self.name = name # class atributs
def get_name(self):
return self.name
student1 = student() student1.set_name("ram") print(student1.name) print(student1.get_name())
student2 = student() student2.set_name("kanha") print(student2.name)
class Rectangle: def set_dimension(self, height, width): self.height = height self.width = width
def area(self):
return self.height * self.width
def perimeter(self):
return 2 * (self.height + self.width)
rectangle1 = Rectangle() rectangle1.set_dimension(4, 3)
print("Rectangle height and width:", rectangle1.height, rectangle1.width) print("This is the area:", rectangle1.area()) print("This is the perimeter:", rectangle1.perimeter())
#class constuctor class Rectangle: def init(self, height, width): self.height = height self.width = width
def area(self):
return self.height * self.width
def perimeter(self):
return 2 * (self.height + self.width)
rectangle1 = Rectangle(4, 3)
print("Rectangle height and width:", rectangle1.height, rectangle1.width) print("This is the area:", rectangle1.area()) print("This is the perimeter:", rectangle1.perimeter())
#privet
#secur
class perent: def init(self) : self.super_atribut = True print("this is perent class")
class child(perent): def init(self): super().init() print("this is cild class") print(self.super_atribut)
child_object=child()
#example class Vihcal:
def __init__(self, sitting_capacity):
self.sitting_capacity = sitting_capacity
def get_fear(self):
return self.sitting_capacity * 100
class Bus(Vihcal):
def __init__(self, sitting_capacity):
super().__init__(sitting_capacity)
def get_fear(self):
the_vahcal_fear = super().get_fear()
maintenance = 0.1 * the_vahcal_fear
total = maintenance + the_vahcal_fear
return total
vahcal1 = Vihcal(15) print("This is a Vihcal fear:", vahcal1.get_fear())
bus1 = Bus(15) print("Bus fear is:", bus1.get_fear())
#polymorphism
#comple time polymorphism
#run time polymorphism
#encapsulation
#try #except #finally
If I ==10 Break(loop ko off ) \continue(value ko jump )
Doc is a like comment but it’s not ignore buy reader and it’s write after the function name ether it’s not work it’s write like this( def name() ‘’’this is doc’’’ Print(name.doc) Pep 8 is help to python learning
The zin of python