Professional Documents
Culture Documents
2.wap to divide a number by 5 and print "highfive" ifthe value is equal to zero
else:
print("its a prime number")
4.Wap to generate two random single digit numbers.if num1>num2, then swap the
numbers and subtract both.
import random
a=random.randint(0,9)
b=random.randint(0,9)
print(a)
print(b)
if(a>b):
a,b=b,a
print("after swapping")
print(a)
print(b)
c=a-b
print("difference:",c)
*......................................................................* 09:49
10-08-2022
if score>=60.0
grade='D'
elif score<60.0
grade='C'
ans: indendation
if score>=60.0
grade='D'
elif score<60.0
grade='C'
i=1
j=2
k=3
if i>j:
if i>k:
print('A')
else:
print('B')
ans: indendation
i=1
j=2
k=3
if i>j:
if i>k:
print('A')
else:
print('B')
nunber=6
if (number%2==0):
even=true
else:
even=false
ans: indentation and print statement.
nunber=6
if (number%2==0):
even=true
print(even)
else:
even=false
print(even)
paragraph qstn!!!
9.an investor eposits 10k, annual percentage rate 6%,for 5 yrs. WAP that prints the
beginning principal and the interest earned fo each yr of the period, and total
interest and the final amount.
beg_principal=10000
roi=0.06
for x in range(5):
print("beginning principal", beg_principal)
interest=beg_principal*roi
print("interest",interest)
amount=beg_principal+interest
print("amount", amount)
final_principal=amount
beg_principal=final_principal
10.WAP to ask for two numbers. if the sum is greater than 100. print "this is a big
number" and terminate the program.
11.WAP to print all the numbers that are divisible by 7 and multiples of 5 for
numbers between 1500 and 2700
for x in range(1500,2700,1):
if(x%7==0 and x%5==0):
print(x)
12.WAP using while loop
quit="n"
while quit=="n":
quit=input("do u want to quit? y/n")
***********************************************************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************
************************************************************************
13. WAP to take a word as input from the user and print it in reverse
***********************************************************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************
************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************
************************************
14.WAP which iterates from 1 to 50. for multiples of 3 print FIZZ and for multiples
of 5 print BUZZ and for both prnt FIZZBUZZ.
for x in range(1,51):
if(x%3==0 and x%5==0):
print("FizzBuzz")
elif(x%5==0):
print("Buzz")
elif(x%3==0):
print("Fizz")
else:
print(x)
16.WAP a password guessing program to keep track of how many times the user has
entered the password wrong.if it is more than 3 times print "you have been denied
access " and terminate the program. otherwise "you have successfully logged in"
guess=0
password="abhi23"
while(True):
tryy=input("enter the password")
guess+=1
if(password==tryy):
print("you are successfully logged in")
elif(password!=tryy and guess>3):
print("access denied")
break
x=0
y=1
while y<50:
print(y)
x,y=y,x+y
range is given btw 0 and 50 hence fibonacci series is not started from 0.
series from 0:
x=0
y=1
while x<50:
print(x)
x,y=y,x+y
a="";
for row in range(0,7):
for column in range(0,7):
if(((column==1 or column==5)and row!=0)or ((row==0 or row==3) and (column>1
and column<5))):
a=a+"*"
else:
a=a+" "
a=a+"\n"
print(a);
20.WAP to print B
import math
print(abs(-2))
print(pow(3,-4))
print(min(4,5,6))
print(math.ceil(3.5))
print(math.floor(3.2))
22.FUNCTION DEFINITION
def Newline():
print("first line")
Newline()
print("second line")
23.ADD NUMBERS FROM 1-25 AND 50-75 AND 90-100 USING 3 FOR LOOPS
sum=0
for a in range(1,26):
sum+= a
print(sum)
sum2=0
for x in range(50,76):
sum2+= x
print(sum2)
sum3=0
for y in range(90,101):
sum3+= y
print(sum3)
def add(x,y):
sum=0
for i in range(x,y+1):
sum+=i
print("sum of the integers from", x,y ,"is", sum)
add(1,25)
add(50,75)
add(90,100)
p=20
def demo():
q=10
print("the value of the local variables is :" , q)
print("the value of the global variables is :" , p)
demo()
print("the value of the global variables is :" , p)
print("the value of the local variables is :" , q)
26.COUNTDOWN
def countdown(n):
if n==0:
print("blastoff")
else:
print(n)
countdown(n-1)
countdown(5)
fact=1
def factorial(n):
if n==0:
print("factorial of 0 is 1")
elif(n==1):
return(n)
else:
fact=n*factorial(n-1)
return(fact)
print(factorial(5))
OR
n=int(input("enter the number"))
def checkk(n):
if(n==1):
return "not prime"
elif(n==2):
return "prime"
else:
for x in range(2,n):
if(n%x==0):
return "not prime"
return "prime"
print(checkk(n))
def checkk(x):
string1=input("enter the value")
string2=string1[::-1]
print(string2)
if(string1==string2):
print("palindrome")
else:
print("not palindrome")
checkk(0)
32.create a list of colour names and find the colour at the fifth position
l1=["red","blue","orange","green","yellow","cyan","black"]
l1[5]
l1[2+2]
l1[6-1]
l1=[10,20,30,40,50,60]
l1.sort(reverse=True)
l1 <-------------prints list
concatanate 2 list
l1+l2 <----------------adds the list
multiplication
l5*2 <------------------repeats the elements of the list.
import random
l1=[10,20,30,40,50,60]
random.shuffle(l1)
l1
l1=[10,20,30,40,50,60]
print(sum(l1))
cloning lists
a=[1,2,3]
b=a[:]
list parameters
def head(list):
return list
l1=[10,20,30,40,50,60]
head(l1)
matrices
m1=[[1,2,3],[7,8,9],[4,5,6]]
m1[0]
m1[0][0]
m1[1]
STRINGS
import string
string1="I am learning python"
str.split(string1)<--------------------split the words in the sentence
str.split(string1, "ea")<---------------------------split the word when encounters
the "ea"
" ".join(string1)<--------------------------joins each character with space
"_".join(string1)<---------------------------------joins each character with
underscore
list commands
l1=[1,2,3,4,5]
l1.append(6) <---------------------------------------append obj at last
l2=[1,1,2,2,3,3,4,4]
l2.count(2)<------------------------------------count of number of occurence of obj
in the list
l2.index(2)<-----------------------index of the first occurence of the obj
l1.pop(3)<------------------delete element from the given index {give index no.
inside th parenthesis}
l2.remove(5)<--------------------removes obj from te frst index of occurence of the
element
l1[:]
l2[:]
l1=[1,2,3,4,5]
l2=[1,1,7,2,3,9,4,4]
l2.sort(reverse=False) ascending
l1.sort(reverse=True) descending
l1[0:]
l1=[15,646,159,196,645,46]
l2=[11,55,463,456,544,48]
sum(l1)
max(l1)
min(l2)
TUPLE
tuple1=('B','c','s','g','e')
tuple2=('A',)+tuple1[1:]
tuple2
TUPLE ASSIGNMENT
anil=('1','anil','kumar')
(id,f_name,l_name)=anil
print(id)
DICTIONARIES
inventory={'apples':430, 'bananas':312, 'oranges':525,'pears':217}
print (inventory)
***********************************************************************************
************************************************
PASS KEYWORD--------> WE DONT WANT TO INCLUDE ANYTHING INSIDE THE BODY AND WE DONT
WANT ANY ERROR DURING EXECUTION WE USE PASS
class abc:
var=10
obj=abc()
print(obj.var)
***********************************************************************************
************************************************
class abc():
def __init__(self,val):----------------------------------> two underscore on each
side
print("in class method")
self.val=val
print("the value is", val)
a1=abc(10)
***********************************************************************************
************************************************
#class definition
class employee:
def __init__(self):#constructor
self.name="abc"
self.salary=10
def disp(self):#member function
print("name=",self.name)
print("salary=",self.salary)
emp1=employee()#object creation
emp1.disp()#member funtion calling
#access by object
print(emp1.name)
***********************************************************************************
************************************************
class employee:
def __init__(self):
self.name="abhi"
self.rollno=10
self.regno=12113731
self.percentage=80
def disp(self):
print("name=",self.name)
print("roll no=",self.rollno)
print("reg no=",self.regno)
print("percentage=",self.percentage)
emp1=employee()
emp1.disp()
print(emp1.name)
print(emp1.rollno)
print(emp1.regno)
print(emp1.percentage)
***********************************************************************************
**********************************************
class employee:
def __init__(self):
self.name=input("enter name")
self.rollno=input("enter roll no")
self.regno=eval(input("enter reg no"))
self.percentage=eval(input("enter percentage"))
def disp(self):
print("name=",self.name)
print("roll no=",self.rollno)
print("reg no=",self.regno)
print("percentage=",self.percentage)
emp1=employee()
emp1.disp()
print(emp1.name)
print(emp1.rollno)
print(emp1.regno)
print(emp1.percentage)
***********************************************************************************
************************************************
class abc():
class_var=0
def __init__(self,var):
abc.class_var+=1
self.var=var
print("the object value is:",var)
print("the value of class variable is:",abc.class_var)
def __del__(self):
abc.class_var-=1
print("object with value %d is going out of scope"
%self.var)------------------------>>>>>>>>>>>> %self.var is address
obj1=abc(10)
obj2=abc(20)
obj3=abc(30)
del obj1
del obj2
del obj3
***********************************************************************************
************************************************
write a class that has a list of integers as data members and
read(),display(),find_largest(),sum() as its member function.
***********************************************************************************
************************************************
wap that has a class CARS.create 2 objects set1 and set2 to be red convertible with
price 10 lakhs and name as pugo ad set2 as blue sedan with price 20 lakhs and name
mavo
class CARS():
def __init__(self,model,price,colour):
self.model=model
self.price=price
self.colour=colour
def display(self):
print("model is %s, price is %s,colour is %s" %
(self.model,self.price,self.colour))
c1=CARS('PUGO convertible','10lakhs','red')
c2=CARS('SEDAN','20 lakhs','blue')
c1.display()
c2.display()
***********************************************************************************
************************************************
class abc():
def _init_(self, name, var):
self.name=name
self.var=var
def _repr_(self):
return repr(self.var)
def _len_(self):
return len(self.name)
def _cmp_(self,obj):
return self.var-obj.var
obj=abc("abcdef",10)
print("The value stored in object is:", repr(obj))
print("The length of the name stored in object is:",len(obj))
obj1=abc("ghijkl",1)
val=obj._cmp_(obj1)
if val==0:
print("both the values are equal")
elif val== -1:
print("first value is less than the second")
else:
print("second value is less than the first")
***********************************************************************************
************************************************
FILE HANDLING
***********************************************************************************
**************************************************
wap to open a file and count the numebr of a in it?
myfile=open('data.txt')
data=myfile.read()
print(data)
print(data.count('a'))
print(data.__len__())
myfile.close()
or
myfile=open('data.txt')
data=myfile.read()
print(data)
print(data.__len__())
count=0
for i in data:
if(i=='a'):
count=count+1
print("frequency of a is:", count)
myfile.close()
***********************************************************************************
**************************************************
myfile=open('data.txt')
data=myfile.read()
print(data)
print
myfile.close()
print(data.replace('e','a'))
count=0
for i in data:
if(i.isupper()==True):
count=count+1
print(count)
myfile.close()
or
'''/for i in data:
if i.isupper():
count=count+1
print(count)'''
***********************************************************************************
***************************************************
append file
myfile=open(r"C:\Users\AbhiPanchami\OneDrive\Desktop\data.txt","w")
for i in range(3):
name=input("enter yor name")
myfile.write(name)
myfile.close()
myfile=open(r"C:\Users\AbhiPanchami\OneDrive\Desktop\data.txt","r")
print(myfile.read()) <-------------------------------not print(data)
will get a not defined error
***********************************************************************************
****************************************************
w+ write and read mode
r+ read and write mode
a+ append and read mode
***********************************************************************************
***************************************************
myfile=open(r"C:\Users\AbhiPanchami\OneDrive\Desktop\student.txt","a")
ans='y'
while ans=='y':
sno=int(input("enter the serial number"))
sname=input("enter student name")
rno=int(input("enter roll number"))
strl=str(sno)+","+str(sname+","+str(rno)+'\n')
myfile.write(strl)
ans=str(input("add more?"))
myfile.close()