PYTHON PROGRAMMING
LABORATORY
Subject Code: LPEEC-110
JULY – DECEMBER 2024
SUBMITTED TO: SUBMITTED BY:
Dr. Narwant Singh Grewal Akash Kumar
URN: 2104355
DEPARTMENT OF ELECTRONICS & COMMUNICATION
ENGINEERING GURU NANAK DEV ENGINEERING COLLEGE
LUDHIANA
PROGRAM-1
Write a program to print basics information of a student.
name=input("enter the student name: ")
roll_number= int(input("enter student roll number: "))
admission_year = int(input("enter your admission year: "))
branch = input("enter your branch: ")
print("student name:", name)
print("student roll number:", roll_number)
print("student admission year:", admission_year)
print("student branch:", branch)
OUTPUT:
1
PROGRAM-2
Write a program to perform basic mathematical operation on simple and complex
numbers.
num1= float(input("enter the first value number1: "))
num2= float(input("enter the second value number2: "))
#add two numbers
add = num1 + num2
# subtract num2 from num1
sub= num1 - num2
#multiply num1 with num2
multi = num1*num2
#divide num1 by num2
div = num1/num2
#modulus num1 and num2
mod = num1 % num2
#exponent of num1 and num2
expo= num1**num2
print("the sum of numbers=", add)
print("the subtraction of numbers=", sub)
2
print("the multiplication of numbers=", multi)
print("the division of numbers=", div)
print("the modulus of numbers=", mod)
print("the exponential of numbers=", expo)
OUTPUT:
num1= float(input("enter the first value number1: "))
num2= complex(input("enter the second value number2: "))
#add two numbers
add = num1 + num2
# subtract num2 from num1
sub= num1 - num2
#multiply num1 with num2
multi = num1*num2
3
#divide num1 by num2
div = num1/num2
#exponent of num1 and num2
expo= num1**num2
print("the sum of numbers=", add)
print("the subtraction of numbers=", sub)
print("the multiplication of numbers=", multi)
print("the division of numbers=", div)
print("the exponential of numbers=", expo)
OUTPUT:
4
PROGRAM-3
Write a program to display first character, last character, all characters except first
character, all characters except last character, all characters except the first and last
characters and the reverse of the given string.
def string_operations(s):
if not s:
print("The string is
empty.") return
# First character
first_char = s[0]
# Last character
last_char = s[-1]
# All characters except the first character
except_first = s[1:]
# All characters except the last character
except_last = s[:-1]
# All characters except the first and last characters
except_first_last = s[1:-1]
# Reverse of the string
5
reverse_s = s[::-1]
# Display the results
print(f"First character: {first_char}")
print(f"Last character: {last_char}")
print(f"All characters except the first character: {except_first}")
print(f"All characters except the last character: {except_last}")
print(f"All characters except the first and last characters: {except_first_last}")
print(f"Reverse of the string: {reverse_s}")
# Example usage
input_string = input("Enter a string: ")
string_operations(input_string)
OUTPUT:
6
PROGRAM-4
Write a program to calculate the simple interest.
#simple interest calculator
principle = 0
rate = 0
time = 0
while principle <=0:
principle= int(input("enter the principle: "))
if principle <=0:
print("principle cant be less tha or equal to zero: ")
while rate <=0:
rate = int(input("enter the rate: "))
if rate <=0:
print("rate cant be less tha or equal to zero: ")
while time <=0:
time = int(input("enter the time: "))
if time <=0:
print("time cant be less tha or equal to zero: ")
total = principle * pow((1 + rate / 100), time)
7
print(f"balance after {time} years : ${total:.2f}")
OUTPUT:
8
PROGRAM-5
Write a program to perform the assignment operations and bitwise operations.
#Assignment Operations
x=3
y=2
x += y
print(x)
x=4
y=5
x -= y
print(x)
x=3
y=3
x *= y
print(x)
x=3
y=2
x /= y
print(x)
9
x=3
y=2
x **= y
print(x)
x=3
y=2
x %= y
print(x)
OUTPUT:
#Bitwise Operators
a = 10
b=4
print("a & b =", a & b)
print("a | b =", a | b)
10
print("~a =", ~a)
print("a ^ b =", a ^ b)
print("a >> 1 =", a >> 1)
print("b >> 1 =", b >> 1)
a=5
b = -10
print("a << 1 =", a << 1)
print("b << 1 =", b << 1)
OUTPUT:
11
PROGRAM-6
Write a program using operators to check whether the character you entered is a
consonant or not.
char = input("enter a character: ")
if char in ('a','e','i','o','u','A','E','I','O','U'):
print("the character is vowel")
else:
print("the character is consonant")
OUTPUT:
12
PROGRAM-7
Write a program to print the table of any given number using loops.
Using For loop
num = int(input("Enter the number: "))
start = 1
end = 10
print(f"Multiplication Table for {num}")
for i in range(start, end+1):
product = num * i
print(f"{num}*{i} = {product}")
OUTPUT:
13
Using While loop
num = int(input("Enter the number: "))
i=1
end = 10
print(f"Multiplication Table for {num}")
while i <= end:
product = num * i
print(f"{num}*{i} = {product}")
i += 1
OUTPUT:
14
PROGRAM-8
Write a program to draw the given pattern using loops.
num_row = 5
for i in range(1, num_row+1):
for j in range(i):
print("*", end="")
print()
OUTPUT:
15
PROGRAM-9
Write a program to print the information of a customer using list.
customers = [{"name":"Alice", "age":30, "email":"Alice@gmail.com", "phone":"123-456-
7890"},
{"name":"Patrick", "age":32, "email":"Patrick@gmail.com", "phone":"987-654-
3210"},
{"name":"Sandy", "age":34, "email":"Sandy@gmail.com", "phone":"555-555-
5555"}]
def print_customer_info(name):
for customer in customers:
if customer["name"] == name:
print("Customer Information:")
print("Name:", customer["name"])
print("Age:", customer["age"])
print("Email:", customer["email"])
print("Phone:", customer["phone"])
return customer
else:
16
print("customer not found")
customer_name = input("Enter customer name to retrieve information: ")
print_customer_info(customer_name)
OUTPUT:
17
PROGRAM-10
Write a program to create dictionary with key as a string and perform basic string
functions.
my_dict = {}
my_dict["name"] = "John"
my_dict["age"] = "30"
my_dict["city"] = "Michigan"
print("Original Dictionary")
print(my_dict)
#1 Uppercase the name
my_dict["name"] = my_dict["name"].upper()
#2 Concentrate the name and city
my_dict["name_and_city"] = my_dict["name"] + " from " + my_dict["city"]
#3 Check if “city” key exist in dictionary
if "city"in my_dict:
print("the city key exist in my dictionary")
#4 Get the length of the name
name_length = len(my_dict["name"])
print("name length:", name_length)
18
# Display the updated dictionary
print("updated dictionary")
print(my_dict)
OUTPUT:
19
PROGRAM 11
Write a program to print credentials of a student using function only
def get_students_details():
name = input("Enter the name of the student: ")
roll_number = input("Enter student roll number: ")
age= input("Enter the age: ")
return name, roll number, age
def print_student_credentials(name, roll_number, age):
print("\nStudent Credentials: ")
print("Name:", name)
print("Roll number:", roll_number)
print("Age:", age)
def main():
print("Student Information Program")
student name, student roll_number, student age = get_students_details()
print_student_credentials(student_name, student_roll_number, student_age)