●Approved by AICTE, Ministry of HRD, Govt.
of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: VMSB Uttarakhand Technical University Sri Dev Suman Uttarakhand University Uttarakhand Board of Technical Education
COURSE RECORD
Academic Year: - 2025-2026 (Odd Semester)
Faculty Name: Ms. Mansi Negi
Course Title: Python Programming
Semester: III
Department: Computer Science & Engineering
Vision
• To emerge as an academic centre producing world class professionals promoting innovation and Dhoolkot, P.O. Selaqui, Chakrata
research. Raod Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment
info@[Link]
and entrepreneurship.
• To Be educational centre of excellence of multi ethnicity and diversity.
0135-2699300
• To Establish as technology driven teaching learning institution.
0135-2699309
• To Provide world class platform for research and innovation.
• ToInculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Index
[Link] Name of Practical Date Remark
1 a) Running instructions in Interactive interpreter and a Python Script
b) Write a program to purposefully raise Indentation Error and Correct it
2 a) Write a program to compute distance between two points taking input
from the user (Pythagorean Theorem)
b) Write a program [Link] that takes 2 numbers as command line
arguments and prints its sum.
3 a)Write a program for checking whether the given number is a even number
or not
b)Using a for loop, Write a program that prints out the decimal equivalent of
1/2, 1/3, 1/4, …………………………….1/10
c)Write a Program using a for loop that loops over a sequence.
d)Write a program using a while loop that asks the user for a number, and
prints a countdown from that number to zero.
4 a)Find the sum of all the primes below two million. Adding the previous
two terms, each new term in the Fibonacci sequence is generated. By
starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55,
89, ...
b) By considering the terms in the Fibonacci sequence whose values do not
exceed four million, find the sum of the even valued terms.
c) Linear Search and Binary Search
d)Selection Sort and Insertion Sort
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
5 a)Write a program to count the numbers of characters in the string and
store them in a dictionary data structure
b) Write a program to use split and join method in the string and trace a
birthday with a dictionary data structure
6 a)Write a program combine_lists that combines these list into a dictonary.
b) Write a program to count frequency of character in a given file. Can you
use character frequency to tell whether the given file is a python program
file.c program or a text file.
7 a)Write a program to print each line of a file in reverse order.
b)Write a program to compute the number of characters, word
8 a)Write a function ball collide that takes two balls as parameters and
computes if they are colliding. Your function should return a Boolean
representing whether or not the balls are colliding. Hint: Represent a ball
on a plane as a tuple of (x, y, r), r being the radiusIf (distance between two
balls centers) <= (sum of their radii), then (they are colliding)
b) Find mean, median, mode of numbers in a list
9 a)Function nearly_equal to test string similarity
b) Function dups to find duplicates in a listc) Function unique to find unique
elements
10 a) Function cumulative_product for list
b) Function to reverse a list (without reverse())
c) Function to compute gcd and lcm of two numbers
11 a)Install and explore packages (requests, flask, pip)
b)Plot graphs using Python and Matplotlib
c) Data Analysis using NumPy and Pandas
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Practical 1
a) Running instructions in Interactive interpreter and a Python Script
In Interactive Interpreter (IDLE Shell)
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c=a+b
print("Sum =", c)
As a Python Script
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c=a+b
print("Sum =", c)
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Output:
In Interactive Interpreter (IDLE Shell)
As a Python Script
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
b) Write a program to purposefully raise Indentation Error and Correct it
# Program with Indentation Error
num = 5
if num > 0:
print("Number is positive")
# Correct Program
num = 5
if num > 0:
print("Number is positive")
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Output :
Output with Indentation Error
Output without Indentation Error
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Practical 2
c) Write a program to compute distance between two points taking input from the user (Pythagorean
Theorem)
import math
# Taking input from user
x1 = float(input("Enter x1: "))
y1 = float(input("Enter y1: "))
x2 = float(input("Enter x2: "))
y2 = float(input("Enter y2: "))
# Applying Pythagorean theorem
distance = [Link]((x2 - x1)**2 + (y2 - y1)**2)
# Output the result
print("The distance between the points is:", distance)
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
d) Write a program [Link] that takes 2 numbers as command line arguments and prints its sum.
# [Link]
import sys
# Check if exactly 2 arguments are provided (excluding script name)
if len([Link]) != 3:
print("Usage: python [Link] <num1> <num2>")
else:
# Convert arguments to numbers
num1 = float([Link][1])
num2 = float([Link][2])
# Print the sum
print("Sum:", num1 + num2)
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Practical 3
a) Write a Program for checking whether the given number is a even number or not.
# Taking input from user
num = int(input("Enter a number: "))
# Checking even or not using modulus operator
if num % 2 == 0:
print(num, "is an Even number.")
else:
print(num, "is an Odd number.")
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
b) Using a for loop, write a program that prints out the decimal equivalents of 1/2, 1/3, 1/4, . . .
,1/10
# Program to print decimal equivalents of fractions 1/2 to 1/10
# Start of the program
print("Decimal equivalents of fractions from 1/2 to 1/10:\n")
# Loop through numbers from 2 to 10
for i in range(2, 11):
# Calculate decimal value
decimal_value = 1 / i
# Format the fraction as text
fraction_text = "1/" + str(i)
# Print the fraction and its decimal value (up to 4 decimal places)
print(fraction_text, "=", format(decimal_value, ".4f"))
# End of the program
print("\nProgram finished successfully!")
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Output :
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
c) Write a program using a for loop that loops over a sequence.
# Program: Looping over a sequence using for loop
subjects = ["python", "operating system", "deep learning", "machine learning"]
print("List of subjects:")
for sub in subjects: # loop over the sequence (list)
print(sub)
print("Loop finished!")
Output :
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
d) Write a program using a while loop that asks the user for a number, and prints a countdown
from that number to zero.
# Countdown using while loop
num = int(input("Enter a number to start countdown: "))
while num >= 0:
print(num)
num = num - 1 # decrease the number by 1 each time
print("Countdown finished!")
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Practical 4
a) Find the sum of all the primes below two million. Adding the previous two terms, each new
term in the Fibonacci sequence is generated. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
# Sum of all primes below 2 million
n = 2000000
sieve = [True] * n
sieve[0] = sieve[1] = False # 0 and 1 are not prime numbers
for i in range(2, int(n**0.5) + 1): # for loop (control flow)
if sieve[i]: # if statement
for j in range(i*i, n, i): # nested loop
sieve[j] = False # assignment operator
prime_sum = 0
for i in range(n): # another loop
if sieve[i]: # conditional
prime_sum += i # arithmetic operator (+)
print("Sum of all primes below 2,000,000:", prime_sum)
# Sum of even Fibonacci numbers ≤ 4 million
a, b = 1, 2
fib_sum = 0
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
while a <= 4000000: # while loop
if a % 2 == 0: # modulus operator + if
fib_sum += a # arithmetic operator
a, b = b, a + b # assignment + arithmetic
print("Sum of even Fibonacci terms ≤ 4,000,000:", fib_sum)
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
b) By considering the terms in the Fibonacci sequence whose values do not exceed four million,
find the sum of the even valued terms.
# Program to find the sum of even-valued Fibonacci numbers <= 4 million
limit = 4000000
a=1 # first Fibonacci number
b=2 # second Fibonacci number
even_sum = 0
while b <= limit:
if b % 2 == 0: # check even
even_sum = even_sum + b # using operator
c=a+b # next Fibonacci number
a=b
b=c
print("Sum of even-valued Fibonacci terms:", even_sum)
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
c) Linear search and Binary search
# Linear Search in Python
arr = [10, 25, 30, 45, 50]
key = 30
found = False
for i in range(len(arr)):
if arr[i] == key:
print("Element found at index:", i)
found = True
break
if not found:
print("Element not found")
# Binary Search in Python
arr = [10, 25, 30, 45, 50]
key = 50
low = 0
high = len(arr) - 1
found = False
while low <= high:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
mid = (low + high) // 2 # middle index
if arr[mid] == key:
print("Element found at index:", mid)
found = True
break
elif arr[mid] < key:
low = mid + 1
else:
high = mid - 1
if not found:
print("Element not found")
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
d) Selection sort, Insertion sort
arr = [8, 3, 5, 4, 6]
n = len(arr)
for i in range(1, n):
key = arr[i]
j=i-1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j=j–1
arr[j + 1] = key
print("Sorted array:", arr)
# Selection Sort using operators and control flow
arr = [68, 33, 15, 84, 46]
n = len(arr)
for i in range(n - 1): # outer loop for passes
min_index = i # assume current index has minimum element
for j in range(i + 1, n): # inner loop to find actual minimum
if arr[j] < arr[min_index]: # comparison operator (<)
min_index = j # update min_index if smaller element found
# swap the found minimum element with first element of unsorted part
temp = arr[i] # assignment
arr[i] = arr[min_index] # swap using assignment
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
arr[min_index] = temp
print("Sorted array:", arr)
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Practical 5
a) Write a program to count the numbers of characters in the string and store them in a
dictionary data structure
# Step 1: Input string
text = input("Enter a string: ")
# Step 2: Create an empty dictionary
char_count = {}
# Step 3: Loop through each character in the string
for ch in text:
if ch in char_count: # if character already in dictionary
char_count[ch] = char_count[ch] + 1 # increment count
else:
char_count[ch] = 1 # first occurrence
# Step 4: Print the dictionary
print("Character frequencies:", char_count)
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
b) Write a program to use split and join methods in the string and trace a birthday with a
dictionary data structure.
# Program to use split and join and trace a birthday using dictionary
# Step 1: Create an empty dictionary to store birthdays
birthdays = {}
# Step 2: Take input for multiple friends (name:date format)
data = input("Enter names and birthdays (format: Name:DD/MM/YYYY, separated by commas):\n")
# Step 3: Split the input by commas to get each name-birthday pair
entries = [Link](',')
# Step 4: Process each pair
for entry in entries:
name, date = [Link](':') # split name and date
birthdays[[Link]()] = [Link]() # store in dictionary
# Step 5: Ask user to search a birthday
search_name = input("\nEnter the name to trace birthday: ")
# Step 6: Check and print result
if search_name in birthdays:
print(search_name, "has birthday on", birthdays[search_name])
else:
print("Sorry,", search_name, "is not found in the birthday list.")
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
# Step 7: Use join() to show all data neatly
print("\nAll Birthdays:")
for name, date in [Link]():
info = " : ".join([name, date]) # join name and date using :
print(info)
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Practical 6
a) Write a program combine_lists that combines these lists into a dictionary.
def combine_lists(keys, values):
result = {} # Create an empty dictionary
for i in range(len(keys)): # Use loop to combine both list
result[keys[i]] = values[i] # store key-value pair
return result # Return the combined dictionary
names = ["Mansi", "Riya", "John"]
birthdays = ["06/10/1999", "23/09/2001", "12/05/2000"]
combined_dict = combine_lists(names, birthdays) # Function call
print("Combined Dictionary:", combined_dict) # Print result
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
b) Write a program to count frequency of characters in a given file. Can you use character
frequency to tell whether the given file is a Python program file, C program file or a text file?
import os
filename = input("Enter file name: ").strip()
if not [Link](filename):
create = input(f"File '{filename}' not found. Create it? (y/n): ").strip().lower()
if create == 'y':
print("Enter file content. Press Enter twice to finish:")
lines = []
while True:
line = input()
if line == "":
break
[Link](line)
with open(filename, 'w', encoding='utf-8') as f:
[Link]("\n".join(lines))
print(f"\nFile '{filename}' created successfully!\n")
else:
print("Aborting. Please place the file in the same folder or give full path.")
exit()
# Count character frequency
freq = {}
with open(filename, 'r', encoding='utf-8') as f:
text = [Link]()
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
for ch in text:
freq[ch] = [Link](ch, 0) + 1
print("\nCharacter frequencies:\n")
for k, v in sorted([Link](), key=lambda x: x[0]):
print(f"{k!r} : {v}")
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Practical 7
a)Write a program to print each line of a file in reverse order.
filename = input("Enter the file name (with extension): ") # Ask for the file name
with open(filename, 'r') as file: # Open the file in read mode
lines = [Link]() # Read all lines into a list
print("\nReversed lines from the file:\n") # Reverse each line and print
for line in lines:
line = [Link]() # Remove newline character at end
print(line[::-1]) # Print reversed line
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
b) Write a program to compute the number of characters, words and lines in a file.
filename = input("Enter file name: ")
with open(filename, 'r') as f:
text = [Link]()
lines = [Link]('\n')
words = [Link]()
chars = len(text)
print("Number of lines :", len(lines))
print("Number of words :", len(words))
print("Number of characters :", chars)
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Practical 8
a) Write a function ball collide that takes two balls as parameters and computes if they are colliding.
Your function should return a Boolean representing whether or not the balls are colliding. Hint:
Represent a ball on a plane as a tuple of (x, y, r), r being the radiusIf (distance between two balls
centers) <= (sum of their radii), then (they are colliding)
import math
def ball_collide(ball1, ball2):
x1, y1, r1 = ball1
x2, y2, r2 = ball2
# Calculate distance between the two centers
distance = [Link]((x2 - x1)**2 + (y2 - y1)**2)
# Check collision condition
return distance <= (r1 + r2)
# Example usage
b1 = (2, 3, 1)
b2 = (4, 5, 2)
print(ball_collide(b1, b2)) # Output: True or False
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Output :
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
b) Find the mean, median, and mode for the given set of numbers in a list.
from statistics import mean, median, mode
def find_stats(numbers):
return mean(numbers), median(numbers), mode(numbers)
# Example usage
data = [3, 4, 5, 4, 8, 6, 4]
m, med, mo = find_stats(data)
print("Mean =", m)
print("Median =", med)
print("Mode =", mo)
Output :
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Practical - 9
a) Write a function nearly_equal to test whether two strings are nearly equal. Two strings a
and bare nearly equal when a single mutation on b can generate a.
def nearly_equal(a, b):
# If both strings are already equal
if a == b:
return True
# If length difference is more than 1, cannot be nearly equal
if abs(len(a) - len(b)) > 1:
return False
# Case 1: Replace one character (lengths equal)
if len(a) == len(b):
diff_count = 0
for i in range(len(a)):
if a[i] != b[i]:
diff_count += 1
if diff_count > 1:
return False
return True
# Case 2: Insert or Delete one character
# Make sure 'a' is the longer string
if len(a) < len(b):
a, b = b, a
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
i=j=0
mismatch_found = False
while i < len(a) and j < len(b):
if a[i] != b[j]:
if mismatch_found:
return False
mismatch_found = True
i += 1 # Skip one character in longer string
else:
i += 1
j += 1
return True
print(nearly_equal("cat", "bat"))
print(nearly_equal("cat", "cats"))
print(nearly_equal("cat", "cut"))
print(nearly_equal("cat", "at"))
print(nearly_equal("cat", "dog"))
print(nearly_equal("test", "tent"))
print(nearly_equal("abc", "abcd"))
print(nearly_equal("abc", "abxy"))
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Output :
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
b)Write a function dups to find all duplicates in tin the list.
def dups(lst):
duplicates = []
for item in lst:
if [Link](item) > 1 and item not in duplicates:
[Link](item)
return duplicates
# Example usage
my_list = [2, 4, 5, 2, 8, 4, 9, 5, 5]
print(dups(my_list))
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
c) Write a function unique to find all the unique elements of a list.
def unique(lst):
unique_list = []
for item in lst:
if [Link](item) == 1:
unique_list.append(item)
return unique_list
# Example usage
my_list = [2, 4, 5, 2, 8, 4, 9, 5, 5]
print(unique(my_list))
Output :
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Practical–10
a) Write a function cumulative_product to compute the cumulative product of a list of numbers.
Cumulative Product : It means multiplying numbers one by one and storing results
def cumulative_product(lst):
result = []
product = 1
for num in lst:
product = product * num
[Link](product)
return result
# Example usage
numbers = [2, 3, 4, 5]
print(cumulative_product(numbers))
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Output :
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
b) Write a function reverse to reverse a list. Without using the reverse function.
def reverse(lst):
rev = []
for item in lst:
[Link](0, item) # insert each item at the beginning
return rev
# Example usage
numbers = [10, 20, 30, 40, 50]
print(reverse(numbers))
Output :
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
b) Write a function to compute gcd, lcm of two numbers. Each function shouldn’t exceed one
line.
# GCD in one line
gcd = lambda a, b: a if b == 0 else gcd(b, a % b)
# LCM in one line
lcm = lambda a, b: (a * b) // gcd(a, b)
# Example usage
x = 12
y = 18
print("GCD:", gcd(x, y))
print("LCM:", lcm(x, y))
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Practical - 11
a) Install packages requests, flask and explore them. using (pip)
Step 1: Open the terminal / command prompt
Step 2: Check Python and pip
• python --version
• pip –version
Step 3: Install requests package
• pip install requests
Step 4: Install flask package
• pip install flask
Step 5: Verify installation
• pip show requests
• pip show flask
Step 6: Explore requests
import requests
# Send a GET request to a website
response = [Link]("[Link]
# Print status code and content
print("Status Code:", response.status_code)
print("Content:", [Link][:200]) # first 200 chars
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Output:
b) Plot graphs using python and Matplotlib.
c) Data Analysis using numpy and Pandas Libraries
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
b) Plot graphs using python and Matplotlib.
Step 1: pip install matplotlib in the VS Code Terminal, type:
Step 2: Create a Python file
import [Link] as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
[Link]()
[Link](x, y)
[Link]("Sample Line Graph")
[Link]("X values")
[Link]("Y values")
[Link]()
Output:
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
c) Data Analysis using numpy and pandas Libraries
import numpy as np
import pandas as pd
# --- NumPy Analysis ---
# Creating NumPy array
data = [Link]([10, 20, 30, 40, 50])
print("NumPy Array:", data)
# Basic statistics
print("Mean:", [Link](data))
print("Median:", [Link](data))
print("Standard Deviation:", [Link](data))
print("Sum:", [Link](data))
print("Max:", [Link](data))
print("Min:", [Link](data))
# --- Pandas Data Analysis ---
# Creating DataFrame
students = {
"Name": ["Amit", "Neha", "Priya", "Rohan", "Karan"],
"Marks": [85, 92, 78, 88, 95]
}
df = [Link](students)
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
print("\nDataFrame:")
print(df)
# Describe statistics
print("\nStatistical Summary:")
print([Link]())
# Maximum marks student
print("\nStudent with highest marks:")
print(df[df["Marks"] == df["Marks"].max()])
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ·VMSB Uttarakhand Technical University ·Sri Dev Suman Uttarakhand University ·Uttarakhand Board of Technical Education
Output :
Vision Dhoolkot, P.O. Selaqui, ChakrataRaod
• To emerge as an academic centre producing world class professionals promoting innovation and research. Dehradun-248011 (U.K. India)
Mission:
[Link]
• To Promote intellectual and skilled human capital generation employment and entrepreneurship.
info@[Link]
• To Be educational centre of excellence of multi ethnicity and diversity.
• To Establish as technology driven teaching learning institution.
0135-2699300
• To Provide world class platform for research and innovation.
0135-2699309
• To Inculcate social, environmental, heritage values.