Python Programming
• Input a welcome message and display it.
message=input("Enter the welcome message:")
print(message)
• Input two numbers and display the larger/smaller number.
no1 = int(input("Enter the 1st number:"))
no2 = int(input("Enter the 2nd number:"))
if no1 > no2:
print(no1,"is larger")
else:
print(no2,"is larger")
no1 = int(input("Enter the 1st number:"))
no2 = int(input("Enter the 2nd number:"))
if no1 < no2:
print(no1,"is smaller")
else:
print(no2,"is smaller")
• Given two integers x and n, compute xn.
x = int(input("Enter the base number:"))
n = int(input("Enter the power:"))
print(x**n)
• Input a string and determine whether it is a palindrome or not.
original = input("Enter a string:")
reverse = original[::-1]
if original == reverse:
print("String is palindrome.")
else:
print("String is not palindrome.")
• Determine whether a number is a perfect number, an armstrong number or a
palindrome.
num=int(input("Enter a number:"))
sum=0
for i in range(1,num):
if num % i == 0:
sum = sum + i
if sum == num:
print(num,"is perfect number.")
else:
sum = 0
temp = num
while temp > 0:
rem = temp %10
sum = sum + (rem*rem*rem)
temp = temp // 10
if sum == num:
print(num,"is armstrong number.")
else:
temp = num
rev = 0
digit = 0
while temp > 0:
digit = temp % 10
rev = rev * 10 + digit
temp = temp // 10
if rev == num:
print(num,"is palindrome.")
else:
print(num,"does not belong to any of the three numbers(PERFECT, ARMSTRONG, PALINDROME)")
• Input a number and check if the number is a prime or composite number.
n=int(input("Enter a number:"))
k=0
if n==1:
print(n,"is neither a prime nor composite number")
else:
for i in range(2,n):
if n%i==0:
k+=1
if k>0:
print(n,"is a composite number")
else:
print(n,"is a prime number”)
• Display the terms of a Fibonacci series.
nterms=int(input("How many terms?:"))
n1, n2 = 0, 1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto", nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
• Compute the greatest common divisor and least common multiple of two integers.
def gcd(x,y):
if x == y:
return x
elif x > y:
return gcd(x-y,y)
else:
return gcd(x,y-x)
a,b=input("Enter the numbers:").split()
a,b=int(a),int(b)
print("The GCD is "+str(gcd(a,b)))
• Count display the number of vowels, consonants, uppercase, lowercase characters in
string.
str="Welcome to the world of PYTHON"
l,u,v=0,0,0
for i in str:
if (i>="a" and i<="z"):
l=l+1
if (i>="A" and i<="Z"):
u+=1
if i in "aeiouAEIOU":
v=v+1
print("Lowercase:",l)
print("Uppercase:",u)
print("Vowels:",v)
• Find the largest/smallest number in a list/tuple.
lst = []
num = int(input("How many numbers:"))
for i in range(num):
numbers = int(input("Enter number:"))
[Link](numbers)
print("Maximum element in the list is:",max(lst))
print("\nMinimum element in the list is:",min(lst))
• Input a list of numbers and swap elements at the even location with the
elements at the odd location.
lst = []
num = int(input("How many numbers:"))
for i in range(num):
numbers = int(input("Enter number:"))
[Link](numbers)
print("List before swapping",lst)
for i in range(0,num,2):
temp1=lst[i]
temp2=lst[i+1]
lst[i]=temp2
lst[i+1]=temp1
print("List after swapping",lst)
• Input a list/tuple of elements, search for a given element in the list/tuple.
list = []
num = int(input("How many numbers in the list:"))
for n in range(num):
numbers = int(input("Enter number:"))
[Link](numbers)
print("The entered list is :",list)
length = len(list)
element= int(input("Enter the elements to be searched for:"))
for i in range(0,length):
if element == list[i]:
print(element,"found at index:",i,"and position:",i+1)
break
else:
print(element,"is not found!!!")
• Create a dictionary with roll number, name and marks of n students in a class
and display the names of the students who have marks above 75.
result = {}
n = int(input("Enter number of students:"))
for i in range(n):
print("Enter details of student no.",i+1)
rno = int(input("Roll No.:"))
name = input("Name:")
marks = int(input("Marks:"))
result[rno]=[name,marks]
print(result)
for student in result:
if result[student][1]>75:
print(result[student][0])
• Write a program to input the value of x and n and print the sum of the following series:
➢ 𝟏 + 𝒙 + 𝒙𝟐 + 𝒙𝟑 + 𝒙𝟒 + ⋯ 𝒙𝒏
x = int(input("Enter the base number:"))
n = int(input("Enter the power:"))
sum = 1
for a in range(1,n+1):
sum = sum + x**a
print("Sum of the series is", sum)
➢ 𝟏 − 𝒙 + 𝒙𝟐 − 𝒙𝟑 + 𝒙𝟒 + ⋯ 𝒙𝒏
x=int(input(“Enter the base number:”))
n=int(input(“Enter the power:”)
sum = 1
for a in range(1,n+1):
sum = sum + ((-1) ** a)*(x**a)
print(“Sum of the series is”, sum)
𝒙𝟐 𝒙𝟑 𝒙𝟒 𝒙𝒏
➢ 𝒙+ − + −⋯
𝟐 𝟑 𝟒 𝒏
x=int(input(“Enter the base number:”))
n=int(input(“Enter the power:”))
sum = x
for i in range(2,n+1):
if (i % 2 == 0):
sum+=((x**i)/i)
else:
sum-=((x**i)/i)
print(“Sum of the series is,” sum)
𝒙𝟐 𝒙𝟑 𝒙𝟒 𝒙𝒏
➢ 𝒙 + 𝟐! − 𝟑! + − ⋯ 𝒏!
𝟒!
x=int(input(“Enter the base number:”))
n=int(input(“Enter the power:”))
sum = x
for i in range(2,n+1):
if (i % 2 == 0):
sum = sum + ((x**i)/[Link](i))
else:
sum = sum – ((x**i)/[Link](i))
print(“Sum of the series is”, sum)
• Input three numbers and display the largest/smallest number
no1 = int(input("Enter the 1st number:"))
no2 = int(input("Enter the 2nd number:"))
no3 = int(input("Enter the 3rd number:"))
if no1 > no2 and no1 > no3:
print(no1,"is the largest")
elif no2 > no1 and no2 > no3:
print(no2,"is the largest")
else:
print(no3,"is the largest")
no1 = int(input("Enter the 1st number:"))
no2 = int(input("Enter the 2nd number:"))
no3 = int(input("Enter the 3rd number:"))
if no1 < no2 and no1 < no3:
print(no1,"is the smallest")
elif no2 < no1 and no2 < no3:
print(no2,"is the smallest")
else:
print(no3,"is the smallest")
• Input a list of numbers and test if a number is equal to the sum of the cubes of
its digits. Find the smallest and largest such number from the given list of
numbers.
list = []
list1 = []
num = int(input("How many numbers in the list:"))
for n in range(num):
numbers = int(input("Enter number:"))
[Link](numbers)
print("The entered list is :",list)
for i in range(num):
sum = 0
temp = list[i]
order = len(str(list[i]))
while temp > 0:
digit = temp % 10
k = digit**order
sum = sum + k
temp//=10
if list[i] == sum:
[Link](list[i])
print("The largest number is:",max(list1))
print("The smallest number is:",min(list))