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

Python Lab Programs Material

Python lab programs

Uploaded by

saravanan200410
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python Lab Programs Material

Python lab programs

Uploaded by

saravanan200410
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

1. Demonstrate different Number datatypes.

Program:
#integer
a=13
print(a)
print(type(a))

#float
a=13.04
print(a)
print(type(a))

#boolean
a=True
print(a)
print(type(a))

#string
name="rohit"
print(name)
print(type(name))

#list
b=["i bca","ii bca","iii bca"]
print(b)
print(type(b))

#tuple
t=("c++","c#","php")
print(t)
print(type(t))

#dictionary
dict={"PLA":"nabeel","27":"SMT"}
print(dict)
print(type(dict))

#frozenset
a=13
x=frozenset({"kohli","surya",})
print(x)
print(type(x))

Output:

13
<class 'int'>
13.04

<class 'float'>
True

<class 'bool'>
rohit

<class 'str'>
['i bca', 'ii bca', 'iii bca']

<class 'list'>
('c++', 'c#', 'php')

<class 'tuple'>
{'PLA': 'nabeel', '27': 'SMT'}

<class 'dict'>
frozenset({'kohli', 'surya'})
<class 'frozenset'>
2. Calculate Euclidean distance between two points by
taking input from the user.

Program:

a = int (input("enter x1="))


b = int (input("enter x2="))
c = int (input("enter y1="))
d = int (input("enter y2="))
result=(((b-a)**2+(d-c)**2)**0.5)
print("distance between(a,b)and(c,d):",result)

Output:

enter x1=2
enter x2=2
enter y1=2
enter y2=3
distance between(a,b)and(c,d): 1.0
3. Find the Factorial of a given number using Functions.

Program:

num = int(input("Enter a number:"))


def fun(num):
f=1
for i in range(1,num+1):
f=f*i
print("factorial of number:",f)
fun(num)

Output:

Enter a number:22
factorial of number: 1124000727777607680000
4. Print whether a number is positive/negative using if-else

Program:

num=int(input("enter a number:"))
if num>0:
print("The given number is positive")
elif num==0:
print("The given number is zero")
else:
print("The given number is negative")

Output:

enter a number:2
The given number is positive
enter a number:-9
The given number is negative
enter a number:0
The given number is zero
5. Create a Simple Calculator using if-elif statement

Program:
print("simple calculator")
print("1.addition \n 2.subtraction \n 3.Multiplication \n 4.Division")
a=int(input("Enter value a:"))
b=int(input("Enter value b:"))
opt=int(input("Enter option 1 to 4:"))
if opt==1:
print("Addition:",a+b)
elif opt==2:
print("subtraction:",a-b)
elif opt==3:
print("Multiplication:",a*b)
elif opt==4:
print("division",a/b)
else:
print("invalid")
print("correct option")

Output:
simple calculator
1. Addition
2. Subtraction
3. Multiply
4. Divide

Enter Value a:10


Enter Value b:25
Enter options 1 to 4: 1
Addition: 35

Enter Value a:25


Enter Value b:12
Enter options 1 to 4: 2
Subtraction: 13

Enter Value a:5


Enter Value b:5
Enter option 1 to 4: 3
Multiply: 25

Enter Value a:10


Enter Value b:2
Enter options 1 to 4: 4
Divide: 5.0
6. Find the sum of all primes between 1 to 100 using for loop
Program:
upto=int(input("Find Sum of prime numbers:"))
sum=0
for num in range(1,upto+1):
for i in range(2,num):
if(int(num%i)==0):
break;
else:
sum+=num
print("prime number is ",num)
print("\n sum of all prime numbers:",sum)

Output:
Find Sum of prime numbers:15
prime number is 1
prime number is 2
prime number is 3
prime number is 5
prime number is 7
prime number is 11
prime number is 13

the sum of all prime numbers: 42


7. Compute the number of characters, words, and lines in a
file
Program:
w=0
line=0
ch=0
with open("ex.txt",'r')as f1:
data=f1.read()
w=+len(data.split())
line=data.splitlines()
ch=len(data)
print("no of words:",w)
print("no of lines:",len(line))
print("No of characters:",ch)

Output:

no of words: 9
no of lines: 4
No of characters: 39
8. Print all of the unique words in the file in alphabetical
order

Program:
file=open("file.txt",'r')
txt=file.read().lower()
w=txt.split()
print("The given txt is:\n")
print(txt)
print("\n the words txt file are:\n")
print(w)
print("\n the sorted txt file is:\n")
w.sort()
for x in w:
print(x)
print("\n the sorted words removing duplicates:\n")
u=list(dict.fromkeys(w))
print(u)

Output:
I’m Mohammed,
I’m from Trichy
I’m studying bca at jamal mohammed college
Mohammed
Trichy
At
BcaFrom
I’m
Jamal
Mohammed
Studying
Mohammed
Trichy
At
Bca
College
From
I’m
Jamal
Mohammed
Studying
Trichy
9. Define a module to find Fibonacci Numbers and import
the module to another program.
Program:
fib.py:
Fib(n):
a , b=0,1
Li=[]
For _ in range(n):
Li.append(a)
a, b=b, a+b
return li

fib1.py:
import fib1
N = int(input("Enter a Number : "))
numbers = fib1.fib(N)
print(f"Fibonacci Series upto {N} is ...")
print(numbers)

Output:

Enter a number:6
(0,1,1,2,3,5)
10. Create a list and perform the following methods
a) insert () b) remove () c) append () d) len () e) pop ()

Program:
a=["a","b","c","d"]
print(a)
a.insert(1,"aa")
print(a)
a.remove("b")
print(a)
a.append("j")
print(a)
print(len(a))
a.pop(0)
print(a)

Output:
['a', 'b', 'c', 'd']
['a', 'aa', 'b', 'c', 'd']
['a', 'aa', 'c', 'd']
['a', 'aa', 'c', 'd', 'j']
5
['aa', 'c', 'd', 'j']
11. Create a tuple and perform the following operations
a) Concatenation b) Repetition c) Membership d) Access
items e) Slicing

Program:
t1=(10,20,30,40,50)
t2=(60,70,80,90,100)
t3=(t1+t2)
print("concatination:",t3)
t4=(t1*2)
print("repetition:",t4)
print("membership(using in opr):",20 in t1)
print("membership(using not in opr):",90 not in t2)
print("acces item(using index no[]):",t1[2],t2[-4])
print("slicing[start:stop]:",t2[1:4],t1[3:])

Output:
concatination: (10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
repetition: (10, 20, 30, 40, 50, 10, 20, 30, 40, 50)
membership(using in opr): True
membership(using not in opr): False
acces item(using index no[]): 30 70
slicing[start:stop]: (70, 80, 90) (40, 50)
12.Sort (ascending and descending) a dictionary by value

Program:
import operator
d = {"c": 2, "b": 4, "a": 3}
print('Original dictionary : \n',d)
sd = sorted(d.items(), key=operator.itemgetter(1))
print('Ascending order : \n',sd)
sd =sorted(d.items(), key=operator.itemgetter(1),
reverse=True)
print('Descending order : \n',sd)

Output:
Original dictionary :
{'c': 2, 'b': 4, 'a': 3}
Ascending order :
[('c', 2), ('a', 3), ('b', 4)]
Descending order :
[('b', 4), ('a', 3), ('c', 2)]
13. Prepare a Students Marks List using Class

Program:
class std:
def mark(self):
name=input("enter a name:")
roll=input("enter roll number:")
m1=int(input("enter mark1:"))
m2=int(input("enter mark2:"))
m3=int(input("enter mark3:"))
m4=int(input("enter mark4:"))
print("name:",name,"roll:",roll,"mark1:",m1,"mark2:",m2,"mark3:",m3,"mar
k4:",m4)
total=m1+m2+m3+m4
print("The total mark is",total)
avg=total/4
print("Average",avg)
b=std()
b.mark()
Output:
enter a name: Rohit
enter roll number: 22uca000
enter mark1:55
enter mark2:45
enter mark3:88
enter mark4:78
name: Rohit roll: 22uca000 mark1: 55 mark2: 45 mark3: 88 mark4: 78
The total mark is 266
Average 66.5
14.Find the area of a Circle using Class and Object

Program:

class circle:
def radius(self):
r=int(input("enter radius:"))
rad=3.14*r*r
print(rad)
b=circle()
b.radius()

Output:
enter radius:15
706.5

You might also like