Programming Lab File Sem - 2
Programming Lab File Sem - 2
Lab File Of
SRIDA AGARWAL
Enrollment No. (03716101424)
Submitted To
Bachelor Of Technology
In
Chemical Engineering
a, b = 0, 1
count = 0
if n <= 0:
print("Please enter a positive integer.")
elif n == 1:
print("Fibonacci series up to 1 term: ")
print(a)
else:
print("Fibonacci series up to n terms: ")
while count < n :
print(a, end=" ")
a, b = b, a + b
count += 1
OUTPUT -
Practical – 2
OUTPUT –
Practical – 3
if original == reversed_num:
print(f"{original} is a palindrome.")
else:
print(f"{original} is not a palindrome.")
OUTPUT –
Practical – 4
AIM - Write a recursive function to print the factorial for a given number.
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
if num < 0:
print("Factorial is not defined for negative numbers.")
else:
print(f"The factorial of {num} is: {factorial(num)}")
OUTPUT –
Practical – 5
AIM - Define a function that computes the length of a given list or string.
def mystrlen():
str = input("Enter a string: ")
print ("Length of the input string is: ", len(str))
mystrlen()
OUTPUT -
Practical – 6
AIM - Write a program that takes two lists and returns True if they have at least
one common member.
def common(list1,list2):
result = False
for x in list1:
for y in list2:
if x == y:
result = True
return result
print(common([1,2,3,4,5],[5,6,7,8,9]))
print(common([1,2,3,4,5],[6,7,8,9]))
OUTPUT –
Practical – 7
AIM - Write a Python program to print a specified list after removing the 0th,
2nd, 4th and 5th elements.
lists = ['Apple','Banana','Mango','Orange','Pineapple','Strawberry','Grapes']
lists= [x for (i,x) in enumerate(lists) if i not in (0,2,4,5)]
print (lists)
OUTPUT -
Practical – 8
OUTPUT –
Practical – 9
import operator
d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
print('Original dictionary : ',d)
sorted_d = sorted(d.items(), key=operator.itemgetter(0))
print('Dictionary in ascending order by value : ',sorted_d)
sorted_d = dict( sorted(d.items(), key=operator.itemgetter(0),reverse=True))
print('Dictionary in descending order by value : ',sorted_d)
OUTPUT –
Practical – 10
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
dic4 = {}
for d in (dic1, dic2, dic3): dic4.update(d)
print(dic4)
OUTPUT -
Practical – 11
d={'A':100,'B':540,'C':239}
print("Total sum of values in the dictionary:")
print(sum(d.values()))
OUTPUT –
Practical – 12
OUTPUT –
Practical – 13
AIM – Write a python program to get the vowel and space count in a sentence
input by the user.
OUTPUT –
Practical – 14
AIM – Write a python code to see all the arithmetic operators on 2 numbers
input by user.
addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b
modulus = a % b
floor_division = a // b
exponentiation = a ** b
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
print("Modulus:", modulus)
print("Floor Division:", floor_division)
print("Exponentiation:", exponentiation)
OUTPUT –
Practical – 15
AIM – Write a python program to get area and perimeter of square and
rectangle input by the user.
print("Square")
side = float(input("Enter the side of the square: "))
square_area = side * side
square_perimeter = 4 * side
print(f"Area of square: {square_area}")
print(f"Perimeter of square: {square_perimeter}")
print("Rectangle")
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
rectangle_area = length * width
rectangle_perimeter = 2 * (length + width)
print(f"Area of rectangle: {rectangle_area}")
print(f"Perimeter of rectangle: {rectangle_perimeter}")
OUTPUT -