Python Programs
Python Programs
def factorial(num):
fact=1
for i in range(1, num+1):
fact=fact*i
return fact
2. FIBONACCI SERIES
n = int(input("Enter the value of n: "))
a = 0
b = 1
sum = 0
count = 1
print("Fibonacci Series: ", end = " ")
while(count <= n):
print(sum, end = "\n")
count += 1
a = b
b = sum
sum = a + b
3. STRING PALINDROME
def isPalindrome(str):
for i in range(0, int(len(str)/2)):
if str[i]!=str[len(str)-i-1]:
return False
else:
return True
s=input("Enter string:")
ans = isPalindrome(s)
if (ans):
print("The given string is Palindrome")
else:
print("The given string is not a Palindrome")
def Triangle(B,H):
R=0.5*B*H
print("The Area of triangle is: ",R)
print("1.Area of Circle")
print("2.Area of Rectangle")
print("3.Area of Triangle")
print("Exit (Type any other number)")
ch=int(input("Enter your choice: "))
def sum(a,b):
c=a+b
return c
def subt(a,b):
c=a-b
return c
def mult(a,b):
c=a*b
return c
def divi(a,b):
c=a/b
return c
print("******************")
print("ARITHMETIC OPERATIONS")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("Exit (Any other number)")
while True:
opt=int(input ("Enter your choice: "))
if opt==1:
a=int(input("Enter the first number: "))
b=int(input ("Enter the second number: "))
res=sum(a,b)
print("The sum is ",res)
elif opt==2:
a=int(input("Enter the first number: "))
b=int(input ("Enter the second number: "))
res=subt(a,b)
print("The difference is ",res)
elif opt==3:
a=int(input("Enter the first number: "))
b=int(input ("Enter the second number: "))
res=mult(a,b)
print("The product is ",res)
elif opt==4:
a=int(input("Enter the first number: "))
b=int(input ("Enter the second number: "))
res=divi(a,b)
print("The remainder is ",res)
else:
print("Exiting...")
break
n = random.randrange(1,10)
guess = int(input("Enter any number between 1 & 10: "))
while n!= guess:
if guess < n:
print("Too low")
guess = int(input("Enter any number again: "))
elif guess > n:
print("Too high!")
guess = int(input("Enter number again: "))
else:
break
print("you guessed it right!!")
fin.close( )
fout.close( )
11. Write a Python program to read a text file and display the number of
vowels/consonants/uppercase/lowercase characters in the file
file=open("AI.TXT","r")
content=file.read()
vowels=0
consonants=0
lower_case_letters=0
upper_case_letters=0
for ch in content:
if(ch.islower()):
lower_case_letters+=1
elif(ch.isupper()):
upper_case_letters+=1
ch=ch.lower()
if (ch in ['a','e','i','o','u']):
vowels+=1
elif(ch in
['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z
']):
consonants+=1
file.close()
print("Vowels are :",vowels)
print("Consonants :",consonants)
print("Lower_case_letters :",lower_case_letters)
print("Upper_case_letters :",upper_case_letters)