Python Lab
Python Lab
TEMPERATURE CONVERSION
#PROGRAM TO CONVERT TEMPERATURE FROM FARENHEIT TO CELCIUS AND
VICEVERSA
ch=int(input("enter choice:"))
if ch==1:
celcius=round((faren-32)*(5/9),2)
print(str(celcius)+"c"elif)
ch==2:
faren=round(celcius*(9/5)+32,2)
print(str(faren)+"f")
else:
###################################################################
####
12.78c
190.4f
invalid choice
Print(################################################################)
for i in range(n):
for j in range(n-1,0,-1):
################################################################
*
**
***
****
*****
****
***
**
*
3. CALCULATE TOTAL MARKS, PERCENTAGE AND GRADE
OF A STUDENT
A STUDENT”)
print(“###################################################################”)
mark5:")) total=m1+m2+m3+m4+m5
per=(total/500)*100
per>=70:
per>=60:
per>=40:
print("Grade is E")
OUTPUT
PROGRAM TO CALCULATE TOTAL MARKS, PERCENTAGE AND GRADE OF A
STUDENT
###################################################################
*******************STUDENTS MARKS
enter mark2:98
enter mark3:96
enter mark4:99
enter
mark5:100
Total percentage:
97.6 Grade is A
4. Area of Different shapes
print("**************")
if choice==1:
area=l*w
elif choice==2:
square:")) area=a*a
elif choice==3:
cicle:")) area=3.14*r*r
area=0.5*b*h
else:
print("wrong input...")
OUTPUT
**************
1. rectangle
2. square
3. circle
4. triangle
**************
1. rectangle
2. square
3. circle
4. triangle
**************
1. rectangle
2. square
3. circle
4. triangle
**************
1. rectangle
2. square
3. circle
4. triangle
(2,numerator): if numerator%
denominotor==0: break
else:
print(numerator,end=",")
OUTPUT
if m==0 or
m==1: return
elif m >1:
return m* fact(m-
1) else:
return 0
print (n,"factorial=",fact(n))
OUTPUT
list1=[5,20,15,60,40,111,12]
even_count,odd_count=0,0
num=0
while(num<len(list1)):
if list1[num]%2==0:
even_count+=1 else:
odd_count+=1
num+=1
3
8. WRITE A PYTHON PROGRAM TO REVERSE A STING
WORD BY WORD
class reverse:
def rev_sentence(self,sentence):
words=sentence.split(' ')
reverse_sentence=' '.join(reversed(words))
print(reverse_sentence)
c=reverse()
#tup=('a','a','b','c','d','d')
lst=['a','b']
x=list(tup)
c=0
for i in lst:
c+=x.count(i)
print(c)
OUTPUT
3
10. Create a saving account class that behaves just like a bank
account, but also has an interest rate and a method that
increases the balance by the appropriate amount of interest
class bank_account:
def _init_(self):
self.balance=0
def deposit(self):
self.balance=+amount
print("\nAmount deposited:",amount)
def withdraw(self):
withdrawn:")) if self.balance>=amount:
self.balance-=amount print("\nyou
withdraw:",amount)
else:
def display(self):
s=bank_account()
s.deposit()
s.withdraw()
s.display(
OUTPUT
f.write("hi\n")
f.write("welcome to\n")
f.write("python\n")
f.write("programming\n")
f.close()
print("created file:\n")
f=open("myfile.txt","r")
print(f.read())
f.close()
fn=open('myfile.txt','r')
fn1=open('outfile.txt','w')
cont=fn.readlines() type(cont)
for i in range(0,len(cont)):
if((i+1)%2!=0):
fn1.write(cont[i])
else:
pass
fn1.close()
fn1=open('outfile.txt','r')
cont1=fn1.read()
print(cont1)
fn.close()
fn1.close()
OUTPUT
created file:
hi
welcome to
python
programmin
file: hi
python
12. TURTLE GRAPHICS
import turtle
turtle.setup(300,300)
turtle.colormode(255)
window=turtle.Screen()
my_pen=turtle.Turtle()
my_pen.pencolor('red')
for i in range(50):
my_pen.forward(50)
my_pen.right(144)
turtle.done()
OUTPUT
13. Program for Towers of Hanoi using recursion
def TowerOfHanoi(n,source,destination,auxilliary):
if n==1:
return
TowerOfHanoi(n-1,source,auxilliary,destination)
TowerOfHanoi(n-1,auxilliary,destination,source)
n=3
TowerOfHanoi(n,'A','B','C')
OUTPUT
Move disk 1 from source A to destination B
index=word[0:1].upper() if
index in my_Dict.keys():
if word in my_Dict[index]:
print(my_Dict[index][word])
else:
else:
name?")
print("Good Luck!",name)
words=['rainbow','computer','science','programming','python','mathematics','player','condition','reverse','water',
'board', 'geeks']
word=random.choice(words)
failed = 0
else:
print("_") failed+=1
if failed==0:
print("You Win")
break
print("You have",+turns,'more
print("You Loose")
OUTPUT
guess a character:c
Worng
guess a character: r r
_
_
guess a character: a r
guess a character:i
Worng
ai
guess a character: n r
ai
n
guess a character: b r
ainb
guess a character: o r
ainb
guess a character:w r
ainb
o
You Win
1000
17. To generate Floyd Triangle in python
num = 1
for i in range(5):
for j in range(i+1):
print(num, end="")
num = num+1
print()
OUTPUT
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15 16
18. To find the largest of three numbers
else:
24 is the largest
19. To concatenate two
strings using string
functions.
str1 = 'Hello'
str2 =
'World'
str2 print(result)
Output
‘Hello World