Ques. Count number of words in sentence.
Ans.
#developed by Divyansh Choudhary
S=input(“enter the sentence:”)
count=0
for i in s:
if i ==
count=count+1
print(count)
OUTPUT:-
Enter the sentence: This is a program.
Ques.WAP to put even and odd elements in a list of 2 different list.
Solution:-
#developed by Divyansh Choudhary
a=[]
n=int(input(“enter the number of elements:”))
for i in range (1, n+1)
b=int(input(“enter elements :”))
a=append(b)
even=[]
odd=[]
for j in :
if (j%2=0):
even .append (j)
else:
odd,append(j)
print(“the even list”, even)
print(“the odd list”,odd)
OUTPUT:-
Enter the number of elements 6
Enter the element 23
Enter the element 13
Enter the element 12
Enter the element 16
Enter the element 18
Enter the element 2
The even list [12,16,18,2]
The odd list [23,13]
Ques. Take the list and make a new list that has only the even
elements of this in it using list comprehension
Ans-
#developed by Divyansh Choudhary
list=[]
n=int(input(“enter the no. of elements in the list”)
for n in range (n):
ele=int(input(“enter the elements”))
list.append(ele)
print(list)
list1=[n for n in list if x%2==0]
print(list1)
OUTPUT:-
Enter the number of elements 4
Enter the elements 22
Enter the element 5
Enter the element 14
Enter the element 26
[22,5,14,26]
[12,14,26]
Ques. WAP to find greatest among three number using argument
using return.
Ans.
#developed by Divyansh Choudhary
Def greatest(a,b,c):
If a>b and a>c:
return a
elif b>a and b>c:
return b
else:
return c
n1=int(input(“enter the first number”))
n2=int(input(“enter the second number”))
n3=int(input(“enter the third number”))
grt=greatest(n1,n2,n3)
print(“greatest”,n1,n2,n3, “=”,grt)
Ques. WAP to find factorial using function.
Ans.
#developed by Divyansh Choudhary
#using argument without return
Def fact(n):
f=1
for x in range(1,n+1):
f=f*x
print(“factorial of”,n, “=”,f)
num=int(input(“enter number of factorial”)
fact(num)
Ques. WAP for swapping of two numbers without using third
variable.
Ans.
#developed by Divyansh Choudhary
a=int(input(“enter number 1 \t”))
b=int(input(“enter number 2 \t”))
print(“before swapping number 1=”,a, “& number 2=”,b)
a=a+b
b=a-b
a=a-b
print(“after swapping number 1=”,a, “& number 2=”,b)
Ques. WAP to check if entered number is even or add.
Ans.
#developed by Divyansh Choudhary
n=int(input(“enter the number\t”
If n%2==0
Print(“number is even”,n)
Else,
Print(“number is odd”,n)
Ques. WAP to find the largest number from 3 numbers.
Ans.
#developed by Divyansh Choudhary
n1=int(input(“enter the first number”))
n2=int(input(“enter the second number”))
n3=int(input(“enter the third number”))
If (n1>n2) and (n1>n3)
largest=n1
elif(n1>n2) and (n2>n3)
largest=n2
Else:
Largest=n3
Print (“the largest number is”,largest)
Ques. WAP to find sum of digits .
Ans.
#developed by Divyansh Choudhary
n=int(input("Enter a number:"))
For=0
while(n>0):
dig=n%10
tot=tot + dig
n=n//10
print("the total sum of digits is:" ,tot)
Ques. WAN to find reverse of a number.
Ans.
#developed by Divyansh Choudhary
Number = int (input " Please enter any number : " ))
Reverse = 0
while (Nember>0):
Reminder = number % 10
Reverse =(reverse * 10) + reminder
Number = Number // 10
print ("\n Reverse of entered number is = %d ", %Reverse)
Ques. Python program to find the greatest of three numbers using
Nested if
Ans.
#Developed by Divyansh Choudhary
num1 = int(input())
num2 = int(input())
num3 = int(input())
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
greatest = num2
else:
greatest = num3
print("The greatest number is",greatest )
Ques. - WAP to prompt for a score / marks between 0 to 100 if the
score/ marks is out of 100 print an error message .If the score is
between 0 and 100 , print grade using following table :
SCORE GRADE
>=90 A
>=80 B
>=70 C
>=60 D
Ans.
#developed by Divyansh Choudhary
Score =(input("enter score between 0 and 100"))
if score > "0" or score < "100":
print="error"
elif score >=90:
print="A"
elif score >=80:
print="B"
elif score >=70:
print="C"
elif score >=60:
print="D"
else:
print ="fail"
Ques. Write a program to find out the sum total of coins given in form
of 10rs, 5rs, 2rs, 1rs coins.
Ans.
# developed by Divyansh Choudhary
a = int(input("enter number of 10 rupees coins"))
b = int(input("enter number of 5 rupees coins"))
c = int(input("enter number of 2 rupees coins"))
d = int(input("enter number of 1 rupees coins"))
money = a*10 + b*5 + c*2 + d*1
print("total money", money)
LAB NO. 4
Program.1 :- WAP to Design Arithmetic Calcultor
Ans.
# created by Divyansh Choudhary
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
def floar_division(x, y):
return x // y
def power (x, y):
return x ** y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("5.floar_division")
print("6.power")
while True:
choice = input("Enter choice(1/2/3/4/5/6): ")
if choice in ('1', '2', '3', '4','5','6'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
elif choice == '5':
print(num1, "/", num2, "=", floar_division(num1, num2))
elif choice == '6':
print(num1, "/", num2, "=", power(num1, num2))
break
else:
print("Invalid Input")
LAB No. 6
Program 1. - To find largest number in list
Ans.
#developed by Divyansh Choudhary
list1 = []
num = int(input("Enter number of elements in list: "))
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
list1.append(ele)
print("Largest element is:", max(list1))
Program 2.- WAP to find Sum, Product, and Count the number of
elements in the list.
Ans.
#developed by Divyansh Choudhary
List = [24,35,46,15,13]
Sum = 0
Product = 1
Count = 0
for x in list:
Sum = Sum + x
Product = Product * x
Count = Count + 1
print(Sum)
Ques. WAP that accept an integer (n) between 1 to 9 & compute the
value of n+nn+nnn
Ans.
#created by Divyansh Choudhary
n = int(input("Input an integer : "))
n1 = int( "%s" % n)
n2 = int( "%s%s" % (n,n) )
n3 = int( "%s%s%s" % (n,n,n) )
print (n1+n2+n3)
OR
n= int(input("enter positive number"))
if n>= 0 and n <= 9 :
nn = n*10+n
nnn = n+n*10+n*100
S = n+ nn+ nnn
Print (S)
else :
print ("wrong input")
Ques.- WAP to find a list & assign +1 to +ve number & -1 to -ve
number & 0 for null.
Ans.
#Created by Divyansh Choudhary
list = [23,34,57,-76,-54,0]
n = list.copy()
for i in range (0 , len(list)):
if list [i] > 0 :
list [i]=1
elif list [i] < 0:
list [i] = -1:
print (list)
Ques.- programme to find factorial using function using argument
using return.
Ans.
#Created by Divyansh Choudhary
def fact (n):
f=1
for x in range (1,n+1):
f=f*x
return f
num = int(input ("enter number of factorial"))
ft= fact(num)
print("factorial of",num,"=",ft)
Ques.- WAP to find maximum and minimum by function.
Ans.
#Created by Divyansh Choudhary
x=[ 25, 24,26,27,50]
print ("maximum number of the list:",max(x))
print("minimum number of the list:",min(x))
Print ("length of list", len(n))
print("sum of number of list :",sum(n))
x = reverse ()
print (n)
Program to find roots of a quadratic equation using if else statements
Import math
#developed by Divyansh Choudhary
Print(“i will solve the equation ax**2+bx+c =0”)
a=int(input('a= '))
b=int(input('b= '))
c=int(input('c= '))
d=b**2- 4*a*c
if d<o:
print ( ‘the equation has no real solution ')
elif d == 0 :
x = (-b)/(2*a)
print(‘this equation has one solution : ',x)
else :
x1=(-b+math.sqrt(d))/(2*a)
x2=(-b-math. sqrt(d))/(2*a)
print (‘This equation has two solutions: ',x1 'or' x2)
Program to find sum of 4 digit number using recursion and without
recursion
1.Using Recursion
#developed by Divyansh Choudhary
Def sum_digits(number)
#base case
If number==0:
Return 0
Else:
Return(number%10)+sumdigits(number//10)
2.Without using recursion
#developed by Divyansh Choudhary
N=int(input(‘enter a number= '))
Total=0
While(n>0):
Digit=n%10
Total=Total+digit
N=N//10
Print('the sum of digits is= ',total)
Program to find number of digits in a number
#developed by Divyansh Choudhary
N=int(input('enter a number= '))
M=N
Count=0
While N>0:
Count=Count+1
N=N//10
Print('numbers of digits in',M,'are= ', Count)
Output
Enter number 2345
Number of digits in 2345=4
Ques. Linear search on tuple
Ans.
#Developed by Divyansh Choudhary
def search ( tuple, n):
for i in range (len (tuple)):
if Tuple [i]==n:
return True
else
return False
Tuple=(1,2, 'Home',4, 'Sweet')
n='Home'
if search (Tuple,n):
print ("found")
else
print ("not found")
OUTPUT
Found
Ques. Binary search on Tuple
Ans.
#Developed by Divyansh Choudhary
From bisect import bisect_left
def binary search (a,x):
i=bisect_left (a,x)
if i!=len (a) and a [i]==x:
return i
else:
return -1
a=(1,2,4,4,8)
x=int (4)
res=Binary search (a,x)
if res== -1:
print ( x, "is absent" )
else:
print ( "First occurrence of",x, "is present at",res)
OUTPUT
First occurrence of 4 is present at 2