20201221-20201220-Python Lab Manual
20201221-20201220-Python Lab Manual
20201221-20201220-Python Lab Manual
(Autonomous)
Maisammaguda, Dhulapally (post & via Kompally), Secunderabad-500 100.
Department of
Information technology
Now you can type any valid python expression at the prompt.
Python reads the typed expression, evaluates it and prints the result.
Program:
Program:
import sys
a=int(sys.argv[1])
b=int(sys.argv[2])
c=a+b
print "The sum is",c
Output:
Exercise - 3 Control Flow
a) Write a Program for checking whether the given number is an
even number or not.
Program:
a=input("Enter the number: ")
if a%2==0:
print a,"is EVEN"
else:
print a,"is NOT EVEN"
Output-1:
Enter the number: 15
15 is NOT EVEN
Output-2:
Enter the number: 24
24 is EVEN
b) Using a for loop, write a program that prints out the decimal
equivalents of 1/2, 1/3, 1/4, . . . , 1/10
Program:
i=1
for i in range(1,11):
print "Decimal equivalent value for 1/",i," is",1/float(i)
Output:
Decimal equivalent value for 1/1 is 1.0
Decimal equivalent value for 1/2 is 0.5
Decimal equivalent value for 1/3 is 0.333333333333
Decimal equivalent value for 1/4 is 0.25
Decimal equivalent value for 1/5 is 0.2
Decimal equivalent value for 1/6 is 0.166666666667
Decimal equivalent value for 1/7 is 0.142857142857
Decimal equivalent value for 1/8 is 0.125
Decimal equivalent value for 1/9 is 0.111111111111
Decimal equivalent value for 1/10 is 0.1
Hint: Represent a ball on a plane as a tuple of (x, y, r), r being the radius
If (distance between two balls centers) <= (sum of their radii) then (they
are colliding)
Program:
import math
def ball_collide(x1,y1,r1,x2,y2,r2):
status=False
d=math.sqrt((x2-x1)**2-(y2-y1)**2)
r=r1+r2
if(d<r):
status=True
else:
status=False
return status
s=ball_collide(1,2,4,111,102,3)
print "Balls Collision is",s
s=ball_collide(1,2,2,101,102,3)
print "Balls Collision is",s
Output:
Balls Collision is False
Balls Collision is True
b) Find mean, median, mode for the given set of numbers in a list.
Program:
def mean(a):
s=sum(a)
m1=float(s)/len(a)
print "Mean is",m1
def median(a):
a.sort( )
n=len(a)
if n%2==0:
m2=float((a[n/2]+a[(n-1)/2])/2)
else:
m2=b[(n-1)/2]
print "Median is",m2
def mode(a):
s1=set()
uniq=[ ]
for x in a:
if x in s1:
uniq.append(x)
s1.add(x)
print "Mode is",uniq
lst=[1,1,2,2,3,4,5,6]
mean(lst)
median(lst)
mode(lst)
Output:
Mean is 3.0
Median is 2.0
Mode is [1, 2]
Exercise - 9 Functions - Continued
a) Write a function nearly_equal to test whether two strings are
nearly equal. Two strings a and b are nearly equal when a can be
generated by a single mutation on b.
Program:
def mutate(word):
out_list = []
letters = 'abcdefghijklmnopqrstuvwxyz'
#insert a character
for i in range(len(word) + 1):
for j in range(26):
out_list.append(word[:i] + letters[j] + word[i:])
#deleting a character
for i in range(len(word)):
out_list.append(word[:i] + word[i + 1:])
#replace a character
for i in range(len(word)):
for j in range(26):
out_list.append(word[:i] + letters[j] + word[i + 1:])
#swapping a characters
current_word = []
out_word = ''
for i in range(len(word) - 1):
for j in range(i + 1, len(word)):
#converting string into list
cword = list(word)
#Swapping of characters in a list
cword[i], cword [j] = cword [j], cword [i]
#converting list into string
str1="".join(current_word)
out_list.append(str1)
return out_list
Output-1:
Enter First Word: "welcome"
Enter Second Word: "welcoe"
True
Output-2:
Enter First Word: "welcome"
Enter Second Word: "welcoome"
True
Output-3:
Enter First Word: "welcome"
Enter Second Word: "welcometoyou"
False
Output:
Duplicate elements are set([1, 2, 4, 5])
c) Write a function unique to find all the unique elements of a list.
Program:
def uni(a):
s=set()
d=set()
u=set()
for i in a:
if i in s:
d.add(i)
else:
s.add(i)
for i in a:
if i not in d:
u.add(i)
print "unique elements are",u
uni([1,1,2,3,4,5,5,4,7,2])
Output:
unique elements are set([3, 7])
Exercise - 10 - Functions - Problem Solving
else:
print "Addition is NOT Possible"
Output:
Enter A Matrix Rows:3
Enter A Matrix Columns:3
Enter value:1
Enter value:2
Enter value:3
Enter value:4
Enter value:5
Enter value:6
Enter value:7
Enter value:8
Enter value:9
a= [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Enter B Matrix Rows:3
Enter B Matrix Columns:3
Enter value:4
Enter value:5
Enter value:6
Enter value:7
Enter value:8
Enter value:9
Enter value:1
Enter value:2
Enter value:3
b= [[4, 5, 6], [7, 8, 9], [1, 2, 3]]
5 7 9
11 13 15
8 10 12
b) Write a script that imports requests and fetch content from the
page. Eg. (Wiki)
import requests
r = requests.get('https://www.google.com/')
print r.status_code
print r.headers['content-type']
Output:
200
text/html; charset=ISO-8859-1
Program: 12c.py
from flask import Flask,render_template
app = Flask( name )
print(app)
@app.route("/")
def main():
return render_template('w3.html')
if name == " main ":
app.run(host="127.0.0.1" ,port=2500)
Program: w3.html
<!DOCTYPE html>
<html>
<body>
<a href="https://www.google.com">This is a link</a>
</body>
</html>
Run the 12c.py python program
Click The Link in the shown and it will goto website link
d) Describe about Instance variable using ATM Machine Class
Program:
class ATM:
def init (self):
self.balance=0 #instance Variable print "New
Account Created"
def deposit(self):
amount=input("Enter the amount to deposit: ")
self.balance=self.balance+amount
print "New Balance is",self.balance def
withdraw(self):
amount=input("Enter the amount to withdraw: ") if
self.balance<amount:
print "Insufficient Balance" else:
self.balance=self.balance-amount print "New
Balance is",self.balance
def enquiry(self):
print "The Balance is",self.balance a=ATM()
a.deposit() a.withdraw()
a.withdraw() a.enquiry()
Output: