You are on page 1of 16

1.

wap to take two numbers as input and do basic operations on it

a=eval(input("enter the value of a"))


b=eval(input("enter the value of b"))
c=a+b
print(C)

*remark* use of EVAL

2.wap to divide a number by 5 and print "highfive" ifthe value is equal to zero

a=eval(input("enter the value of a"))


c=a%5
if(c==0):
print("highfive")

*remark* USE MODULUS FOR DIVISION AND NOT THE SLASH.

3.WAP to find whether a number enetered is prime or not

a=eval(input("enter the number"))


if(a>1):
for i in range(2,a):
if(a%i==0):
print("its a not a prime number")
break

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)

*remark* can use if else also


if(a<b):
a,b=b,a
print("after swapping")
print(a)
print(b)
c=a-b
print("difference:",c)
else:
print("no difference number1<number2")

5. choose random numbers and find the greatest of them


import random
a=random.randint(0,9)
b=random.randint(0,9)
c=random.randint(0,9)
print(a)
print(b)
print(c)
if(a>b & a>c):
print("the greatest number is",a)
elif(b>a & b>c):
print("the greatest number is",b)
else:
print("the greatest number is",c)

*......................................................................* 09:49
10-08-2022

6.find the error.

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)

7. wap to take input and find if it is a leap yr or not

a=eval(input("enter the year"))


if(a%4==0):
days=366
print("this is a leapyear with", days,"days")
print("february has 29 days")
else:
days=365
print("this is a leapyear with", days,"days")
print("february has 28 days")

paragraph qstn!!!

answer: using celestial yr condition for finding leap yr

a=eval(input("enter the year"))


if a%4:
print("not leap yr")
elif a%100==0 and a%400!=0:
print("not leap yr")
else:
print("leap yr")

8.wap to find the bmi?

h=eval(input("enter your height"))


w=eval(input("enter your weight"))
bmi=w/(h*h)
if(bmi<=15):
print("very severely underweight")
elif(bmi>15 and bmi<=16):
print("severely underweight")
elif(bmi>16 and bmi<18.5):
print("underweight")
elif(bmi>18.5 and bmi<=25):
print("normal ")
elif(bmi>25 and bmi<=30):
print("overweight")
elif(bmi>30 and bmi<=35):
print("obese class 1")
elif(bmi>35 and bmi<=40):
print("obese class 2")
elif(bmi>40 and bmi<=45):
print("obese class 3")
elif(bmi>45 and bmi<=50):
print("obese class 4")
elif(bmi>50 and bmi<=60):
print("obese class 5")
else:
print("obese class 6")

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.

WITH FOR LOOP:

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

WITHOUT FOR LOOP:

print("the beginning principle is 10000")


y1=(10000*6)/100
print("the interest for the first year is",y1)
y2=10600*.06
print("the interest for the second year is",y2)
y3=11236*.06
print("the interest for the third year is",y3)
y4=11910.16*.06
print("the interest for the fourth year is",y4)
y5=12624.7496*.06
print("the interest for the fifth year is",y5)
tot_interest=y1+y2+y3+y+y5
final=10000+tot_interest
print("the total interest for five years is",tot_interest)
print("the final amount :",final)

10.WAP to ask for two numbers. if the sum is greater than 100. print "this is a big
number" and terminate the program.

print("give two numbers ")


a=eval(input("enter first number"))
b=eval(input("enter second number"))
sum=a+b
if(sum>100):
print("this is a big number")

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

a=input("enter the string to be reversed")[::-1]


print(a)

***********************************************************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************
************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************
************************************

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)

15. WAP to check if the input alphabet is a vowel or a consonant.

x=input("enter the alphabet ")


v=0
c=0
if(x=='a' or x=='e' or x=='i' or x=='o' or x=='u'x=='A' or x=='E' or x=='I' or
x=='O' or x=='U'):
print("vowel")
else:
print("consonant")

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

'true' is wrong, try is a keyword dont use it as a variable


17. WAP to get fibonacci series btw 0 t 50.

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

18.WAP to check if a triangle is equilateral or isosceles or scalene

equi all sides=


iso no sides=
scalene 2 sides=

print("enter the three sides of the triangle")


a=input("enter 1st side")
b=input("second side")
c=input("third side")
while True:
if(a==b==c):
print("equilateral")
break
elif(a!=b and b!=c and a!=c):
print("isosceles")
break
else:
print("scalene")
break
19. WAP to print alphabet A on screen using "*"

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

21.USE MATH FUNCTION AND DO SOME OPERATIONS

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)

24.WAP THE ABOVE QSTN USING FUNCTIONS

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)

25.GLOBAL VARIABLE AND LOCAL VARIABLE

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)

27. FACTORIAL OF A NUMBER

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))

28.WAP a pythn fnction to chck whether a number is in range

n=int(input("enter the number"))


def checkk(n):
if(n>=1 and n<=100):
print("the entered number is in range between 1 and 100")
else:
print("the entered number is not in range between 1 and 100")
checkk(n)

29.WAP a pythn function to check if a number is prime or not

n=int(input("enter the number"))


def test_prime(n):
if (n==1):
return False
elif (n==2):
return True;
else:
for x in range(2,n):
if(n % x==0):
return False
return True
print(test_prime(n))

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))

30. check if palindrome or not

def checkk(x):
string1=input("enter the value")
string2=string1[::-1]
print(string2)
if(string1==string2):
print("palindrome")
else:
print("not palindrome")

checkk(0)

31.WAP to check if a number is perfect or not?

n=int(input("enter the number"))


def checkk(n):
sum=0
for x in range(1,n):
if(n%x==0):
sum+=x
if(sum==n):
print("perfect")
return (sum)
print(checkk(n))

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]

for matrices nested list is required

k1yk in l1 <-------------------------- "in" is an conditional operator

search if grey is present in the list


"grey" in l1
"red" not in l1

show elements of the list


l1[1:4]
l1[4:]
l1[:4]

how to sort a list ?


use sort list

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.

delete an element from the list


del l1[2]

shuffle the given list

import random
l1=[10,20,30,40,50,60]
random.shuffle(l1)
l1

find the sum of the elements of the list

l1=[10,20,30,40,50,60]
print(sum(l1))

check if two elements are saved at the same location


id(a)
id(b)
if id(a)==id(b) <---------------aliasing

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

how to find the len of a string?????


len(l1)

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)

inventory={'apples':430, 'bananas':312, 'oranges':525,'pears':217}


del inventory['pears']
print(inventory)

***********************************************************************************
************************************************
PASS KEYWORD--------> WE DONT WANT TO INCLUDE ANYTHING INSIDE THE BODY AND WE DONT
WANT ANY ERROR DURING EXECUTION WE USE PASS

INIT ALWAYS TAKE THE FIRST ARGUMENT AS THE SELF ARGUMENT

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)

***********************************************************************************
************************************************

DEALLOCATE AND DELETE

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()

***********************************************************************************
**************************************************

wap to count the number of upper case letters in a file

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()

You might also like