0% found this document useful (0 votes)
13 views

Python Lab Record

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)
13 views

Python Lab Record

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/ 31

Ex.

No: 1 Magic 8 Ball


Date: 04.7.2023

Aim:
To create a python program for magic 8 ball game.

Code:
import sys import random ans=True whileans:
question=input("ask the magic 8 ball a question(press enter to quit):")
answers=random.randint(1,8)
if question=="":
sys.exit()
elif answers==1:
print("it is certain") elif answers==2: print("outlook good") elif answers ==3:
print("you may rely on it") elif answers==4: print("ask again later") elif answers==5:
print("concentrate and ask again") elif answers==6:
print("reply hazy,try again") elif answers==7:
print("my reply is no") elif answers==8:
print("my sources say no")

1
Output:

2
3
Ex. No: 2 Prime Number
Date: 06.7.2023

Aim:
To create a python program to find a prime number.

Code:
number=int(input("enter any number:")) if number>1:

for i in range(2,number):

if (number%i)==0:

print(number,"is not a prime number") break;

else:

print(number,"is a prime number") else:

print(number,"is not a prime number")

4
Output:

5
Ex. No: 3 Find Odd or Even
Date: 11.7.2023

Aim:
To create a python program to find a number is Odd or Even

Code:

X=int(input("enter any number "))


if(X%2)==0:
print(X,"it is an even number") else:
print(X,"it is an odd number")

6
Output:

7
Ex. No: 4 Simple Calculator Using Python
Date: 13.7.2023

Aim:
To create a python program for making a simple calculator.

Code:
X=int(input("enter the 1st no:"))
Y=int(input("enter the 2nd no:"))
Z=input("enter the operator")
if Z=="+":
print(X+Y)
elif Z=="-":
print(X-Y)
elif Z=="*":
print(X*Y)
elif Z=="/":
print(X/Y)
elif Z=="%":
print(X%Y)

8
Output:

9
Ex. No: 5 Factorial Number
Date: 20.7.2023

Aim:
To create a python program to find the Factorial of a number.

Code:
n=int(input("enter the factorial number"))
x=1
for i in range(1,n+1):
x=x*i
print(x)

10
Output:

11
Ex. No: 6 Leap Year
Date: 25.7.2023

Aim:
To create python program to find a leap year.

Code:
year = int(input('enter year:'))

if(year%4 == 0 and year%100 !=0) or (year%400 == 0): print(year,"is a leap year.")


else:

print(year,"is not a leap year.")

12
Output:

13
Ex. No: 7 Swapping of Two Numbers
Date:03.08.2023

Aim:
To create a python program to Swap two number

Code:
X=int(input("enter 1st no:")) Y=int(input("enter 2nd no:")) temp=X
X=Y

Y=temp

print(“the 1st number is ”,X) print(“the 2nd number is”,Y)

14
Output:

15
Ex. No: 8 Conversion of Decimal to Binary, Octal,
Date:10.08.2023 Hexadecimal

Aim:
To create a python program to convert decimal to binary, octal,
hexadecimal

Code:
dec=int(input("enter the value:"))

print("the decimal value of",dec,"is:")

print(bin(dec),"in binary.")

print(oct(dec),"in octal.")

print(hex(dec),"in hexadecimal.")

16
Output:

17
Ex. No: 9 Read a File Through Python
Date:16.08.2023

Aim:
To create a python program to read a text file in python.

Code:

p=open(r"C:\Users\User\Desktop\kamesh.txt","r")

print(p.readline())

print(p.readline())

18
Output:

19
Ex. No: 10 Writing a File Through Python
Date:23.08.2023

Aim:
To create a python program to write a text file through python

Code:

X=open(r"C:\Users\User\Desktop\kamesh.txt","w")

L=["ENNAM POL VAZHKAI"]

print(X.writelines(L))

X.close()

X=open(r"C:\Users\User\Desktop\kamesh.txt","r")

print(X.readline())

20
Output:

21
Ex. No: 11 Append a file through python
Date:30.08.2023

Aim:
To create a python program to append a text file through python.

Code:

X=open(r"C:\Users\User\Desktop\kamesh.txt","a")

L=[" hi im kamesh"]

print(X.writelines(L))

X.close()

X=open(r"C:\Users\User\Desktop\kamesh.txt","r")

print(X.readline())

22
Output:

23
Ex. No: 12 Insert and Replace values in List
Date:01.09.2023

Aim:
To create a python program to insert and replace values in list.

Code:

X=["apple","banana","kiwi"]

print(X) #printing the list

X.append("cherry")

print(X) # adding a value at last

X.insert(1,"grape")

print(X) #insert a value at specific index

X[1]="blackberry"

print(X) #replacing value at specific index

24
Output:

25
Ex. No: 13 Sorting a List
Date:11.09.2023

Aim:
To create a python file to sort out list in ascending and descending order

Code:

X=["mark","kamesh","abrar","sam","asheed"]

X.sort()

print(X) #arranging in ascending order

X.sort(reverse=True)

print(X) #arranging in descending order

26
Output:

27
Ex. No: 14 Transpose Matrix
Date:19.09.2023

Aim:
To create a python program to create transpose matrix.

Code:

X=[[12,7], [1,2], [3,8]]

result=[[0,0,0], [0,0,0]]

for i in range(len(X)):

for j in range(len(X[0])):

result[j][i]=X[i][j]

for r in result:

print(r)

28
Output:

29
Ex. No: 15 Generating List of Even Numbers
Date:26.9.2023

Aim:
To create a python program to generate list of even numbers.

Code:

start=int(input("enter starting number:"))

end=int(input("enter ending number:"))

for num in range(start,end+1):

if num % 2==0:

print(num,end=" ")

30
Output:

31

You might also like