PYTHON
PRACTICAL FILE
Department of Computer Science and Engineering
Guru Jambheshwar University of Science &
Technology, Hisar
Submitted By: Ayan Islam
Roll Number – 230010130059 Submitted To:
Class – [Link]. CSE (Batch-1) Mr. Ramesh
Semester – 5 th
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
INDEX
[Link]. Name Remarks
How to install platform for Python
1.
Programming.
WAP to print your name and perform
2.
Arithmetic Operations using Python.
WAP to perform various number operations
3.
in Python.
4. WAP to generate prime numbers in Python.
WAP to perform operation on Strings in
5.
Python.
6. WAP for List operations in Python.
7. WAP of Dictionary in Python.
8. WAP of Set in Python.
9. WAP for Linear Search in Python.
10. WAP for Binary Search in Python.
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
Experiment – 1
AIM : To install *Python* on a personal computer and set up the environment to run
Python programs.
Apparatus / Software Required:*
* A personal computer or laptop
* Operating System: *Windows / Linux / macOS*
* Internet connection
* *Python Installer* (Latest version from
[[Link]
* Text Editor / IDE (Optional): *IDLE, VS Code, PyCharm*, etc.
Theory:
Python is a *high-level, interpreted, and general-purpose programming language*. It is
widely used for software development, web development, data science, artificial
intelligence, and automation.
To use Python on a computer, we need to *install the Python interpreter* and set up the
environment so that we can write, execute, and debug Python programs.
Procedure :
Step 1
1. Open a web browser and visit: *[[Link]
2. Click on the *Downloads* menu.
3. Choose the latest version of Python suitable for your operating system ([Link]).
4. Download the *Python installer* (e.g., [Link]).
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
Step 2: Check both the given options Customize installation
1. Double-click on the downloaded installer.
2. *Check the box: **"Add Python to PATH"* (important).
3. Click on *Install Now*.
4. Wait until the installation completes.
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
Step 3: Enter Next
Step 4: Check the statements and Install
Step 5: Setup Visual Studio Code
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
Experiment – 2
AIM :Write a program to print your name and perform Arithmetic Operations in Python.
Source Code :
print("My name is Ayan Islam")
#Arithmetic operation
a = input("Enter first number: ")
b = input("Enter second number: ")
#Performing arithmetic operations
sum = int(a) + int(b)
sub = int(a) - int(b)
mul = int(a) * int(b)
div = int(a) / int(b)
mod = int(a) % int(b)
#Output the results
print("Sum:", sum)
print("Subtraction:", sub)
print("Multiplication:", mul)
print("Division:", div)
print("Modulus:", mod)
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
Experiment – 3
AIM : Program to perform various number operations.
Source Code :
# 3a)
# Find maximum and minimum from a list of a numbers
a = [23,56,12,90,67,4]
x = min(a)
y = max(a)
print("The smallest element is ", x)
print("The largest element is ", y)
Output :
# 3b)
# GCD of two numbers
import math
a = 60
b = 48
print("GCD of ",a,b," is :",[Link](a,b))
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
# 3c)
# Square root of a number
import math
sr = [Link](16)
print("Square root of 16 is ", [Link](sr))
Output :
# 3 d)
# Check number is prime or not
from math import sqrt
def prime(n,i):
if i == 1 or i==2:
return True
if n % i == 0:
return False
if prime(n, i-1) == False:
return False
return True
n = 13
i = int(sqrt(n)+1)
print(prime(n,i))
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
Experiment – 4
AIM : Write programs to implement the following in Python.
Source Code:
# 4a)
# Print first N prime numbers
import math
def is_prime(num):
if num < 2:
return False
for i in range(2, int([Link](num)) + 1):
if num % i == 0:
return False
return True # this should be outside the for loop
def print_first_n_primes(n):
count = 0
num = 2
while count < n:
if is_prime(num):
print(num, end=" ")
count += 1
num += 1
n = int(input("Enter how many Primes to print: "))
print_first_n_primes(n)
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
# 4b)
#Remove duplicate numbers from a list
numbers=[1,2,1,3,4,3,3,4,2,1,5,6,5]
unique_numbers=[]
for num in numbers:
if num not in unique_numbers:
unique_numbers.append(num)
print("Original Numbers :\n",numbers)
print("List without Duplicates :\n",unique_numbers)
Output :
# 4c)
#Print the Fibonacci series
def fibonacci(n):
a,b=0,1
for i in range(n):
print(a,end=" ")
a,b=b,a+b
n=int(input("Enter No. of Series you want to print :"))
fibonacci(n)
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
Experiment – 5
AIM : WAP to perform operation on Strings in Python.
Source Code :
#5a)
#Python program to create strings
# Using Single Quotes
sqs='This is a Single Quote String'
print(sqs)
# Using Double Quotes
dqs="This is a Double Quote String"
print(dqs)
#Using Triple Quotes
tqs='''This is a
"Multiline String"'''
print(tqs)
#Creating Empty String
es=""
print(es)
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
#5b)
# Python program to Concatinate two strings using '+'
st1="Peter"
st2="Parker"
st3=st1+st2
print(st3)
# Python program to Concatinate two strings without using '+'
st1="Peter"
st2="Parker"
print(st1,st2)
#'This will create a space between the two strings'
Output :
#5c)
# Python Program for deletion of a character from a string
st1="Expectos Petronum"
loc=7
st2=st1[:loc]+st1[loc+1:]
print("Original string: ",st1)
print("Modified string: ",st2)
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
Experiment – 6
AIM : WAP for List operations in Python.
Source Code :
# 6a) Creating a list
l1=[10,20,30]
l2=[10,"India",6.9]
print("l1:",l1)
print("l2:",l2,)
print()
Output :
# 6b) To Add elements in a list
# Using Append method
print("Appending 40")
[Link](40)
print("l1:",l1)
print()
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
# Using Insert
print("Inserting 49.7 at position 2 in l2")
[Link](2,49.7)
print("l2:",l2)
print()
Output :
# Using extend
print("Extending l1 with l2")
[Link](l2)
print("l1:",l1)
print()
Output :
# 6c) To Delete an element from a list
# Using Remove method
print("Removing 10")
[Link](10) # It will remove the first occurence of the element
print("l1:",l1)
print()
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
# Using Pop
print("Pop 40")
p=[Link](2)
print("l1:",l1)
print("Poped item =",p)
print()
Output :
# Using delete
print("Deleting 30 at position 1")
del l1[1]
print("l1:",l1)
print()
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
# To find the sum of all elements in a list
# Using sum()
l3=[3,5,1,6,2,8,6,4,0,7]
s1=sum(l3)
print("l3:",l3)
print("Using sum function")
print("sum :",s1)
print()
Output :
# Using for loop
s2=0
for i in l3:
s2+=i
print("Sum using for loop")
print("sum :",s2)
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
Experiment – 7
AIM : WAP of Dictionary in Python.
Source Code :
#7a) Create a Dictionary
V={"Name":"Peter","Age":21,"Gwen":"My MJ",4_6:25,"Symbiote":"Venom",}
print("V :",V)
Output :
#7b) Insert new key in dictionary
[Link]({"Pavitra":"Prabhakar"})
print("V :",V)
Output :
#7c) Removing/Deleting a key from dictionary
#i) Using Pop() It returns specific key from the dictionary
# and returns it's corresponding value.
m=[Link]("Age")
print("New dictionary V :",V)
print("Deleted key's value :",m)
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
#ii) Using del() It deletes a key value pair from a dictionary
del V["Symbiote"]
print("New dictionary V :",V)
print("Deleted key : Symbiote.")
Output :
#7d) To check a key Exist in Dictionary or not
# i) Using "in" operator
k="Pavitra"
print(k in V)
b=69
print(b in V)
Output :
#ii) Using get() method
if [Link]("Gwen") is None:
print("Key not Available")
else:
print("Key Available")
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
#7e) To clear Dictionary
# Using clear() method
[Link]()
print(V)
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
Experiment – 8
AIM : WAP of Set in Python.
Source Code :
#8a) Create a Set
s={1, 2, 3, 4}
print("set s :",s)
l1=[1, 2, 2, 3, 4, 4, 5]
s1=set(l1)
print("list l1 :",l1)
print("converted set s1 :",s1)
Output :
#8b) Union of Sets
s3={2,5,7,9,5,8}
s4={1,4,55,9,8,22,2,69}
print("s3 :",s3)
print("s4 :",s4)
s3=[Link](s4)
print("[Link](s4) :",s3)
s3=s3|s4
print("s3|s4 :",s3)
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
#8c) Intersection of Sets
s5={11,34,56,23,75}
s6={11,4,55,9,8,23,75}
print("s5 :",s5)
print("s6 :",s6)
s5=[Link](s6)
print("[Link](s6) :",s5)
s3=s5&s6
print("s5&s6 :",s5)
Output :
#8d) Difference of Sets
s7={76,8,4,56,55,93}
s8={1,76,56,93,8,22,69}
print("s7 :",s7)
print("s8 :",s8)
s7=[Link](s8)
print("[Link](s8) :",s7)
s7=s7-s8
print("s7-s8 :",s7)
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
#8e) Adding an element
s9={2,4,3,7,9,1}
print("s9 :",s9)
[Link](5)
print("adding '5' :", s9)
Output :
#8f) Removing an element
s10={4,67,4,35,98,46}
print("s10 :",s10)
[Link](67)
print("s10 :",s10)
Output :
#8g) Discarding an element
s11={3,57,84,44,59,78}
print("s11 :",s11)
[Link](79)
[Link](69) #does not give error if the element is not present
print("s10 :",s10)
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
#8h) Popping an element
s12={4,67,4,35,98,46}
print("s12 :",s12)
e=[Link]()
print("s12 :",s12)
print("Element deleted :",e)
Output :
#8i) Clear all element
s13={2,4,3,7,9,1}
print("s13 :",s13)
[Link]()
print("s13 :", s13)
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
Experiment – 9
AIM : WAP for Linear Search in Python.
Source Code :
def ls(l,key):
for i in range(len(l)):
if l[i]==key:
return i
return -1
l1=[5,34,7,6,23,11,69]
print("l1 :",l1)
key=int(input("Enter key to find :"))
result=ls(l1,key)
if result!=-1:
print("Element ",key," found at index :",result)
else:
print("Element ",key," not found in list")
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1
Experiment – 10
AIM : WAP for Binary Search in Python.
Source Code :
def binary_search(arr, key):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == key:
return mid
elif arr[mid] < key:
low = mid + 1
else:
high = mid - 1
return -1
arr = [2, 4, 6, 8, 10, 12, 14, 16]
print("arr :",arr)
key = int(input("Enter the element to search: "))
result = binary_search(arr, key)
if result != -1:
print(f"Element {key} found at index {result}")
else:
print("Element not found.")
Output :
Name : Ayan Islam Roll No. : 230010130059 Class : B-Tech CSE B-1