100% found this document useful (1 vote)
244 views

Python Example Programs

The document contains Python programs to perform simple calculations, create a histogram, circulate list values, calculate distance between points, swap variable values, determine student grade, count words in a sentence, count characters in a string, check if a number is a palindrome, copy files, read from a file, and write to a file.

Uploaded by

esmani84
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
244 views

Python Example Programs

The document contains Python programs to perform simple calculations, create a histogram, circulate list values, calculate distance between points, swap variable values, determine student grade, count words in a sentence, count characters in a string, check if a number is a palindrome, copy files, read from a file, and write to a file.

Uploaded by

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

E.

SUBRAMANIAN AMET UNIVERSITY

PYTHON PROGRAMS

MAKE SIMPLE CALCULATOR

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

CIRCULATE THE VALUES OF N VARIABLES


no_of_terms = int(input("Enter number of values : "))
list1 = [ ]
for val in range(0,no_of_terms,1):
ele = int(input("Enter integer : "))
list1.append(ele)
print("Circulating the elements of list ", list1)
for val in range(0,no_of_terms,1):
ele = list1.pop(0)
list1.append(ele)
print(list1)

DISTANCE BETWEEN TWO POINTS


import math

print("Enter coordinates for Point 1 : ")


x1 = int(input("x1 = "))
y1 = int(input("y1 = "))
print("Enter coordinates for point 2 : ")
x2 = int(input("x2 = "))
y2 = int(input("y2 = "))
dist = math.sqrt( ((x2-x1)**2) + ((y2-y1)**2) )
print("Distance between given points is", round(dist,2))

SWAP THE VALUES OF TWO VARIABLES


num1 = int(input("Enter a number : "))
num2 = int(input("Enter a number : "))
print("Values of variables Before swap :")
print("Number 1 :",num1)
print("Number 2 :",num2)
num1, num2 = num2, num1
print("Values of variables After swap :")
print("Number 1 :",num1)
print("Number 2 :",num2)

CALCULATE GRADE OF STUDENT

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

COUNT WORD IN SENTENCE

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

CHECK PALINDROME NUMBER OR NOT

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy