0% found this document useful (0 votes)
8 views20 pages

Python Lab Exercises for 5th Semester

This document is a lab file for a Python programming course at SUS College of Engineering & Technology, detailing various practical exercises for 5th-semester students. It includes programs demonstrating numeric data types, arithmetic operations, tuples, dictionaries, string manipulation, and more. Each program is accompanied by sample code and explanations for better understanding.

Uploaded by

Narinder Sharma
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)
8 views20 pages

Python Lab Exercises for 5th Semester

This document is a lab file for a Python programming course at SUS College of Engineering & Technology, detailing various practical exercises for 5th-semester students. It includes programs demonstrating numeric data types, arithmetic operations, tuples, dictionaries, string manipulation, and more. Each program is accompanied by sample code and explanations for better understanding.

Uploaded by

Narinder Sharma
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

Python lab file 5th sem

Programming with python (I. K. Gujral Punjab Technical


University)

Scan to open on Studocu

Downloaded by Narinder Sharma


Studocu is not sponsored or endorsed by any college or university

Downloaded by Narinder Sharma


SUSCET 1810038

Department of computer science

SUS College of Engineering & Technology

Programming in Python
Lab (BTCS513-18)

Lab File
(2020)

5th Semester

Submitted To: - Submitted By: -


Er. Gurleen Batra Abhishek Sharma
CSE Department Roll no. – 1810038
Branch– [Link] (CSE)

Page
1
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Practical Name of Practical Page Date Signature


No. no. /Remarks
1. Write a program to demonstrate 3 18-Aug-25
different numeric data type in
Python.
2. WAP to perform different 4 21-Aug-25
Arithmetic Operations on numbers
in python.
3. WAP to demonstrate working with 5 25 -Aug-25
tuples in python.
4. WAP to demonstrate working with 6 27- Aug-25
dictionary in python.
5. WAP to create, concatenate and 7 01-Sept-25
print a string and accessing sub-
string from a given string.
6. WAP to print the current date in the 8 04 Sept-25
following format “Sun May 29
02:26:23 IST 2017”
7. WAP to create, append and remove 9 08- Sept-25
list in python.
8. WAP to find largest of three 10 11-Sept-25
numbers.
9. WAP to convert to and from 11 15-Sept-25
Celsius, Fahrenheit.
10. WAP to construct the following 12 18 -Sept-25
pattern, using a nested for loop
11. WAP that prints prime numbers less 13 03-Oct-25
than 20.
12. WAP program to find factorial of a 14 14 Oct-25
number using recursion.
13. WAP that accepts the lengths of 15 17-Oct-25
three sides of a triangle as inputs.
14. WAP class to convert an integer to a 16 24-Oct-25
roman numeral.
15. WAP class to implement pow(x, n). 17 27-Oct -25
16. WAP class to reverse a string word 18 29- Oct-25
by word.

Program no. 1: Write a program to demonstrate different numeric data type


Page
2
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

in Python.
In Python, numeric data type represents the data which has numeric value. Numeric value can be
integer, floating number or even complex numbers. These values are defined as int, float and
complex class in Python.
• Integers – This value is represented by int class. It contains positive or negative whole
numbers (without fraction or decimal). In Python there is no limit to how long an
integer value can be.
• Float – This value is represented by float class. It is a real number with floating point
representation. It is specified by a decimal point. Optionally, the character e or E
followed by a positive or negative integer may be appended to specify scientific
notation.
• Complex Numbers – Complex number is represented by complex class. It is specified as
(real part) + (imaginary part) j. For example – 2+3j

A=5
print("Type of A: ", type(A))

B = 5.0
print("Type of B: ", type(B))

C = 2 + 4j
print("Type of C: ", type(C))

Page
3
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program No. 2: Write a program to perform different Arithmetic Operations


on numbers in python.
A= float(input("Enter the value of A: "))
B= float(input("Enter the value of B: "))

Addition= A + B
print("Value of addition: ",Addition)

Subtraction= A - B
print("Value of subtraction: ",Subtraction)

Multiplication= A * B
print("Value of multiplication: ",Multiplication)

Division= A / B
print("Value of Division: ",Division)

Modulus= A % B
print("Value of Modulus: ",Modulus)

Exponentiation= A**B
print("Value of Exponentiation: ",Exponentiation)

Floor_Division= A//B
print("Value of Floor_Division: ",Floor_Division)

Page
4
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program no. 3: WAP to demonstrate working with tuples in python.


Tuples are used to store multiple items in a single variable.
• A tuple is a collection which is ordered and unchangeable.
• Tuples are written with round brackets.
• Tuples are also allowed duplicate.

Fruits= ("Apple", "Banana", "Cheery", "Kiwi")


print("\n Created tuple: ", Fruits)

print("\n Access tuples by indexing: ", Fruits[1:4])

Vegetables= ("Patato", "Carrot", "Ladyfinger")


Products= Fruits+Vegetables print("\
n Join two tuples: ", Products)

''' Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable.
But there is a workaround. You can convert the tuple into a list, change the list, and convert the
list back into a tuple. '''

A=list(Fruits)
[Link]("Orange")
Fruits= tuple(A)
print("\n Update tuple by append tuple: ", Fruits)

A= list(Fruits)
[Link]("Banana")
Fruits= tuple(A)
print("\n Update tuple by remove: ", Fruits)

Page
5
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program no. 4: WAP to demonstrate working with dictionary in python.


• Dictionaries are used to store data values in key:value pairs.
• A dictionary is a collection which is unordered, changeable and does
not allow duplicates.
• Dictionaries are written with curly brackets, and have keys and values.

Car ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print("\n Create a Dictionary: ", Car)

x= Car["model"]
print("\n Access items by key name: ", x)

[Link]({"year": 2021})
print("\n Change key value: ", Car)

[Link]({"color": "red"}) print("\


n Add new key value: ", Car)

[Link]("color")
print("\n Remove items: ",Car)

Page
6
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program no. 5: WAP to create, concatenate and print a string and


accessing sub-string from a given string.
STR1= "Abhishek"
print("Created first string is: ",STR1)

STR2= " Sharma"


print("Created second string is: ",STR2)

STR3= STR1+STR2
print("A string after concatenate: ",STR3)

print("Accessing a sub-string from a given string: ",STR3[4:8])

Page
7
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program no. 6: WAP to print the current date in the following format “Sun
May 29 02:26:23 IST 2017”
import time
from time import gmtime, strftime
= [Link]()
print ([Link](t))

Page
8
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program no. 7: WAP to create, append and remove list in python.

Animals= ['Cat', 'Dog', 'Rabbit']

print("Animals list create: ",Animals)

Wild_Animals= ['Tiger', 'Lion', 'Fox'] [Link](Wild_Animals) print("Animals list after


append: ",Animals) [Link](Wild_Animals) print("Animals list after remove: ",Animals)

Page
9
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program no. 8: WAP to find largest of three numbers.

NUM1=float(input("Enter the first number: "))


NUM2=float(input("Enter the second number: "))
NUM3=float(input("Enter the Third number: "))
def find_largest():
if(NUM1>=NUM2) and (NUM1>=NUM2):
largest=NUM1
elif(NUM2>=NUM1) and (NUM2>=NUM3):
largest=NUM2
else:
largest=NUM3
print("Largest number is",largest)

find_largest();

Page
10
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program no. 9: WAP to convert to and from Celsius, Fahrenheit.

C= float(input("Enter temperature of a city in Celsius: "))

F= (C*9/5)+32

print("Temperature in Celsius: ",C) print("Temperature in Fahrenheit: ",F)

Page
11
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program no. 10: WAP to construct the following pattern, using a nested for
loop
*
**
***
****
*****
****
***
**
*

N= 5
for i in range(N):
for j in range(i):
print ('* ', end="")
print(' ')

for i in range(N,0,-1):
for j in range(i):
print('* ', end="")
print(' ')

Page
12
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program no. 11: WAP that prints prime numbers less than 20.

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

Page
13
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program no. 12: WAP program to find factorial of a number using recursion.

def RECURSION_FACT(NUM):
if NUM==1:
return NUM
else:
return NUM*RECURSION_FACT(NUM-1)

NUMBER=int(input("Enter the number for find factorial: "))

if NUMBER<0:
print("The factorial does not available for negative number")
elif NUMBER==0:
print("The factorial of zero is 1")
else:
print("Factorial of" , NUMBER,"is: ",RECURSION_FACT(NUMBER))

Page
14
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program no. 13: Write a program that accepts the lengths of three sides of a
triangle as inputs. The program output should indicate
whether or not the triangle is a right triangle (Recall from the Pythagorean
Theorem that in a right triangle, the square of one side equals the sum of the
squares of the other two sides).

A= float(input("Side 1 of right angle triangle: "))


B= float(input("Side 2 of right angle triangle: "))
C= float(input("Hypotenuse side of right angle triangle: "))

if C**2==A**2+B**2:
print("Right angled triangle.")
else:
print("Not a right angled triangle.")

Page
15
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program no. 14: Write a Python class to convert an integer to a roman


numeral.
class solution:
def Roman(self, num):
value= [1000, 900, 500, 400,100, 90, 50, 40,10, 9, 5, 4,1]
symbol= ["M", "CM", "D", "CD","C", "XC", "L", "XL","X", "IX", "V", "IV","I"]
roman_num= ''
i= 0
while num>0:
for _ in range(num // value[i]):
roman_num += symbol[i]
num -= value[i]
i += 1
return roman_num

print(solution().Roman(15))
print(solution().Roman(4000))
print(solution().Roman(10000))

Page
16
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program no. 15: Write a Python class to implement pow(x, n)

class solution:
def pow(self, x, n):
if x==0 or x==1 or n==1:
return x
if x==-1:
if n%2 ==0:
return 1
else:
return -1
if n==0:
return 1
if n<0:
return 1/[Link](x,-n)
value= [Link](x,n//2)
if n%2==0:
return value*value
return value*value*x
print("Value of 5 raised to the power of 3:
",solution().pow(5,3)) print("Value of 8 raised to the power of
2: ",solution().pow(8,2)) print("Value of 2 raised to the power
of 5: ",solution().pow(2,5))

Page
17
Downloaded by Narinder Sharma (narinderksharma71@[Link])
SUSCET 1810038

Program no. 16: Write a Python class to reverse a string word by word.

class solution:
def reverse_words(self, s):
return ' '.join(reversed([Link]()))

print(solution().reverse_words("Abhishek Sharma is a student of [Link] CSE 5th sem."))

Page
18
Downloaded by Narinder Sharma (narinderksharma71@[Link])

You might also like