INDEX
Q1. Write a program to input a welcome message and print it.
Q2. Program to obtain three numbers and print their sum.
Q3. Write a program to input three numbers and swap them as this : 1st number
becomes the 2nd number, 2nd number becomes the 3rd number and the 3rd number
becomes the first number.
Q4. Write a program to input a number and print its cube.
Q5. Write a program to find average of three numbers.
Q6. Write a program to input two numbers and swap them.
Q7. Program that takes a number and checks whether the given number is odd or
even.
Q8. Program to test the divisibility of a number with another number.
Q9. Program to display a menu for calculating area of a circle or perimeter of a circle.
Q10. Program that reads three numbers (integers) and prints them in ascending order.
Q11. Program to print table of a number, say 5.
Q12. Program to calculate the factorial of a number.
Q13. Program to input a number and test if it is a prime number.
Q14. Program to accept three integers and print the largest of the three. Make use of
only if statement.
Q15. Write a program to input an integer and check if it contains any 0 in it.
Q16. Write a program to input a string and check if it is a palindrome string using a
string slice.
Q17. Write a program to input two strings. If string1 is contained in string2, then create a
third string with first four characters of string2’ added with word ‘Restore’.
Q18. Write a program that inputs a string that contains a decimal number and prints out
the decimal part of the number. For instance, if 515.8059 is given, the program should
print out 8059.
Q19. Write a program that inputs a line of text and prints its each word in a separate
line. Also, print the count of words in the line.
Q20. Write a python to input three angles and determine if they form a triangle or not
Q21. Program to print elements of a list [ ‘q’, ‘w’, ‘e’, ‘r’, ‘t’, ‘y’ ] in separate lines along
with element’s both indexes (positive and negative).
Q22. Write a program to create a copy of a list. In the list’s copy, add 10 to its first and
last elements. Then display the lists.
Q23. Write a program to calculate the mean of a given list of numbers
Q24. Write a program to design a dice throw
Q25. Write a program that inputs a list, replicates it twice and then prints the sorted list
in ascending and descending orders.
Q1. Write a program to input a welcome message and print it.
message=input("Enter welcome message : ")
print("Hello, ",message)
OUTPUT
Enter welcome message : Python welcomes you to its beautiful world.
Hello, python welcomes you to its beautiful world.
Q2. Program to obtain three numbers and print their sum.
num1=int(input("Enter Number 1 : "))
num2=int(input("Enter Number 2 : "))
num3=int(input("Enter Number 3 : "))
sum=num1+num2+num3
print("Three Numbers are : ",num1,num2,num3)
print("Sum is : ",sum)
OUTPUT
Enter Number 1 : 7
Enter Number 2 : 3
Enter Number 3 : 13
Three Numbers are : 7 3 13
Sum is : 23
Q3. Write a program to input three numbers and swap them as this : 1 st number
becomes the 2nd number, 2nd number becomes the 3rd number and the 3rd number
becomes the first number.
n1=int(input("Enter number 1: "))
n2=int(input("Enter number 2: "))
n3=int(input("Enter number 3: "))
print("Original Number: ",x,y,z)
x,y,z=y,z,x
print("After Swapping: ",x,y,z)
OUTPUT
Enter number 1: 6
Enter number 2: 11
Enter number 3 : 9
Original Number : 6 11 9
After Swapping : 11 9 6
Q4. Write a program to input a number and print its cube.
num=int(input("Enter a number: "))
cube=num**3
print("Number is",num,")
print(“Its cube is”,cube)
OUTPUT
Enter a number:7
Number is 7
Its cube is 343
Q5. Write a program to find average of three numbers.
x = int (input("Enter value1: "))
y = int (input("Enter value2: "))
z = int (input("Enter value3: "))
print(“Three numbers are:”,x,y,z))
avg=(x+y+z)/3
print("Average of given numbers :", avg)
OUTPUT
Enter value1 :14
Enter value2 :12
Enter value3 :13
Three numbers are: 14 12 13
Average of given numbers: 13.0
Q6. Write a program to input two numbers and swap them.
n1=int(input("Enter number 1 : "))
n2=int(input("Enter number 2 : "))
print("Original number : ",n1,n2)
n1,n2=n2,n1
print("After swapping : ",n1,n2)
OUTPUT
Enter number 1 : 17
Enter number 2 : 28
Original Number : 17 28
After Swapping : 28 17
Q7. Program that takes a number and checks whether the given number is odd or
even.
num = int(input("Enter an integer: "))
if num % 2 == 0:
print(num, “is EVEN number.”)
else:
print(num, “is ODD number.”)
OUTPUT
Enter an integer:8
8 is EVEN number.
Q8. Program to test the divisibility of a number with another number.
num1=int(input("Enter first number :"))
num2=int(input(" Enter second number"))
remainder=num1%num2
if remainder==0:
print(number1, "is divisible by", number2)
else:
print (number1, "is not divisible by", number2)
OUTPUT
Enter first number :119
Enter second number : 3
119.0 is not divisible by 3.0
Q9. Program to display a menu for calculating area of a circle or perimeter of a
circle.
radius=float(input(“Enter radius of the circle:”))
print("1. Calculate Area")
print("2. Calculate Perimeter")
choice= int(input(" Enter your choice (1 or 2):"))
If choice==1:
area=3.14159*radius*radius
print("Area of circle with radius”,radius, ‘is’,area)
else:
Perm=2*3.14159*radius
Print(“Perimeter of circlewith radius”,radius, ‘is’, perm)
OUTPUT
Enter radius of circle : 2.5
1.Calculate Area
2.Calculate Perimeter
Enter your choice (1 or 2) :1
Area of circle with radius 2.5 is 19.6349375
Q10. Program that reads three numbers (integers) and prints them in ascending
order.
x=int(input("Enter First number : "))
y=int(input("Enter Second number : "))
z=int(input("Enter Third number : "))
min=mid=max=None
if x<y and x<z:
if y<z:
min,mid,max=x,y,z
else:
min,mid,max=x,z,y
elif y<x and y<z:
if x<z:
min,mid,max=y,x,z
else:
min,mid,max=y,z,x
else: in
if x<y:
min,mid,max=z,x,y
else:
min,mid,max=z,y,x
print("Numbers in ascending order are : ",min,mid,max)
OUTPUT
Enter First number : 5
Enter Second number : 9
Enter Third number : 2
Numbers in ascending order are : 2 5 9
Q11. Program to print table of a number, say 5.
num=5
for a in range(1,11):
print(num, 'x',a, '=',num*a)
OUTPUT
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Q12. Program to calculate the factorial of a number.
num=int(input("Enter a number:"))
fact=1
a=1
while a <=num:
fact*=a
a+=1
print("The factorial of",num,"is",fact)
OUTPUT
Enter a number:3
The factorial of 3 is 6
Q13. Program to input a number and test if it is a prime number.
num=int(input("Enter number:"))
lim=int(num/2)+1
for i in range(2,lim):
rem=num%i
if rem==0:
print(num,"is not a prime number")
break
else:
print(num,"is a prime number")
OUTPUT
Enter number:7
7 is a prime number
Q14. Program to accept three integers and print the largest of the three. Make
use of only if statement.
x=y=z=0
x=float(input("Enter first number : "))
y=float(input("Enter second number : "))
z=float(input("Enter third number : "))
max=x
if y>max:
max=y
if z>max:
max=z
print("Largest number is",max)
OUTPUT
Enter first number: 7.235
Enter second number: 6.99
Enter third number: 7.533
Largest number is 7.533
Q15. Write a program to input an integer and check if it contains any 0 in it.
n=int(input(“Enter a number:”))
s=str(n)
If ‘0’ in s:
print(There’s a 0 in”,n)
else:
print(“No 0 in”,n)
OUTPUT
Enter a number : 6708
There's a 0 in 6708
Q16. Write a program to input a string and check if it is a palindrome string
using a string slice.
s=input("Enter a string:")
if(s==s[::-1):
print(s,"is a Palindrome.")
else:
print(s,"is NOT a Palindrome.")
OUTPUT
Enter a string : inosuke
inosuke is NOT a Palindrome.
Q17. Write a program to input two strings. If string1 is contained in string2, then
create a third string with first four characters of string2’ added with word
‘Restore’.
s1=input("Enter string1:")
s2=input("Enter string2:")
print(“Original strings:",s1,s2)
if s1 in s2:
s3=s2[0:4]+"Restore"
print("Final strings:",s1,s3)
OUTPUT
Enter string1: tanjiro
Enter string2: kamado
Original strings: tanjiro kamado
Final strings : tanjiro kamadoRestore
Q18. Write a program that inputs a string that contains a decimal number and
prints out the decimal part of the number. For instance, if 515.8059 is given, the
program should print out 8059.
s=input(‘ Enter a string (a decimal number) : ’ )
t=s.partition(‘.’)
print(“Given string:”, s)
print(“part after decimal”, t[2] )
OUTPUT
Enter a string(a decimal number):515.8059
Given string: 515.8059
part after decimal
Q19. Write a program that inputs a line of text and prints its each word in a
separate line. Also, print the count of words in the line.
s=input(“Enter a list of text :”)
count=0
for word in s.split():
print (word)
count += 1
print(“Total words:”, count)
OUTPUT
Enter a line of text : I am under the water
I
am
under
the
water
Total words: 5
Q20. Write a python to input three angles and determine if they form a triangle or
not
#check triangle from angles
Angle1 = angle2= angle3= 0
Angle1=(float(input(“Enter angle1 : “))
Angle2=(float(input(“Enter angle2 : “))
Angle3=(float(input(“Enter angle3 : “))
If (angle1 + angle2 + angle3) == 180 :
Print(“Angles form a triangle.”)
Else:
Print (“Angles do no form a triangle”)
Enter angle1 : 30
Enter angle2 : 60
Enter angle3 : 90
Angles form a triangle.
Q21. Program to print elements of a list [ ‘q’, ‘w’, ‘e’, ‘r’, ‘t’, ‘y’ ] in separate lines
along with element’s both indexes (positive and negative).
L = [‘q’, ‘w’, ‘e’, ‘r’, ‘t’, ‘y’]
Length = len(L)
For a in range(length):
Print(“At indexes”, a, “and”, (a – length), “element:”, L[a])
OUTPUT
At indexes 0 and -6 element : q
At indexes 1 and -5 element : w
At indexes 2 and -4 elemen : e
At indexes 3 and -3 element : r
At indexes 4 and -2 element : t
At indexes 5 and -1 element : y
Q22. Writer program to create a copy of a list. In the list’s copy, add 10 to its first
and last elements. Then display the lists.
L1 = [09 , 17 , 24 ,44]
L2=L1.copy()
Print(“Original list :”,L1)
Print(“Creating copy of the list :”,L2)
L2[0] += 10
L2[-1] += 10
Print(“Copy of list after changes :” , L2)
Print(“Original list :” L1)
OUTPUT
Original list : [09, 17 , 24 , 44]
Created copy of the list : [09 , 17 , 24 , 44]
Created copy of the after chances : [19 , 17 , 24 , 54]
Original list : [09, 17 , 24 , 44]
Q23. Write a program to calculate the mean of a given list of numbers
Lst eval(input(“Enter list: “))
Length = len(1st)
Mean = sum = 0
For I in range(0, length):
Sum += 1st[i]
Mean = sum/length
Print(“Given list is: “, 1st)
Print(“The mean of the given list is :”, mean)
OUTPUT
Enter list : [7, 23, -11, 55, 13.5, 20.05, -5.5]
Given list is [7, 23, -11, 55, 13.5, 20.05, -5.5]
The mean of the given list is: 14.578571428571427
Q24. Write a program to design a dice throw
Import random
N = int (input (“How many dice throws?”))
For I in range(1, n+1) :
Print(“Throw”, I, “:”, random.randint . (1, 5) )
OUTPUT
How many dice throws ? 5
Throw 1: 5
Throw 2: 4
Throw 3: 5
Throw 4: 3
Throw 5: 5
Q25. Write a program that inputs a list, replicates it twice and then prints the
sorted list in ascending and descending orders.
Val = eval(input(“Enter a list :”))
Print(“Original list : “, val)
Val = val* 2
Print(“Replicated list : “, val)
Val.sort()
Print(“Sorted in ascending order : “, val)
Val.sort(reverse = True)
Print(“Sorted in descending order : “, val)
OUTPUT
Enter a list : [23, 11, 29]
Original list : [23, 11, 29]
Replicated list: [23, 11, 29, 23, 11, 29]
Sorted in ascending order: [11, 11, 23, 23, 29, 29]
Sorted in descending order: [29, 29, 23, 23, 11, 11]