Python Example Programs
Python Example Programs
Python Example Programs
PYTHON PROGRAMS
while True:
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Exit")
choice = int(input("Enter your choice: "))
if (choice>=1 and choice<=4):
print("Enter two numbers: ")
num1 = int(input())
num2 = int(input())
if choice == 1:
res = num1 + num2
print("Result = ", res)
elif choice == 2:
res = num1 - num2
print("Result = ", res)
elif choice == 3:
res = num1 * num2
print("Result = ", res)
else:
res = num1 / num2
print("Result = ", res)
elif choice == 5:
break
else:
print("Wrong input..!!")
HISTOGRAM
def histogram( items ):
for n in items:
output = ''
times = n
while( times > 0 ):
output += '*'
times = times - 1
print(output)
histogram([2, 3, 6, 5])
1|Page
E. SUBRAMANIAN AMET UNIVERSITY
while True:
print("Enter 'x' for exit.")
print("Enter marks obtained in 5 subjects: ")
mark1 = int(input())
mark2 = int(input())
2|Page
E. SUBRAMANIAN AMET UNIVERSITY
mark3 = int(input())
mark4 = int(input())
mark5 = int(input())
if mark1 == 'x':
break
else:
sum = mark1 + mark2 + mark3 + mark4 + mark5
average = sum/5
if(average>=91 and average<=100):
print("Your Grade is A+")
elif(average>=81 and average<=90):
print("Your Grade is A")
elif(average>=71 and average<=80):
print("Your Grade is B+")
elif(average>=61 and average<=70):
print("Your Grade is B")
elif(average>=51 and average<=60):
print("Your Grade is C+")
elif(average>=41 and average<=50):
print("Your Grade is C")
elif(average>=0 and average<=40):
print("Your Grade is F")
else:
print("Strange Grade..!!")
while True:
print("Enter 'x' for exit.")
string = input("Enter any string: ")
if string == 'x':
break
else:
word_length = len(string.split())
print("Number of words =",word_length,"\n")
COUNT CHARACTER IN STRING
while True:
print("Enter 'x' for exit.")
string = input("Enter any string: ")
if string == 'x':
break
3|Page
E. SUBRAMANIAN AMET UNIVERSITY
else:
char = input("Enter a character to count: ")
val = string.count(char)
print(val,"\n")
while True:
print("Enter 'x' for exit.")
num = input("Enter any number: ")
if num == 'x':
break
else:
number = int(num)
orig = number
rev = 0
while number > 0:
rev = (rev*10) + number%10
number //= 10
if orig == rev:
print(orig, "is a Palindrome Number.\n")
else:
print(orig, "is not a Palindrome Number.\n")
COPYING FILES
from shutil import copyfile
while True:
print("Enter 'x' for exit.")
sourcefile = input("Enter source file name: ")
destinationfile = input("Enter destination file name: ")
if sourcefile == 'x':
break
else:
copyfile(sourcefile, destinationfile)
print("File copied successfully!")
print("Want to display the content ? (y/n): ")
check = input()
if check == 'n':
break
else:
c = open(destinationfile, "r")
4|Page
E. SUBRAMANIAN AMET UNIVERSITY
print(c.read())
c.close()
print()
print()
READ A FILE
while True:
print("Enter 'x' for exit.")
filename = input("Enter file name (with extension) to read: ")
if filename == 'x':
break
else:
c = open(filename, "r")
print("\nThe file,",filename,"opened successfully!")
print("The file contains:")
print(c.read())
c.close()
print()
WRITE TO FILE
while True:
print("Enter 'x' for exit.")
filename = input("Enter file name to create and write content: ")
if filename == 'x':
break
else:
c = open(filename, "w")
print("\nThe file,",filename,"created successfully!")
print("Enter 3 sentences to write on the file: ")
sent1 = input()
sent2 = input()
sent3 = input()
c.write(sent1)
c.write("\n")
c.write(sent2)
c.write("\n")
c.write(sent3)
c.close()
print("Content successfully placed inside the file.!!\n")
5|Page