Python Lab Record
Python Lab Record
NO :
Aim:
To find the largest element among three numbers.
Program:
largest = num1
largest = num2
else:
largest = num3
Expected Output:
Executed Output:
Result:
Hence, the program to find largest element among three numbers is executed
successfully.
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
Aim:
To display all prime numbers within a given interval.
Program:
start = int(input("Enter the start of the interval: "))
end = int(input("Enter the end of the interval: "))
print("Prime numbers between", start, "and", end, "are:")
for num in range(start, end + 1):
if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
break
else:
print(num, end=" ")
print()
Expected Output:
Enter the start of the interval: 10
Enter the end of the interval: 20
Prime numbers between 10 and 20 are: 11 13 17 19
Executed Output:
Enter the start of the interval: 2
Enter the end of the interval: 10
Prime numbers between 2 and 10 are: 3 5 7
Result:
Hence, all prime numbers within the given interval are displayed
successfully.
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
Aim:
To swap two numbers without using a temporary variable.
Program:
a = float(input("Enter the first number (a): "))
b = float(input("Enter the second number (b): "))
a=a+b
b=a-b
a=a-b
print("After swapping, the first number (a) is:", a)
print("After swapping, the second number (b) is:", b)
Expected Output:Enter the first number (a): 115
Enter the second number (b): 225
After swapping, the first number (a) is: 225.0
After swapping, the second number (b) is: 115.0
Executed Output:
Enter the first number (a): 115
Enter the second number (b): 225
After swapping, the first number (a) is: 225.0
After swapping, the second number (b) is: 115.0
Result:
Hence, two numbers are swapped without using a temporary
variable successfully.
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
Aim:
To demonstrate the use of different operators in Python with suitable
examples.
Program:
# Arithmetic Operators
a=8
b=4
print("Arithmetic Operators:")
print("a + b =", a + b) # Addition
print("a - b =", a - b) # Subtraction
print("a * b =", a * b) # Multiplication
print("a / b =", a / b) # Division
print("a % b =", a % b) # Modulus
print("a ** b =", a ** b) # Exponentiation
print("a // b =", a // b) # Floor Division
# Relational Operators
print("\nRelational Operators:")
print("a > b:", a > b)
print("a < b:", a < b)
print("a == b:", a == b)
print("a != b:", a != b)
# Assignment Operators
print("\nAssignment Operators:")
a += b
print("a += b:", a)
a -= b
print("a -= b:", a)
a *= b
print("a *= b:", a)
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
a /= b
print("a /= b:", a)
# Logical Operators
print("\nLogical Operators:")
print("True and False:", True and False)
print("True or False:", True or False)
print("not True:", not True)
# Bitwise Operators
print("\nBitwise Operators:")
x = 6 # 110 in binary
y = 3 # 011 in binary
print("x & y:", x & y)
print("x | y:", x | y)
print("x ^ y:", x ^ y)
print("~x:", ~x)
print("x << 1:", x << 1)
print("x >> 1:", x >> 1)
# Ternary Operator
print("\nTernary Operator:")
max_value = a if a > b else b
print("The larger value is:", max_value)
# Membership Operators
print("\nMembership Operators:")
list_example = [1, 2, 3, 4, 5]
print("3 in list_example:", 3 in list_example)
print("6 not in list_example:", 6 not in list_example)
# Identity Operators
print("\nIdentity Operators:")
a = 10
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
b = 10
print("a is b:", a is b)
print("a is not b:", a is not b)
Output:
Arithmetic Operators:
a + b = 12
a-b=4
a * b = 32
a / b = 2.0
a%b=0
a ** b = 4096
a // b = 2
Relational Operators:
a > b: True
a < b: False
a == b: False
a != b: True
Assignment Operators:
a += b: 12
a -= b: 8
a *= b: 32
a /= b: 8.0
Logical Operators:
True and False: False
True or False: True
not True: False
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
Bitwise Operators:
x & y: 2
x | y: 7
x ^ y: 5
~x: -7
x << 1: 12
x >> 1: 3
Ternary Operator:
The larger value is: 12
Membership Operators:
3 in list_example: True
6 not in list_example: True
Identity Operators:
a is b: True
a is not b: False
Result:
Hence, program to demonstrate the use of different operators in
Python with suitable examples is executed successfully.
Aim:
To add and multiply two complex numbers.
Program:
c1 = complex(input("Enter the first complex number (e.g., 3+4j): "))
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
Aim:
To print the multiplication table of a given number.
Program:
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
2 x 9 = 18
2 x 10 = 20
Result:
Hence, the multiplication table of the given number is displayed
successfully.
Aim:
Write a program to define a function that returns multiple values.
Program:
def arithmetic_operations(a, b):
addition = a + b
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
subtraction = a - b
multiplication = a * b
division = a / b if b != 0 else "Division by zero not allowed"
return addition, subtraction, multiplication, division
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
add, sub, mul, div = arithmetic_operations(num1, num2)
print(f"Addition: {add}")
print(f"Subtraction: {sub}")
print(f"Multiplication: {mul}")
print(f"Division: {div}")
Expected Output:
Enter the first number: 10
Enter the second number: 5
Addition: 15.0
Subtraction: 5.0
Multiplication: 50.0
Division: 2.0
Executed Output:
Enter the first number: 10
Enter the second number: 5
Addition: 15.0
Subtraction: 5.0
Multiplication: 50.0
Division: 2.0
Result:
Hence, the function returns multiple values successfully.
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
Aim:
Write a program to define a function with default arguments.
Program:
def greet(name="User", message="Welcome!"):
print(f"Hello, {name}! {message}")
user_name = input("Enter your name (leave blank for default): ")
user_message = input("Enter your message (leave blank for
default): ")
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
Aim:
Write a program to find the length of the string without using any
library functions.
Program:
def string_length(s):
length = 0
for _ in s:
length += 1
return length
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
Aim:
Write a program to check if a substring is present in a given string.
Program:
def is_substring_present(main_str, sub_str):
return sub_str in main_str
main_string = input("Enter the main string: ")
substring = input("Enter the substring to search: ")
if is_substring_present(main_string, substring):
print("Substring is present in the main string.")
else:
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
Aim:
Write a program to perform the following operations on a list:
Addition, Insertion, and Slicing.
Program:
user_list = list(map(int, input("Enter elements of the list separated
by spaces: ").split()))
element_to_add = int(input("Enter an element to add to the list: "))
user_list.append(element_to_add)
element_to_insert = int(input("Enter an element to insert: "))
position_to_insert = int(input("Enter the position at which to insert
the element: "))
user_list.insert(position_to_insert, element_to_insert)
start_index = int(input("Enter the starting index for slicing: "))
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
Aim:
Write a program to demonstrate any 5 built-in functions on a list.
Program:
user_list = list(map(int, input("Enter elements of the list separated
by spaces: ").split()))
max_value = max(user_list)
min_value = min(user_list)
sorted_list = sorted(user_list)
length_of_list = len(user_list)
sum_of_elements = sum(user_list)
print(f"Maximum value: {max_value}")
print(f"Minimum value: {min_value}")
print(f"Sorted list: {sorted_list}")
print(f"Length of the list: {length_of_list}")
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
Result: The program correctly checks and verifies if the key exists
in the dictionary.
PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :
PYTHON PROGRAMMING
CSE DEPARTMENT