Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Python Lab

python lab

Uploaded by

Bala Siva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Python Lab

python lab

Uploaded by

Bala Siva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

PROGRAM #1 TEMPERATURE CONVERSION

print("convertion of temperature")
print("\n 1.convert from celsius to farenheit \n 2.convert from farenheit to
celsius\n")
user=int(input("enter your choice :"))
if(user==1):
c=float(input("enter temperature in celsius : "))
f=(c*1.8)+32
print("temperature in farenheit = ",f)
else:
f=float(input("enter temperature in farenheit : "))
c=(f-32)/1.8
print("temperature in celsius = ",c)
OUTPUT :
PROGRAM #2 PRINTING PATTERN

n=5
for i in range(n):
print(' '*(n-i-1)+'* '*(i+1))
for i in range(n-1):
print(' '*(i+1)+'* '*(n-i-1))

OUTPUT :
PROGRAM #3
CALCULATE TOTAL MARKS,PERCENTAGE AND GRADE
OF A STUDENT
py='student mark database'
print(py.center(60,'*'))
s1=int(input("enter marks of the first subject:"))
s2=int(input("enter marks of the second subject:"))
s3=int(input("enter marks of the third subject:"))
s4=int(input("enter marks of the fourth subject:"))
s5=int(input("enter marks of the fifth subject:"))
total=(s1+s2+s3+s4+s5)
avg=total/5
print('\n total marks:',avg)
if(avg>80):
print("grade:A")
elif(avg>70 and avg<80):
print("grade:B")
elif(avg>60 and avg<70):
print("grade:c")
elif(avg>40 and avg<60):
print("grade:D")
else:
print("grade:E")
OUTPUT:
PROGRAM NO #4
FIND THE AREA OF RECTANGLE , SQUARE, CIRCLE AND
TRIANGLE

print("n1.area of rectangle\n2.area of sqaure\n3.area of circle \n4. area of


triangle\n")
shape=float(input("enter your choice:"))
if(shape==1):
len=float(input("enter the length:"))
bre=float(input("enter the breath"))
area=len*bre
print("area of rectangle",area)
elif(shape==2):
side=float(input("enter length of side:"))
area=side*side
print("area of sqaue",area)
elif(shape==3):
import math
r=float(input("enter radius of circle:"))
area= math.pi*r*r
print("area of circle",area)
elif(shape==4):
base=float(input("enter the base:"))
height=float(input("enter the height:"))
area=0.5*base*height
print("area of triangle",area)
else:
print("invalid choice")
OUTPUT:
PROGRAM #5 PRIME NUMBERS

for num in range(20):


if(num>1):
for i in range(2,num):
if(num%i)==0:
break
else:
print(num)

OUTPUT:
PROGRAM #6 FACTORIAL

def fact(n):
if(n==0 or n==1):
return 1
else:
return n*fact(n-1)
n=int(input("enter a number :"))
if(n<0):
print("the factorial does not exist")
else:
print("the factorial of ",n,"is ",fact(n))

OUTPUT :
PROGRAM #7 COUNTING ODD AND EVEN NUMBER

a=[ ]
for i in range(6):
a.append(int(input("enter a element :")))
print(a)
odd=0
even=0
for i in a:
if(i%2==0):
even+=1
else:
odd+=1
print("there are ",odd,"odd number in the list")
print("there are ",even,"even number in the list")

OUTPUT :
PROGRAM # 8 REVERSE A STRING WORD BY WORD

str1=input("enter a sentence: ")


words=str1.split()
rev_words=words[::-1]
sen=" ".join(rev_words)
print(sen)

OUTPUT:
PROGRAM #9 COUNT OCCURRENCES OF LIST ITEMS IN
TUPLE

def count_occurence(t1,l1):
dict={item:0 for item in l1}
for item in t1:
if item in dict:
dict[item]+=1
total_count=sum(dict.values())
return total_count
t=('a','a','c','b','d')
l=['a','b']
output=count_occurence(t,l)
print(l,"occured ", output," times in ",t)

OUTPUT:
PROGRAM #10 CREATING BANK ACCOUNT USING
INHERITANCE

class bankaccount:
def _int_(self, account_number, account_holder,balance):
self.account_number = account_number
self.account_holder = account_holder
self.balance = balance
def deposit(self,amount):
self.balance += amount
return f"deposited ${amount}.new balance: ${self.balance}"
def withdraw(self,amount):
if amount <= self.balance:
self.balance -= amount
return f"withdrew ${amount}.new balance: ${self.balance}"
else:
return " insufficient funds"
def getbalance(self):
return f"current balance: ${self.balance}"

class savingsaccount(bankaccount):
def _init_(self, account_number, account_holder, balance, interest_rate):
super()._init_(account_number, account_holder, balance)
self.interest_rate = interest_rate
def calculate_interest(self):
interest = self.balance*self.interest_rate
self.balance += interest
return f"interest added: #${interest}.new balance: ${self.balance}"
#creating instances of savingaccount
savings_account = savingsaccount("90435642","vijay shankar e s",000,0.05)
print(savings_account.get_balance())
print(savings_account.deposit(500))
print(savings_account.calculate_interest())
print(savings_account.withdraw(2000))
OUTPUT:

Current Balance : $ 1000


Deposited $500
New Balance : $ 1500
Interest Added : $75.0 New Balance : $ 1575.0
Insufficient funds
PROGRAM # 11 WORKING WITH FILES

fn=open('D:/file1.txt','r')
fn1=open('odd_file.txt','w')
cont=fn.readlines()
print("file1.txt contains")
print(cont)
for i in range(0,len(cont)):
if(i%2!=0):
fn1.write(cont[i])
fn.close()
fn1=open('odd_file.txt','r')
cont1=fn1.readlines()
print("odd_file,txt contains")
print(cont1)
fn.close()
fn1.close()

OUTPUT:
PROGRAM #12 TURTLE GRAPHICS

import turtle
turtle.setup(800,600)
window=turtle.Screen()
window.title("My First Turtle Graphics Program")
skk=turtle.Turtle()
for i in range(4):
skk.forward(50)
skk.right(90)
turtle.done()

OUTPUT:
PROGRAM #13 TOWER OF HANOI

def towers_of_hanoi(n,source,destination,auxillary):
if n==1:
print(f"move disk 1 from (source) to (destination)")
return
towers_of_hanoi(n-1,source,auxillary,destination)
print(f"move disk (n) from (source) to (destination)")
towers_of_hanoi(n-1,auxillary,destination,source)
n=3
towers_of_hanoi(n,'A','C','B')

OUTPUT:
PROGRAM # 14 MENU DRIVEN PROGRAM WITH
DICTIONARY

word_meanings={ }
def display_menu():
print("menu :")
print("1.look up a word")
print("2.add a new word and its meaning")
print("3.exit")

def look_up_word():
word=input("enter the word search")
if word in word_meanings:
print(f"meaning of '{word}':{word_meanings[word]}")
else:
print(f"'{word}'not found in the dictionary")

def add_new_word():
word=input("enter the new word :")
meaning=input("enter the meaning of '{word}' :")
word_meanings[word]=meaning
print(f"'{word}' and its meaning added to the dictionary")

while True:
display_menu()
choice=input("enter your choice :")
if choice=='1':
look_up_word()
elif choice=='2':
add_new_word()
elif choice=='3':
print("existing the program")
break
else:
print("invalid choice please select valid choice")
OUTPUT:
PROGRAM # 15 HANGMAN GAME

import random
name=input("Enter your Name: ")
print("Good Luck !",name)
words=['rainbow','computer','science','programming','python','mathematics','play
er','condition','reverse','water','board']
word=random.choice(words)
#print(word)
print("guess the character :")
guesses=''
turns=6
while turns>0:
failed=0
for char in word:
if char in guesses:
print(char)
else:
print("_")
failed+=1
if failed==0:
print("you win")
print("the word is :",word)
break
guess=input("guess a character :")
guesses+=guess
if guess not in word:
turns-=1
print("wrong")
print("you have",turns,"more guesses")
if turns==0:
print("you lose,the secret word was",word)
OUTPUT:

You might also like