0% found this document useful (0 votes)
0 views44 pages

Python Lab

The document contains a collection of Python programs covering various topics such as temperature conversion, pattern construction, calculating student grades, area calculations, prime number generation, and more. Each section includes code snippets along with sample outputs demonstrating the functionality of the programs. The programs illustrate fundamental programming concepts and operations in Python.

Uploaded by

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

Python Lab

The document contains a collection of Python programs covering various topics such as temperature conversion, pattern construction, calculating student grades, area calculations, prime number generation, and more. Each section includes code snippets along with sample outputs demonstrating the functionality of the programs. The programs illustrate fundamental programming concepts and operations in Python.

Uploaded by

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

1.

TEMPERATURE CONVERSION
#PROGRAM TO CONVERT TEMPERATURE FROM FARENHEIT TO CELCIUS AND
VICEVERSA

print("Enter 1 to convert temperature from farenheit to celcius:")

print("Enter 2 to convert temperature from celcius to farenheit:")

ch=int(input("enter choice:"))

if ch==1:

faren=float(input("Enter the temperature in farenheit:"))

celcius=round((faren-32)*(5/9),2)

print(str(celcius)+"c"elif)

ch==2:

celcius=float(input("Enter the temperature in celcius:"))

faren=round(celcius*(9/5)+32,2)

print(str(faren)+"f")

else:

print("invalid choice") input("press

any key to exit")


OUTPUT
#PROGRAM TO CONVERT TEMPERATURE FROM FARENHEIT TO

CELCIUS AND VICEVERSA

###################################################################

####

Enter 1 to convert temperature from farenheit to

celcius: Enter 2 to convert temperature from celcius to

farenheit: enter choice: 1

Enter the temperature in farenheit:55

12.78c

Enter 1 to convert temperature from farenheit to

celcius: Enter 2 to convert temperature from celcius to

farenheit: enter choice: 2

Enter the temperature in celcius: 88

190.4f

Enter 1 to convert temperature from farenheit to

celcius: Enter 2 to convert temperature from celcius to

farenheit: enter choice: 0

invalid choice

press any key to exit


2 .CONSTRUCTION OF PATTERN
#PROGRAM TO CONSTRUCT THE FOLLOWING PATTERN USING A NESTED LOOP

Print(################################################################)

n=int(input("enter the value of n:"))

for i in range(n):

print(' '*(n-i-1)+'* '*(i+1))

for j in range(n-1,0,-1):

print(' '*(n-j)+'* '*(j))


OUTPUT
#PROGRAM TO CONSTRUCT THE FOLLOWING PATTERN USING A NESTED LOOP

################################################################

Enter the value of n: 5

*
**
***
****
*****
****
***
**
*
3. CALCULATE TOTAL MARKS, PERCENTAGE AND GRADE

OF A STUDENT

print(“PROGRAM TO CALCULATE TOTAL MARKS, PERCENTAGE AND GRADE OF

A STUDENT”)

print(“###################################################################”)

print("students marks details".center(60,'*'))

m1=int(input("enter mark1:")) m2=int(input("enter

mark2:")) m3=int(input("enter mark3:"))

m4=int(input("enter mark4:")) m5=int(input("enter

mark5:")) total=m1+m2+m3+m4+m5

per=(total/500)*100

print("\n Total mark:",total) print("\n

Total percentage:",per) if per>=80:

print("Grade is A") elif

per>=70:

print("Grade is B") elif

per>=60:

print("Grade is C") elif

per>=40:

print("Grade is D") else:

print("Grade is E")
OUTPUT
PROGRAM TO CALCULATE TOTAL MARKS, PERCENTAGE AND GRADE OF A

STUDENT

###################################################################

*******************STUDENTS MARKS

DETAILS******************* enter mark1:95

enter mark2:98

enter mark3:96

enter mark4:99

enter

mark5:100

Total mark: 488

Total percentage:

97.6 Grade is A
4. Area of Different shapes

print("finding area of shapes")

print("**************")

print("\n1. rectangle\n2. square\n3. circle\n4. triangle")

choice=int(input("enter your choice:"))

if choice==1:

l=float(input("enter the length of a rectangle:"))

w=float(input("enter the width of rectangle:"))

area=l*w

print('area of a rectangle is:',round(area,2))

elif choice==2:

a=float(input("enter the side of a

square:")) area=a*a

print("area of a square is:",round(area,2))

elif choice==3:

r=float(input("enter the radius of a

cicle:")) area=3.14*r*r

print("area of a circle is:",round

(area,2)) elif choice==4:

b=float(input("enter the base of a triangle:"))

h=float(input("enter the height of a triangle:"))

area=0.5*b*h

print("area of a triangle is:",round(area,2))

else:

print("wrong input...")
OUTPUT

finding area of shapes

**************

1. rectangle

2. square

3. circle

4. triangle

enter your choice:1

enter the length of a rectangle:5

enter the width of rectangle:8

area of a rectangle is: 40.0

finding area of shapes

**************

1. rectangle

2. square

3. circle

4. triangle

enter your choice:2

enter the side of a square:5

area of a square is: 25.0

finding area of shapes

**************

1. rectangle

2. square
3. circle

4. triangle

enter your choice:3

enter the radius of a cicle:5

area of a circle is: 78.5

finding area of shapes

**************

1. rectangle

2. square

3. circle

4. triangle

enter your choice:4

enter the base of a triangle:8 enter

the height of a triangle:5 area of a

triangle is: 20.0


5. PYTHON SCRIPT THAT PRIME NUMBER

PROGRAM TO PYTHON SCRIPT THAT PRIME NUMBER LESS THAN 20

print("The prime numbers less than 20

are...") for numerator in range (2,21):

for denominotor in range

(2,numerator): if numerator%

denominotor==0: break

else:

print(numerator,end=",")
OUTPUT

The prime numbers less than 20

are... 2, 3 , 5, 7, 11, 13, 17, 19,


6. FIND FACTORIAL A GIVEN NUMBER
PROGRAM TO FIND FACTORIAL OF GIVEN NUMBER

n=int(input("Enter the number to find

factorial:")) def fact(m):

if m==0 or

m==1: return

elif m >1:

return m* fact(m-

1) else:

return 0

print (n,"factorial=",fact(n))
OUTPUT

Enter the number to find

factorial:7 7 factorial= 5040


7. Program to count the number of even and odd numbers from
array of N Numbers

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

print("Even numbers in the array:",even_count)

print("Odd numbers in the array:",odd_count)


OUTPUT

Even numbers in the array:

4 Odd numbers in the array:

3
8. WRITE A PYTHON PROGRAM TO REVERSE A STING
WORD BY WORD

PROGRAM TO REVERSE A STRING WORD BY WORD

class reverse:

def rev_sentence(self,sentence):

words=sentence.split(' ')

reverse_sentence=' '.join(reversed(words))

print(reverse_sentence)

c=reverse()

c.rev_sentence(input("Enter the string:"))


OUTPUT
Enter the string: YOU ARE HOW

HOW ARE YOU


9. GIVE A TUPLE AND LIST AS INPUT WRITE PROGRAM TO
COUNT THE OCCURRENCES OF ALL ITEMS OF THE LIST IN THE
TUPLE

#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

print("Hello! welcome to the deposit and withdrawal machine")

def deposit(self):

amount=float(input("Enter the amount to be deposited:"))

self.balance=+amount

print("\nAmount deposited:",amount)

def withdraw(self):

amount=float(input("Enter the amount to be

withdrawn:")) if self.balance>=amount:

self.balance-=amount print("\nyou

withdraw:",amount)

else:

print("\n Insufficient Balance")

def display(self):

print("\n Net available balance=",self.balance)

s=bank_account()

s.deposit()

s.withdraw()

s.display(
OUTPUT

Enter the amount to be deposited: 5000

Amount deposited: 5000.0

Enter the amount to be withdrawn:

4000 you withdraw: 4000.0

Net available balance= 1000.0


11. Read a file content and copy only the contents at odd lines into
new file
f=open("myfile.txt",'w')

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

print("output:to display odd lines from the file:\n")

fn1=open('outfile.txt','r')

cont1=fn1.read()

print(cont1)

fn.close()

fn1.close()
OUTPUT

created file:

hi

welcome to

python

programmin

output: to display odd lines from the

file: hi

python
12. TURTLE GRAPHICS

import turtle

turtle.setup(300,300)

turtle.colormode(255)

window=turtle.Screen()

window.title("my first turtle program:")

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:

print("Move disk 1 from source",source,"to destination",destination)

return

TowerOfHanoi(n-1,source,auxilliary,destination)

print("Move disk",n,"from source",source,"to destination",destination)

TowerOfHanoi(n-1,auxilliary,destination,source)

n=3

TowerOfHanoi(n,'A','B','C')
OUTPUT
Move disk 1 from source A to destination B

Move disk 2 from source A to destination C

Move disk 1 from source B to destination C

Move disk 3 from source A to destination B

Move disk 1 from source C to destination A

Move disk 2 from source C to destination B

Move disk 1 from source A to destination B


14. DICTIONARY MEANINGS

my_Dict={'A':{'above':'at a higher level','action':'process of doing something','accurate':'exact'},

'B':{'big':'large','bouquet':'arranged bunch of flowers','bread':'food made of flour'}}

word=input("Enter the word to be searched:")

index=word[0:1].upper() if

index in my_Dict.keys():

if word in my_Dict[index]:

print(my_Dict[index][word])

else:

print('word not found')

else:

print('word not found')


OUTPUT

Enter the word to be searched: bouquet

arranged bunch of flowers


15. Create a Python program to implement the Hangman Game

import random name=input("What is your

name?")

print("Good Luck!",name)

words=['rainbow','computer','science','programming','python','mathematics','player','condition','reverse','water',
'board', 'geeks']

word=random.choice(words)

print("Guess the characters") guesses=" "

turns = 12 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("Worng")

print("You have",+turns,'more

guesses') if turns ==0:

print("You Loose")
OUTPUT

What is your name?Lokesh

Good Luck! Lokesh

Guess the characters

guess a character:c

Worng

You have 11 more guesses

guess a character: r r

_
_

guess a character: a r

guess a character:i

Worng

You have 10 more guesses r

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

The word is: rainbow


16. To find sum of all items in a dictionary

d= {‘A’: 100, ‘B’:200, ‘C’:=300, ‘D’:400}

Print(“ Total sum of values in the

dictionary:”) Print (sum(d.values()))


OUTPUT

Total sum of values in the dictionary:

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

print (“Largest of three numbers”)


a =int(input (“Enter the first number:”))
b =int(input (“Enter the second number:”))
c =int(input (“Enter the third

number:”)) if (a>b) and (a>c):

print (a, “is the largest”)

elif (b>a) and (b>c):

print (b, “is the largest”)

else:

print (c, “is the largest”)


OUTPUT

Largest of three numbers

Enter the first number: 10

Enter the second number: 3

Enter the third number: 24

24 is the largest
19. To concatenate two
strings using string
functions.

str1 = 'Hello'

str2 =

'World'

result = str1 + ' ' +

str2 print(result)
Output

‘Hello World

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