Python Practical Program-Xi
Python Practical Program-Xi
Write a python program to input a welcome message and display it. 21-09-22
1 message=input("Welcome to MBS PS, Bhubaneswar, Computer Science Lab. : ")
print("Hello, ",message)
Write a python program to input two numbers and display the larger / smaller 22-09-22
number.
Write a python program to input three numbers and display the largest / smallest 24-09-22
number.
9 Write a program to input the value of x and n and print the sum of the following 05-11-22
series:
1) 1 + x² + x³ + … + xⁿ
Output: Sum of series in python
The output shell python window will be appeared like below:-
10 09-11-22
for a in range (n + 1) :
if a%2==0:
s += x**a
else:
s -= x**a
print ("Sum of series", s)
Output : Sum of series
11 10-11-22
for a in range (n + 1) :
if a%2==0:
s += (x**a)/n
else:
s -= (x**a)/n
print ("Sum of series", s)
Output : Sum of Series Program :-
12 x – x2/2! + x3/3! – x4/4! + x5/5! – x6/6! 12-11-22
PALINDROME NUMBER
temp = int(input(“Enter a number”))
rev = 0 #reverse is 0
while n > 0: #if n is greater than 0 this loop runs
dig = n % 10
rev = rev * 10 + dig #This loop calculates the reverse and matches it with input
n = n // 10
if temp == rev:
print(temp, “The number is a palindrome!”)
else:
print(temp, “The number isn’t a palindrome!”)
ARMSTRONG NUMBER
count = 0
temp = int(input(“Enter a number”))
while temp > 0:
digit = temp % 10
count += digit ** 3
temp //= 10 # Armstrong number is a number that is equal to the sum of cubes of
its digits
if n == count:
print(n, “is an Armstrong number”)
else:
print(n, “is not an Armstrong number”)
Write a program to input a number and check if the number is a prime or 17-11-22
composite number.
# Number to be checked for prime
n=5
if n > 1:
14
for i in range(2, int(n/2)+1):
if (n % i) == 0:
print(num, "is not a prime number")
break
else:
print(n, "is a prime number")
# If the number is less than 1, its also not a prime number.
else:
print(n, "is not a prime number")
Write a python program to compute the greatest common divisor and least 23-11-22
common multiple of two integers.
# python program to find LCM of two number using GCD
#input two numbers
n1 = int(input("Enter First number :"))
n2 = int(input("Enter Second number :"))
x = n1
y = n2
16 while(n2!=0):
t = n2
n2 = n1 % n2
n1 = t
gcd = n1
print("GCD of {0} and {1} = {2}".format(x,y,gcd))
lcm = (x*y)/gcd
print("LCM of {0} and {1} = {2}".format(x,y,lcm))
Write a program to count and display the number of vowels, consonants, 24-11-22
uppercase, lowercase characters in the string.
s = input("Enter any string :")
vowel = consonent = uppercase = lowercase= 0
for i in s:
if(i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'or i == 'A' or i == 'E' or i == 'I' or i
== 'O' or i == 'U'):
vowel = vowel +1
else:
17 consonent = consonent + 1
if i.isupper() :
uppercase = uppercase + 1
if i.islower():
lowercase = lowercase + 1
print("Total number of vowel:",vowel)
print("Total number of consonent:",consonent)
print("Total number of uppercase letter:",uppercase)
print("Total number of lowercase letter:",lowercase)
Input a string and determine whether it is palindrome or not; convert the case of 26-11-22
characters in a string.
# Program to check if a string is palindrome or not
my_str = 'aIbohPhoBiA'
# make it suitable for caseless comparison
my_str = my_str.casefold()
# reverse the string
18
rev_str = reversed(my_str)
# check if the string is equal to its reverse
if list(my_str) == list(rev_str):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")