Q Python record file:
1. Input a welcome message and display it:
Code
A=input("Enter a message")
print(A)
Output
2. Input two numbers and display the largest / smallest number.
Code
a=int(input('Enter a number'))
b=int(input('Enter a number'))
if a>b:
print("Larger number",a)
print("smaller number",b)
else:
print("Larger number",b)
print("smaller number",a)
Output
3. Input three numbers and display the largest / smallest number
Code
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Largest number:", max(a, b, c))
print("Smallest number:", min(a, b, c))
Output
4 Generating series:
Code
print("Pattern 1")
for i in range(1,6):
for j in range(1,i+1):
print("*",end='')
print()
print("Pattern 2")
for i in range(5,0,-1):
for j in range(1,i+1):
print(j,end='')
print()
print("Pattern 3")
for i in range(1,6):
for j in range(1,i+1):
print(chr(j+64),end='')
print()
Output:
5.
Code:
x=int(input("Enter a number"))
n=int(input("Enter a number"))
sum=1
for i in range(1,n+1):
sum+=(x**i)/(i)
print(sum)
x= int(input("enter a number: "))
n= int(input("enter a number: "))
Sum = 1
for i in range(1,n+1):
if i%2== 0:
Sum+= (x**i)
else:
Sum+= -(x**i)
print(Sum)
Output:
6. Write a menu driven program to print: ( n is given by user.)
1. factorial series up to n.
2. even series up to n.
3. prime series up to n.
Code:
n=int(input("enter a number"))
print("1 for calculating factorial till the given no.")
print("2 for calculating even series till the given no.")
print("3 for prime series upto the given no.")
b=int(input("enter the menu choice"))
if b==1:
fac==1
for i in range(n+1):
fac*=i
print(fac)
elif b==2:
for i in range(n+1):
if i%2==0:
print(i)
elif b==3:
count=0
for i in range(2, n+1):
for j in range(2,i):
if i%j==0:
break
else:
print(i, end="")
Output:
7 #Determine whether a number is a perfect number, an Armstrong number or a palindrome, using
interactive menu.
Code:
n = int(input("Enter a number: "))
print("1 for finding if the no. is a perfect no.")
print("2 for finding if the no. is a armstrong no.")
print("3 for finding if the no. is a palindrome")
b = int(input("enter the menu choice: "))
if b == 1:
s=0
for i in range(1, n):
if n % i == 0:
s += i
if s == n:
print(n, "is a Perfect number.")
else:
print(n, "is NOT a Perfect number.")
elif b == 2:
s=0
t=n
ds = len(str(n))
for i in range(ds):
d = t % 10
s += d ** ds
t //= 10
if s == n:
print(n, "is an Armstrong number.")
else:
print(n, "is NOT an Armstrong number.")
elif b == 3:
if n == int(str(n)[::-1]):
print("Palindrome")
else:
print("Not Palindrome")
else:
print("the entered choice is invalid")
output:
8. Input a number and check if the number is a prime or composite number.
Code
n=int(input("Enter a number"))
count=0
for i in range(1,n+1):
if n%i==0:
count+=1
if count>2:
print("the no. is composite")
else:
print("the no. is prime")
Output
9. Display the n terms of a Fibonacci series, n is given by user. (hint : 0 1 1 2
3 5 8 13 21..)
Code:
n=int(input('Enter a number'))
a=0
b=1
for i in range(n):
print(a)
a, b = b, a + b
Output:
10. Compute the greatest common divisor and least common multiple of
two integers.
Codes
a=int(input('Enter a number'))
b=int(input('Enter a number'))
if a%b==0:
print('Least common multiple of both the terms is',a)
print('Greatest commmon divisor of both terms is',(a*b)/a)
elif b%a==0:
print('Least common multiple of both the terns is',b)
print('Greatest commmon divisor of both terms is',(a*b)/b)
else:
print('The least common multiple of both the terms is',a*b)
print('Greatest commmon divisor of both terms is',(a*b)/(a*b))
Output:
11. Write a menu driven interactive program to :
1. Count and display the number of vowels, consonants, uppercase,
lowercase characters in the given string.
2. Whether it is a palindrome or not.
3. Convert the case (lower to upper & vice versa) of characters of given
string.
Code:
n=input("Enter a string")
print('''1 to Count and display the number of vowels, consonants, uppercase,
lowercase characters in the given string.''')
print(" 2 to check whether it is a palindrome or not.")
print('''3 to convert the case (lower to upper & vice versa) of characters of given
string.''')
b=int(input("Enter your menu choice"))
if b==1:
v=0
c=0
u=0
l=0
vow="aeiouAEIOU"
for i in range(len(n)):
if n[i] in vow== True:
v+=1
if n[i].isupper==True:
u+=1
if n[i].islower== True:
l+=1
if n[i] in vow== False:
c+=1
print("NO. of vowels",v)
print("No. of consonants",c)
print("No. of lower case",l)
print("No. of upper case", u)
if b==2:
if n==n[::-1]:
print("The word is a palindrome")
else:
print("The word is not a palindrome")
if b==3:
r = ""
for i in range(len(n)):
if n[i].islower():
r += n[i].upper()
elif n[i].isupper():
r += n[i].lower()
else:
r += i
print("Converted string:", r )
Output:
12. interactive menu
Codes:
n=eval(input("Enter a list"))
print("1 Search an element")
print("2 largest and smallest element")
print("3 sum of all elements")
print("4 to terminate the program")
b=int(input("Enter a no."))
if b==1:
for i in range(len(n)):
a=int(input("Element to be searched"))
if n[i]== a:
print("The given element is at index",i)
if b==2:
print("the largest no.",max(n))
print("the smallest no.",min(n))
if b==3:
s=0
for i in range(len(n)):
s+=n[i]
print("sum of the elements is",s)
if b==4:
print("your program is succesfully terminated")
Output:
13. L=eval(input("Enter a List"))
if len(L)%2==0:
end=len(L)
else:
end=len(L)-1
print("Original List",L)
for i in range(0,end,2):
L[i],L[i+1]=L[i+1],L[i]
print("List after Swapping",L)
Output:
14. Write a menu driven program to do following operations on a tuple:
Codes:
n=eval(input("Enter a tuple"))
print("1 Search an element")
print("2 largest and smallest element")
print("3 sum of all elements")
print("4 to terminate the program")
b=int(input("Enter a no."))
if b==1:
a=int(input("Element to be searched"))
for i in range(len(n)):
if n[i]== a:
print("The given element is at index",i)
if b==2:
print("the largest no.",max(n))
print("the smallest no.",min(n))
if b==3:
s=0
for i in range(len(n)):
s+=n[i]
print("sum of the elements is",s)
if b==4:
print("your program is succesfully terminated")
Output:
15. create a dictionary students in a class and display the names of students who have
marks above 75.
for i in range(5):
print("Enter Details of student No.", i+1)
roll_no = int(input("Roll No: "))
std_name = input("Student Name: ")
marks = int(input("Marks: "))
result[roll_no] = [std_name, marks]
print("Dictionary with the details of 5 students")
print(result)
# Display names of students who have got marks more than 75
for student in result:
if result[student][1] > 75:
print("Student's name who get more than 75 marks is",(result[student][0]))
Output: