INDIRA GANDHI
DELHI TECHNICAL UNIVERSITY
FOR WOMEN
Department of B. Tech : Information Technology
PRACTICAL FILE
Semester: 1
Subject: Programming with Python
Session: 2025-26
Subject Code: BAI 101
Submitted To: Prof. Arun Sharma
Submitted By: Aabhanshi Sharma (IT-1)
I. Exercise programs on basic control structures & loops.
a) Write a program for checking the given number is even or odd.
#I. Exercise programs on basic control structures & loops
#Number odd or even.
'''n=int(input("enter the number:"))
if n==0:
print ("enter a non zero number")
elif n%2==0:
print ("the number is even")
else:
print ("the number is odd")
b)Using a for loop, write a program that prints the decimal equivalents of 1/2, 1/3, 1/4 , 1/10
# Using a for loop, print decimal equivalents of 1/2 to 1/10
for i in range(2, 11):
print(1 / i)
c) Write a program for displaying reversal of a number.
# Reversal of a number.
num = int(input("enter the number: "))
rev = 0
while num > 0:
lastDigit = num % 10
rev = rev * 10 + lastDigit
num = num // 10
print("reversed number is", rev)
d) Write a program for finding biggest number among 4 numbers.
# Biggest number among 4 numbers
a = int(input("enter number: "))
b = int(input("enter number: "))
c = int(input("enter number: "))
d = int(input("enter number: "))
print("maximum is:", max(a, b, c, d))
e) Write a program using a while loop that asks the user for a number, and prints a countdown from
that number to zero.
# Countdown program
countDown = int(input("enter the number to start the countdown: "))
while countDown >= 0:
print(countDown)
countDown -= 1
II. Exercise programs on operators & I/O operations.
a) Write a program that takes 2 numbers as command line arguments and prints its sum.
# II. Exercise programs on operators & I/O operations
# Sum of 2 numbers (command-line style example)
def add(a, b):
return a + b
print("the sum is:", add(6, 4))
b)Implement python script to show the usage of various operators available in python language.
# Arithmetic operators
a = int(input("enter a number: "))
b = int(input("enter a number: "))
print("Arithmetic operators")
print("addition:", a + b)
print("subtraction:", a - b)
print("multiplication:", a * b)
print("division:", a / b)
print("floor division:", a // b)
print("modulus:", a % b)
print("exponent:", a ** b)
# Relational Operators
a = int(input("enter a number: "))
b = int(input("enter a number: "))
print("Relational (Comparison) Operators")
print(a > b)
print(a < b)
print(a == b)
print(a != b)
print(a >= b)
print(a <= b)
# Logical Operators
a = True
b = False
print("Logical Operators")
print("AND:", a and b)
print("OR:", a or b)
print("NOT a:", not a)
# Assignment Operators
x=6
x += 4
print(x)
x -= 2
print(x)
x *= 3
print(x)
x /= 3
print(x)
x %= 5
print(x)
# Bitwise Operators
m=5
n=3
print("Bitwise Operators")
print("m & n =", m & n)
print("m | n =", m | n)
print("m ^ n =", m ^ n)
print("~m =", ~m)
print("m << 1 =", m << 1)
print("m >> 1 =", m >> 1)
# Membership Operators
list1 = [2, 5, 1, 9, 4]
print("Membership Operators")
print("5 in list1:", 5 in list1)
print("7 in list1:", 7 in list1)
c) Implement python script to read person’s age from keyboard and display whether he is eligible for voting
or not.
# Voting eligibility
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
d) Implement python script to check the given year is leap year or not.
# Leap year program
year = int(input("enter the year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
III. Exercise programs on Python Script.
a)Implement Python Script to generate first N natural numbers.
# III. Exercise programs on Python Script.
# First N natural numbers
N = int(input("enter the number of natural numbers you want to print: "))
num = 1
while num <= N:
print(num)
num += 1
b) Implement Python Script to check given number is palindrome or not.
# Palindrome number
num = int(input("enter a number: "))
num1 = num
rev = 0
while num > 0:
lastDigit = num % 10
rev = rev * 10 + lastDigit
num = num // 10
if num1 == rev:
print(num1, "is palindrome")
else:
print(num1, "is not palindrome")
c) Implement Python script to print factorial of a number.
# Factorial
num = int(input("enter a number: "))
i=1
product = 1
while i <= num:
product *= i
i += 1
print("the factorial of", num, "is:", product)
d) Implement Python Script to print sum of N natural numbers.
# Sum of N natural numbers
n = int(input("enter the number till which you want the sum: "))
i=1
s=0
while i <= n:
s += i
i += 1
print("sum is:", s)
e) Implement Python Script to check given number is Armstrong or not.
# Armstrong number
num = int(input("enter the number: "))
num1 = num
newNum = 0
power = len(str(num))
while num > 0:
lastDigit = num % 10
newNum = newNum + lastDigit ** power
num = num // 10
if newNum == num1:
print(num1, "is Armstrong.")
else:
print(num1, "is not Armstrong.")
f) Implement Python Script to generate prime numbers series up to n
# Prime numbers up to n
def isPrime(num):
if num < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
def primeNum(n):
for i in range(2, n + 1):
if isPrime(i):
print(i)
n = int(input("enter limit to generate primes: "))
print("prime numbers are:")
primeNum(n)
IV. Exercise programs on Lists.
a) Finding the sum and average of given numbers using lists.
Code:
numbers = []
n = int(input("Enter how many numbers you want: "))
for i in range(n):
num = float(input("Enter number " + str(i + 1) + ": "))
[Link](num)
total = sum(numbers)
average = total / n
print("\nNumbers entered:", numbers)
print("Sum of numbers:", total)
print("Average of numbers:", average)
b) To display elements of list in reverse order.
Code:
numbers = [10, 20, 30, 40, 50]
print("Original list:", numbers)
# M1: Using slicing
print("Reversed list (using slicing):", numbers[::-1])
# M2: Using reverse() method
[Link]()
print("Reversed list (using reverse()):", numbers)
c) Finding the minimum and maximum elements in the lists.
# Min & max
list1 = [3, 6, 5, 2, 7, 4, 9, 8]
print("minimum:", min(list1))
print("maximum:", max(list1))
V. Exercise programs on Strings.
a) Implement Python Script to perform various operations on string using string libraries.
Code:
text = "Aabhanshi Sharma"
print("Original String:", text)
print("Length of String:", len(text))
print("Uppercase:", [Link]())
print("Lowercase:", [Link]())
print("Title Case:", [Link]())
print("Reversed String:", text[::-1])
print("Count of 'l':", [Link]('l'))
print("Does string start with 'He'? ->", [Link]("Aa"))
print("Does string end with 'ld'? ->", [Link]("ma"))
print("String after replacing 'World' with 'Python':", [Link]("World", "Python"))
b) Implement Python Script to check given string is palindrome or not.
Code:
string = input("Enter a string: ")
cleaned = [Link](" ", "").lower() #spaces removed and made lowercase
if cleaned == cleaned[::-1]:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
c) Implement python script to accept line of text and find the number of characters,
number of vowels and number of blank spaces in it.
Code:
text = input("Enter a line of text: ")
characters = len(text)
vowels = 0
spaces = 0
for ch in text:
if [Link]() in 'aeiou':
vowels += 1
elif ch == ' ':
spaces += 1
print("Number of characters:", characters)
print("Number of vowels:", vowels)
print("Number of blank spaces:", spaces)
VI. Exercise programs on functions.
a) Define a function max_of_three() that takes three numbers as arguments and returns the
largest of them.
Code:
def max_of_three(a, b, c):
if a >= b and a >= c:
return a
elif b >= a and b >= c:
return b
else:
return c
# Example usage
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
print("The largest number is:", max_of_three(num1, num2, num3))
b) Write a program which makes use of function to display all such numbers which are
divisible by 7 but are not a multiple of 5, between 1000 and 2000.
Code:
def filter_numbers():
result = []
for num in range(1000, 2001):
if num % 7 == 0 and num % 5 != 0:
[Link](num)
return result
# Display the numbers
numbers = filter_numbers()
print("Numbers divisible by 7 but not a multiple of 5 between 1000 and 2000:")
print(numbers)
VII. Exercise programs on recursion & parameter passing techniques.
a) Define a function which generates Fibonacci series up to n numbers.
Code:
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
# Generate series
num = int(input("Enter how many terms: "))
print("Fibonacci Series:")
for i in range(num):
print(fibonacci(i), end=" ")
b) Define a function that checks whether the given number is Armstrong
Code:
def is_armstrong(num):
digits = str(num)
power = len(digits)
total = sum(int(d)**power for d in digits)
return total == num
# Input
n = int(input("Enter a number: "))
if is_armstrong(n):
print(n, "is an Armstrong number")
else:
print(n, "is not an Armstrong number")
c) Implement a python script for Call-by-value and Call-by-reference
Code:
# Call-by-value example (immutable: int)
def modify_value(x):
x = x + 10
print("Inside function (value):", x)
# Call-by-reference example (mutable: list)
def modify_list(lst):
[Link](99)
print("Inside function (list):", lst)
# Main Program
num = 5
my_list = [1, 2, 3]
modify_value(num)
print("Outside function (value):", num)
modify_list(my_list)
print("Outside function (list):", my_list)
d) Implement a python script for factorial of number by using recursion.
Code:
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
# Input
number = int(input("Enter a number: "))
print("Factorial:", factorial(number))
VIII. Exercise programs on Tuples.
a) Write a program which accepts a sequence of comma-separated numbers from console and
generate a list and a tuple which contains every number. Suppose the following input is supplied to
the program: 34, 67, 55, 33, 12, 98. Then, the output should be:
['34', '67', '55', '33', '12', '98'] ('34',67', '55', '33', '12', '98').
Code:
# Accept comma-separated numbers
values = input("Enter comma-separated numbers: ")
# Split into list
list_values = [Link](",")
# Convert list to tuple
tuple_values = tuple(list_values)
print("List:", list_values)
print("Tuple:", tuple_values)
b) With a given tuple (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), write a program to print the first half
values in one line and the last half values in one line.
Code:
t = (1,2,3,4,5,6,7,8,9,10)
mid = len(t) // 2 # midpoint
# First half
print("First half:", t[:mid])
# Second half
print("Second half:", t[mid:])
IX. Exercise programs on files.
a) Write Python script to display file contents.
Code:
# Create a file and write content into it
filename = "[Link]"
# Creating & writing to the file
with open(filename, "w") as file:
[Link]("This is a sample file.\n")
[Link]("Python file handling demonstration.\n")
[Link]("File created and displayed successfully.")
# Now display the contents of the file
print("---- Contents of", filename, "----")
with open(filename, "r") as file:
content = [Link]()
print(content)
b) Write Python script to copy file contents from one file to another.
Code:
# Create a source file and write content into it
source_file = "[Link]"
with open(source_file, "w") as src:
[Link]("This is the source file.\n")
[Link]("We are copying this content into another file.\n")
[Link]("Python file copy operation successful.\n")
# Destination file name
destination_file = "[Link]"
# Copy contents to destination file
with open(source_file, "r") as src:
data = [Link]()
with open(destination_file, "w") as dest:
[Link](data)
print("File copied successfully!")
print(f"Contents of '{destination_file}' are:")
# Display destination file contents
with open(destination_file, "r") as dest:
print([Link]())