PYTHON PROGRAMS
1 .Write a program to check whether a number is divisible by 2 or 3 using nested if
num=float(input('Enter a number'))
if num%2==0:
if num%3==0:
print ("Divisible by 3 and 2")
else:
print ("divisible by 2 not divisible by 3")
else:
if num%3==0:
print ("divisible by 3 not divisible by 2")
else:
print ("not Divisible by 2 not divisible by 3")
2.Write a program to find sum, subtraction, multiplication and division of values.
print("1. Sum of two numbers")
print("2. Subtaction of two numbers")
print("3. Multiplication of two numbers")
print("4. Division of two numbers")
choice=int(input('Enter your choice'))
if choice==1 :
a=int(input('Enter first number'))
b=int(input('Enter second number'))
c=a+b
print("Sum=",c)
elif choice==2 :
a=int(input('Enter first number'))
b=int(input('Enter second number'))
c=a-b
print("Subtraction=",c)
elif choice==3 :
a=int(input('Enter first number'))
b=int(input('Enter second number'))
c=a*b
print("Multiplication=",c)
elif choice==4 :
a=int(input('Enter first number'))
b=int(input('Enter second number'))
c=a/b
print("Division=",c)
else
print("Wrong choice")
3. Write a Program to find sum of even numbers from 1 to 7
sum=0
for num in range(8):
if num%2==0:
sum=sum+num
print("Sum of even values=",sum)
4. Write a Program to find factorial of a number
n=int(input("Enter a number="))
i=1
f=1
while i<=n:
f=f*i
i=i+1
print("Factorial of",n,"=",f)
5. Write a Program to print table of any number.
n=int(input("Enter a number whose table you want="))
i=1
while i<=10:
print(n,"X",i,"=",n*i)
i=i+1
7. Write a Program to check whether a value exists in dictionary.
aDict={'Bhavna':1,"Richard":2,"Firoza":3,"Arshnoor":4}
val=int(input('Enter value'))
flag=0
for k in aDict:
if val==aDict[k]:
print("value found at key",k)
flag=1
if flag==0:
print("value not found")
8. Write a Program to find largest among two numbers using a user defined function .
def largest():
a=int(input("Enter first number="))
b=int(input("Enter second number="))
if a>b :
print ("Largest value=%d"%a)
else:
print ("Largest value=%d"%b)
return
largest()
9. Write a Program to find sum of two numbers using a user defined function with parameters.
def sum(a,b): #a and b are formal parameters
c=a+b
print("sum=",c)
sum(4,5)
n1,n2=eval(input('Enter two values'))
sum(n1,n2)
6. Write a Program to use default arguments in a function.
def printinfo( name, age = 35 ): #default argument
print ("Name: ", name)
print ("Age ", age)
return
printinfo("aman",45)
printinfo("Parth")
10. Write a Progrm to pass a list as function argument and modify it.
def changeme( mylist ):
print ("inside the function before change ", mylist)
mylist[0]=1000
print ("inside the function after change ", mylist)
return
list1 = [10,20,30]
print ("outside function before calling function", list1)
changeme( list1 )
print ("outside function after calling function", list1)
11. Write a function that receives two numbers and generates a random number from that range. Using
function the main should be able to print three numbers randomly.
import random
def random_num(x,y):
return random.randint(x,y)
n1=int(input("Enter the first number:"))
n2=int(input("Enter the second number:"))
print("Random No.1:",random_num(n1,n2))
print("Random No.2:",random_num(n1,n2))
print("Random No.3:",random_num(n1,n2))
12. Write a function that receives two string arguments and checks whether they are same-length strings. (returns
True in this case otherwise False).
def check(s1,s2):
f len(s1) == len(s2):
return True
else:
return False
s1 = input("Enter First String: ")
s2 = input("Enter Second String: ")
print("The String is same length?",check(s1,s2))
13. Write a python program to take input for 3 numbers, check and print the largest number?
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
c=int(input("Enter 3rd no "))
if(a>b and a>c):
m=a
else:
if(b>c):
m=b
else:
m=c
print("Max no = ",m)
14. Write a python program to take input for 2 numbers and an operator (+ , – , * , / ). Based on the
operator calculate and print the result?
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
op=input("Enter the operator (+,-,*,/) ")
if(op=="+"):
c=a+b
print("Sum = ",c)
elif(op=="*"):
c=a*b
print("Product = ",c)
elif(op=="-"):
if(a>b):
c=a-b
else:
c=b-a
print("Difference = ",c)
elif(op=="/"):
c=a/b
print("Division = ",c)
else:
print("Invalid operator")
15. Write a program to find weather an inputted number is perfect ot not
def pernum(num):
divsum = 0
for i in range(1,num):
if num%i == 0:
divsum +=i
if divsum == num:print("Number is perfect")
else:print("Number is not perfect")
X = int(input("Enter a number"))
pernum(X)
16. Write a program to check weather a number is armstrong or not
def checknum(num):
_len = len(str(num))
no = num
sum = 0
while(num > 0):
rem = num % 10
sum = sum + math.pow(rem,_len)
num = int(num/10)
if(sum == no):print("Armstorm number")
else:print("Not armstorm number")
checknum(int(input("Enter a number"))
17. Write a program to find factorial
def checknum(num):
fact = 1
no = num
while(no > 0):
fact = fact * no
no = no - 1
print("Factorial of a num ",fact)
checknum(int(input("Enter a number"))
18 .Write a python program on linear search
def linear_search(lst, target):
for index, value in enumerate(lst): if value == target: return index
return -1
# Example usage:my_list = [1, 3, 5, 7, 9, 11, 13]
target_value = 7
result = linear_search(my_list, target_value)
if result != -1: print(f"Element {target_value} found at index {result}.")
else: print(f"Element {target_value} not found in the list.")
19. Write a program to print pattern in descending
def pattprint(n):
for i in range(n,-1,-1):
for a in range(0,i):
print("*",end="")
print()
num = int(input("Enter the len"))
pattprint(num)
)
20. Write a python program on Binary search
def binary_search(lst, target):
low, high = 0, len(lst) - 1
while low <= high:
mid = (low + high) // 2
mid_value = lst[mid]
if mid_value == target:
return mid
elif mid_value < target:
low = mid + 1
else:
high = mid - 1
return -1
# Example usage:
my_sorted_list = [1, 3, 5, 7, 9, 11, 13]
target_value = 7
result = binary_search(my_sorted_list, target_value)
if result != -1:
print(f"Element {target_value} found at index {result}.")
else:
print(f"Element {target_value} not found in the list.")
21.Write a program on recursive code to find the sum of all elements of a list.
def recursive_sum(lst):
if not lst:
return 0
else:
return lst[0] + recursive_sum(lst[1:])
# Example usage:
my_list = [1, 2, 3, 4, 5]
result = recursive_sum(my_list)
print(f"The sum of the elements in the list is: {result}
22. Write a program on recursive code to compute the nth Fibonacci number.
def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return(fibonacci(n-2) + fibonacci(n-1))
nterms = int(input("Please enter the Range Number: "))
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(fibonacci(i),end=' ')
23. Write a random number generator that generates random numbers between 1 and
6 (simulates a dice).
import random
import random
def roll_dice():
print (random.randint(1, 6))
print("""Welcome to my python random dice program!
To start press enter! Whenever you are over, type quit.""")
flag = True
while flag:
user_prompt = input(">")
if user_prompt.lower() == "quit":
flag = False
else:
print("Rolling dice...\nYour number is:")
roll_dice()
24. Write a program to find the factorial of a natural number.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
def main():
n = int(input("Enter any number: "))
print("The factorial of given number is: ",factorial(n))
main()
SUGHAR SINGH ACADEMY
SWARN JAYANTI VIHAR ,KANPUR
2023-24
COMPUTER PRACTICAL FILE
SUBMITTED BY: SUBMITTED TO:
NAME: Manas Shukla MR. MANISH GUPTA (P.G.T)
CLASS : XII HOD(C.S)
ROLL.NO:
SUGHAR SINGH ACADEMY
SWARN JAYANTI VIHAR ,KANPUR
2023-24
COMPUTER PRACTICAL FILE
SUBMITTED BY: SUBMITTED TO:
NAME: Digvijay Singh MR. MANISH GUPTA (P.G.T)
CLASS : XII HOD(C.S)
ROLL.NO:
SUGHAR SINGH ACADEMY
SWARN JAYANTI VIHAR ,KANPUR
2023-24
COMPUTER PROJECT
TOPIC: HOTEL MANAGEMENT SYSTEM
SUBMITTED BY: SUBMITTED TO:
NAME: Manas Shukla MR. MANISH GUPTA (P.G.T)
CLASS : XII HOD(C.S)
ROLL.NO:
SUGHAR SINGH ACADEMY
SWARN JAYANTI VIHAR ,KANPUR
2023-24
COMPUTER PROJECT
TOPIC: HOTEL MANAGEMENT SYSTEM
SUBMITTED BY: SUBMITTED TO:
NAME: Digvijay Singh MR. MANISH GUPTA (P.G.T)
CLASS : XII HOD(C.S)
ROLL.NO:
CERTIFICATE
This is hereby to certify that, the original and genuine work has been
carried out to create the about the topic PYTHON PROGRAMMING
AND SQL as well as the entire work has been completed solely, sincerely
and satisfactorily done by, Manas Shukla a student of class XII (Science)
under the Academic Session 2023-2024.
Regarding the Project entitled:
"PYTHON PROGRAMMING AND SQL"
For Computer Science department under direct supervision of the undersigned
as per the requirement for the Examination.
INTERNAL EXAMINER EXTERNAL EXAMINER
Principal
CERTIFICATE
This is hereby to certify that, the original and genuine work has been
carried out to create the about the topic PYTHON PROGRAMMING
AND SQL as well as the entire work has been completed solely, sincerely
and satisfactorily done by, Digvijay Singh a student of class XII (Science)
under the Academic Session 2023-2024.
Regarding the Project entitled:
"PYTHON PROGRAMMING AND SQL"
For Computer Science department under direct supervision of the undersigned
as per the requirement for the Examination.
INTERNAL EXAMINER EXTERNAL EXAMINER
Principal
CERTIFICATE
This is hereby to certify that, the original and genuine work has been
carried out to create the about the topic HOTEL MANAGEMENT as
well as the entire work has been completed solely, sincerely and
satisfactorily done by, Digvijay Singh a student of class XII (Science)
under the Academic Session 2023-2024.
Regarding the Project entitled:
"HOTEL MANAGEMENT SYSTEM "
For Computer Science department under direct supervision of the undersigned
as per the requirement for the Examination.
INTERNAL EXAMINER EXTERNAL EXAMINER
Principal
CERTIFICATE
This is hereby to certify that, the original and genuine work has been
carried out to create the about the topic HOTEL MANAGEMENT as
well as the entire work has been completed solely, sincerely and
satisfactorily done by, Manas Shukla a student of class XII (Science)
under the Academic Session 2023-2024.
Regarding the Project entitled:
"HOTEL MANAGEMENT SYSTEM "
For Computer Science department under direct supervision of the undersigned
as per the requirement for the Examination.
INTERNAL EXAMINER EXTERNAL EXAMINER
Principal