Python Lab Manaual
Python Lab Manaual
PYTHONLAB MANUAL
Regulation: R20
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
VISION
To become a prominent knowledge hub for learners, strive for education excellent with
innovative and industrial techniques so as to meet the global needs.
MISSON
LIST OF PEOs
List of PSOs
Analyze and develop computer programs in the areas related to system software,
multimedia, web design and networking for efficient design of computer based
PSO1
system of varying complexity basic engineering sciences and Computer
fundamentals.
CONTENTS
"""
2. Write a program that asks the user to enter three
numbers (use three separate input statements). Create variables
called total and average that hold the sum and average of the
three number and print out the values of total and average.
"""
a=int (input("enter value of a: "))
b=int (input("enter value of b: "))
c=int (input("enter value of c: "))
Total = (a+b+c)
Average = Total/3
print ("sum of three numbers is ", Total)
print ("Average of three numbers is ", Average)
print ("sum of three numbers is %d and Average of three numbers
is %0.2f" %(Total, Average))
"""
3.Write a program that users a for loop to print the numbers
8,11,14,17,20,…,83,86,89.
"""
for i in range(8,90,3):
print( i,end=' ')
"""
4.Write a program that asks the user for their name and how many
times to print it.
The program should print out the user’s name the specified no of
times
"""
username=input("enter your name")"""
"""
6.Generate a random number between 1 and 10. Ask the user to
guess the number and print a message based on
whether they get it right or not.
"""
import random as ra
random_num = ra.randint(1,10)
guess = int(input('Guess a number between 1 and 10 until you get
it right :'))
while random_num != guess:
print("try again")
guess = int(input('Guess a number between 1 and
10 until you get it right: '))
print('Well guessed! ')
"""
7.Write a program that asks the user for two numbers and prints
close if the numbers are within.
001 of each other and not close otherwise
"""
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
if abs(num1 - num2) <= 0.001:
print("close")
else:
print("not close")
"""
8.Write a program that asks the user to enter a word and prints
out whether that word contains any vowels.
"""
vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'I', 'o', 'u']
a=input("enter word")
c=0
for i in a:
if(i in vowels):
c=c+1
if(c!=0):
print("string contains vowels")
else:
print("string dosent contains vowels")
"""
9.Write a program that asks the user to enter two string of the
same length. The program should then check to see if the strings
are of the same length. If they are not, the program should
print and appropriate message and exit. If they are of same
length, the program should alternate the characters of the two
strings. For example,
if the user enters abcde and ABCDE the program should print out
AaBbCcDdEe.
"""
"""
10.Write a program that asks the user for a large integer and
inserts commas into it according to the standard American
convention for commas in large numbers.
For instance, if the user enters 1000000, the output should be
1,000,000.
"""
print('Enter the number')
n=int(input( ))
print("{:,}".format(n))
print(list[op])
"""op=int(input("Choose Options from Above"))
if(op==0):
Inches=n*12
print("Feet to Inches value is",Inches)
if(op==1):
Yards=n*0.33333
print("Feet to Yards value is ",Yards)
if(op==2):
Miles = n*0.00018939
print("Feet to Miles value is ",Miles)
if(op==3):
MM=n* 304.8
print("Feet to millimeters value is ",MM)
if(op==4):
CM=n*30.48
print("Feet to centimeters value is",CM)
if(op==5):
meters=n/3.2808
print("Feet to meters value is",round(meters,3))
if(op==6):
KM=n/3280.8
print("Feet to kilometers value is",KM)
"""
lenOne = len(strOne)
lenTwo = len(strTwo)
if lenOne==lenTwo:
i = 0
chk = 0
while i<lenOne:
if strOne[i] != strTwo[i]:
chk = 1
break
i = i+1
if chk==0:
print("\nStrings are Equal")
else:
print("\nStrings are Not Equal")
else:
print("\nStrings are Not Equal")
"""24.Write a program that asks the user for a word and finds
all the
smaller words that can be made from the letters of that word.
The number of occurrences of a letter in a smaller word cant
exceed the
number of occurrences of the letter in the users word.
"""
from itertools import permutations
s=input('Enter a word:::')
for i in range (2,len(s)):
for p in permutations(s,i):
print(' '.join(p), end =' ')
fo=open("sample.txt","r+")
str=fo.read()
print("Read String is: ",str)
lst=re.findall('\S+@\S+',str)
print(";"+lst[0]+";")
fo.close()
fo= open("temps.txt")
f1= open("ftemps.txt","w+")
for i in fo:
c=float(i)
F=(9*c + (32*5))/5
f1.write(str(F)+"\n")
f1.close( )
f0.close( )
"""27. Write a class called product. The class should have
fields called name, amount, and price, holding the products
name, the number of items of that product in stop, and the
regular price of the product. There should be a method get_price
that receives the number of items to be both and returns a the
cost of buying that many items, where the regular price is
changed for orders of less than 10 items, a 10% discount is
applied for orders of between 10 and 99 items, and a 20%
discount is applied for orders of 100 or more items. There
should also be a method called make_purchase that receives the
number of items to be bought and decreases amount by that much.
"""
class Product:
def __init__(self,name,amount,price):
self.name=name
self.amount=amount
self.price=price
def get_price(self, number_items):
if number_items<10:
return self .price*number_items
elif 10 <=number_items < 100:
return 0.9*self.price*number_items
else:return 0.8*self.price*number_items
q1=4
print(f'cost for {q1} {shoes.name} = {shoes.get_price(q1)}')
shoes.make_purchase(q1)
print(f'remaining stock: {shoes.amount}\n')
q2=12
print(f'cost for {q2} {shoes.name} = {shoes.get_price(q2)}')
shoes.make_purchase(q2)
print(f'remaining stock: {shoes.amount}\n')
q3=112
print(f'cost for {q3} {shoes.name} = {shoes.get_price(q3)}')
shoes.make_purchase(q3)
print(f'remaining stock: {shoes.amount}\n')
def convert_to_minutes(seconds):
seconds = seconds % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
# Driver program
n = int(input("Enter seconds"));
print(convert_to_minutes(n))
class Converter:
def __init__(self, n,name):
self.value = n
self.name=name
def FeetToInches(self):
Inches=self.value*12
print("Feet to Inches value is ", Inches)
def milestokm(self):
km=self. value/0.62137
print("miles to KM is ", km)
def FeetToYards(self):
Yards= self.value * 0.3333
print("Feet to Yards value is ", Yards)
def FeetToMiles(self):
Miles= self.value * 0.00018939
print("Feet to Miles value is ", Miles)
x.FeetToInches( )
x.milestokm( )
x.FeetToYards( )
x.FeetToMiles( )
"""32. Write a program that open a file dialog that allows you
to select a text file. The program then displays the contents of
the file in a textbox.
"""
from tkinter import *
from tkinter.filedialog import *
from tkinter.scrolledtext import ScrolledText
#lab program 2 type
root = Tk( )
root.title('File dialog')
textbox = ScrolledText()
textbox.grid( )
filename=askopenfilename(initialdir='c:\python_code\examples',
filetypes=[('Text files', '.txt'), ('All files', '*')])
s = open(filename).read( )
textbox.insert(1.0, s)
mainloop( )
try:
print('try block')
x=int(input("Enter a number:"))
y=int(input("Enter another number:"))
z=x/y
expect ZeroDivisionError:
print("expect ZeroDivisionError block")
print("Division by not accepted")
else:
print("else block")
print("Division = ", z)
finally:
print("finally block")
print ("Out of try, except, else and finally blocks.")
Output1:
try block
Enter a number:100
Enter another number:10
else block
Division = 10.0
finally block
out of try, except, else and finally blocks.
Output2:
try block
Enter a number:100
Enter another number:0
expect ZeroDivisionError block
Division by not accepted
finally block
Out of try, except, else and finally blocks.
with/as
# file handling
#1)without using with statement
file=open('123.txt', 'w')
file.write('hello world')
file.close( )
#2)with try and finally
File=open('123.txt', 'w')
try:
file.write('hello world')
finally:
file.close( )
#3)using with statement
with open('123.txt', 'w') as file:
file.write('hello world')