Python Lab Record
Python Lab Record
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:
else:
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:
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:'))
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
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(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")
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"]
X.append("cherry")
X.insert(1,"grape")
X[1]="blackberry"
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()
X.sort(reverse=True)
26
Output:
27
Ex. No: 14 Transpose Matrix
Date:19.09.2023
Aim:
To create a python program to create transpose matrix.
Code:
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:
if num % 2==0:
print(num,end=" ")
30
Output:
31