Python Practical Programs
Python Practical Programs
Program 1:
Input three numbers and display the largest and smallest number
Code:
a=int(input("Enter a number: "))
b=int(input("Enter a number: "))
c=int(input("Enter a number: "))
if a>b and a>c:
print(a,"is the greatest number")
elif b>a and b>c:
print(b,"is the greatest number")
else:
print(c,"is the greatest number")
if a<b and a<c:
print(a,"is the smallest number")
elif b<a and b<c:
print(b,"is the smallest number")
else:
print(c,"is the smallest number")
Output:
Enter a number: 4
Enter a number: 3
Enter a number: 2
4 is the greatest number
2 is the smallest number
Program 2:
Write a program to input a value x and n and print the sum of the series:
a)1+x+x2+x3+…..xn
b)1-x+x2-x3+….xn
c)x+(x2/2)-(x3/3)+(x4/4)…(xn/n)
d)x+(x2/2!)-(x3/3!)+(x4/4!)…(xn/n!)
Codes:
a)
x=float(input("Enter a number: "))
n=int(input("Enter the power: "))
s=0
for i in range(n+1):
s+=x**i
print("The sum of first",n,"terms is",s)
Output:
Enter a number: 2
Enter the power: 3
The sum of first 3 terms is 15.0
b)
x=int(input("Enter a number: "))
n=int(input("Enter the power: "))
s=0
sign=+1
for i in range(n+1):
term=x**i*sign
s+=term
sign*=-1
print("The sum of first",n,"terms is",s)
Output:
Enter a number: 5
Enter the power: 4
The sum of first 4 terms is 521
C)
x=int(input("Enter a number: "))
n=int(input("Enter the power: "))
s=x
sign=+1
for a in range(2,n+1):
term=((x**a)*sign)/a
s+=term
sign*=-1
print("Sum of first",n,"terms is",s)
Output:
Enter a number; 2
Enter the power; 3
Sum of first 3 terms is 1.33333333
d)
x=int(input("Enter a number: "))
n=int(input("Enter the power: "))
s=x
sign=+1
for a in range(2,n+1):
f=1
for i in range(1,a+1):
f*=i
term=((x**a)*sign)/f
s+=term
sign*=-1
print("Sum of first",n,"terms is",s)
Output:
Enter a number: 2
Enter the power: 3
Sum of first 3 terms is 2.666666666666667
Program 3:
Determine whether a number is a perfect number,Armstrong number or a
palindrome number
Code:
n = int(input("Enter a number: "))
_sum = 0
if _sum == n:
print(n, "is a perfect number")
else:
print(n, "is not a perfect number")
if n == _s:
print(n, "is an Armstrong number")
else:
print(n, "is not an Armstrong number")
if n == rev_no:
print(n, "is a palindrome number")
else:
print(n, "is not a palindrome number")
Output:
Enter a number: 407
407 is not a perfect number
407 is an Armstrong number
407 is not a palindrome number
Program 4:
Input a number and check if it is a prime number or composite number
Code:
n = int(input("Enter a number:"))
if n > 1:
is_prime = True
for i in range(2, n):
if n % i == 0:
is_prime = False
break
if is_prime:
print(n, "is a prime number")
else:
print(n, "is a composite number")
elif n == 0 or n == 1:
print(n, "is neither prime nor composite")
else:
print("Please enter a positive integer greater than or equal to 0")
Output:
Enter a number:7
7 is a prime number
Program 5:
Program to display n terms of a Fibonacci series
Code:
n=int(input("Enter the number of terms:"))
first=0
second=1
print(first,second,end=' ')
for i in range(0,n-2):
third=first+second
print(third,end=' ')
first=second
second=third
Output:
Enter the number of terms:10
0 1 1 2 3 5 8 13 21 34
Program 6:
Compute the GCD and LCM of 2 integers
Code:
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
vowel_count = 0
consonant_count = 0
uppercase_count = 0
lowercase_count = 0
vowels = "aeiouAEIOU"
if char.isupper():
uppercase_count += 1
else:
lowercase_count += 1
cleaned_string = ''
for c in input_string:
if c.isalnum() or c.isspace():
cleaned_string += c.lower()
if cleaned_string == cleaned_string[::-1]:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
Output:
Enter a string: level
The string is a palindrome.
Program 9:
Convert the case of the characters in a string. For example: if the input is
Good,then the output is gOOD
Code:
string = input("Enter a string: ")
output_string = ''