Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
38 views

Python Programs

The document contains examples of Python programs that demonstrate various programming concepts like calculating factorials, Fibonacci series, string palindrome, octal to decimal conversion, menu driven area calculation, list sum, arithmetic operations, random number generation, reading and writing files, character counting in a text file, and binary file operations.

Uploaded by

sandhya kumari
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Python Programs

The document contains examples of Python programs that demonstrate various programming concepts like calculating factorials, Fibonacci series, string palindrome, octal to decimal conversion, menu driven area calculation, list sum, arithmetic operations, random number generation, reading and writing files, character counting in a text file, and binary file operations.

Uploaded by

sandhya kumari
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

FACTORIAL OF A GIVEN NUMBER

def factorial(num):
fact=1
for i in range(1, num+1):
fact=fact*i
return fact

n=int(input("Please enter any number to find factorial: "))


result=factorial(n)
print("The factorial of", n ,"is:", result)

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")

4. OCTAL TO OTHER EQUIVALENT NUMBER CONVERSION USING FUNCTION


def oct2others(n):
print("passed octal number:",n)
numstring=str(n)
decNum=int(numstring,8)
print("Number in Decimal:",decNum)
print("Number in Binary:",bin(decNum))
print("Number in Hexadecimal:",hex(decNum))
num=int(input("Enter an octal number:"))

num = int(input("Enter an Octal Number: "))


oct2others(num)

5. CREATING A MENU DRIVEN PROGRAM TO FIND AREA OF CIRCLE,RECTANGLEAND TRIANGLE


def Circle(r):
Area=3.1428571*(r**2)
print("The Area of circle is: ",Area)
def Rectangle(L,B):
R=L*B
print("The Area of rectangle is: ",R)

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: "))

while (ch >=1 and ch <= 3):


if ch==1:
a=float(input("Enter the radius value: "))
Circle(a)
elif ch==2:
L=int(input("Enter the Length of the Rectangle: "))
B=int(input("Enter the Breadth of the Rectangle: "))
Rectangle(L,B)
elif ch==3:
B=int(input("Enter the Base of Triangle: "))
H=int(input("Enter the Height of Triangle: "))
Triangle(B,H)
print("\n\n\n")
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: "))

6. SUM OF ALL THE ELEMENTS OF A LIST


def sum_list(items):
sum=0
for i in items:
sum=sum+i
return sum

7. MENU DRIVEN PROGROM TO PERFOR M ARITHMETIC OPERATORS


lst=eval(input("Enter list items: "))
print("Sum of list items is : ",sum_list(lst))

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

8. RANDOM NUMBER GENERATION


import random

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!!")

9. READ A FILE LINE BY LINE


for line in file1:
word=line.split()
for i in word:
print(i,end='#')
10. COPYING LINES IN A FILE
fin=open("xiics2.txt","r")
fout=open("xiics4.txt","w")

for line in fin:


word=line.split( )
a=1
for i in word:
for letter in i:
if letter=='a' or letter=='A':
fout.write(line)
a=0
break
if a==0:
break

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)

12. CREATE AND SEARCHING RECORDS IN A BINARY FILE


import pickle
stud={}
stufile=open("stud.dat","wb")
ans='y'
while ans=='y':
rno=int(input("Enter roll number:"))
name=input("Enter name:")
mark1=int(input("Enter English mark:"))
mark2=int(input("Enter Maths mark:"))
mark3=int(input("Enter CS mark:"))
stud["Rollno"]=rno
stud["Name"]=name
stud["Mark1"]=mark1
stud["Mark2"]=mark2
stud["Mark3"]=mark3
pickle.dump(stud,stufile)
ans=input("Do u want to append more records?(y/n)...?")
stufile.close()

You might also like