Prem’s Python Lab Programs
G. Pullaiah College of Engineering and Technology (Autonomous)
Department of Computer Science and Engineering
Python Programming (Skill Enhancement Course R23)
Unit – 1
# 1.Write a program to find the largest element among three Numbers.
# Input: three numbers
number1 = float(input("Enter the first number: "))
number2 = float(input("Enter the second number: "))
number3 = float(input("Enter the third number: "))
# Determine the largest number
if number1 >= number2 and number1 >= number3:
largest_number = number1
elif number2 >= number1 and number2 >= number3:
largest_number = number2
else:
largest_number = number3
# Display the largest number
print("The largest is ",largest_number)
'''
OUTPUT:
Enter the first number: 5
Enter the second number: 2
Enter the third number: 7
The largest is 7.0
'''
#2. Write a Program to display all prime numbers within an interval
# Input: Start and end of the interval
start = int(input("Enter the start of the interval: "))
end = int(input("Enter the end of the interval: "))
print(f"Prime numbers between {start} and {end} are:")
# Loop through the interval
for num in range(start, end + 1):
if num > 1: # Prime numbers are greater than 1
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
Prem’s Python Lab Programs
is_prime = False
break
if is_prime:
print(num)
'''OUTPUT
Enter the start of the interval: 5
Enter the end of the interval: 20
Prime numbers between 5 and 20 are:
5
7
11
13
17
19
'''
#3. Write a program to swap two numbers without using a temporary
variable
# f in print denotes formetted string
# Input: Two numbers
a = float(input("Enter the first number (a): "))
b = float(input("Enter the second number (b): "))
# Display numbers before swapping
print(f"Before swapping: a = {a}, b = {b}")
# Swap the numbers without using a temporary variable
a=a+b
b=a-b
a=a-b
# Display numbers after swapping
print(f"After swapping: a = {a}, b = {b}")
'''OUTPUT:
Enter the first number (a): 5
Enter the second number (b): 10
Before swapping: a = 5.0, b = 10.0
After swapping: a = 10.0, b = 5.0
"""
#4. Demonstrate the following Operators in Python with suitable examples.
i) Arithmetic Operators ii) Relational Operators iii) Assignment Operators iv)
Logical Operators v) Bit wise Operators vi) Ternary Operator vii) Membership
Operators viii) Identity Operators
Prem’s Python Lab Programs
# Demonstration of Arithmetic Operators
a = 10
b=3
print("Arithmetic Operators:")
print(f"a + b = {a + b}") # Addition
print(f"a - b = {a - b}") # Subtraction
print(f"a * b = {a * b}") # Multiplication
print(f"a / b = {a / b}") # Division
print(f"a % b = {a % b}") # Modulus
print(f"a ** b = {a ** b}") # Exponentiation
print(f"a // b = {a // b}") # Floor Division
print()
# Demonstration of Relational Operators
print("Relational Operators:")
print(f"a > b = {a > b}") # Greater than
print(f"a < b = {a < b}") # Less than
print(f"a == b = {a == b}") # Equal to
print(f"a != b = {a != b}") # Not equal to
print(f"a >= b = {a >= b}") # Greater than or equal to
print(f"a <= b = {a <= b}") # Less than or equal to
print()
# Demonstration of Assignment Operators
c=5
print("Assignment Operators:")
print(f"c = {c}") # Assign
c += 2
print(f"c += 2: {c}") # Add and assign
c -= 1
print(f"c -= 1: {c}") # Subtract and assign
c *= 3
print(f"c *= 3: {c}") # Multiply and assign
c /= 4
print(f"c /= 4: {c}") # Divide and assign
c %= 2
print(f"c %= 2: {c}") # Modulus and assign
c **= 3
print(f"c **= 3: {c}") # Exponentiation and assign
c //= 2
print(f"c //= 2: {c}") # Floor Division and assign
print()
# Demonstration of Logical Operators
x = True
y = False
print("Logical Operators:")
print(f"x and y = {x and y}") # Logical AND
print(f"x or y = {x or y}") # Logical OR
print(f"not x = {not x}") # Logical NOT
Prem’s Python Lab Programs
print()
# Demonstration of Bitwise Operators
m = 5 # (binary: 0101)
n = 3 # (binary: 0011)
print("Bitwise Operators:")
print(f"m & n = {m & n}") # Bitwise AND
print(f"m | n = {m | n}") # Bitwise OR
print(f"m ^ n = {m ^ n}") # Bitwise XOR
print(f"~m = {~m}") # Bitwise NOT
print(f"m << 1 = {m << 1}") # Bitwise left shift
print(f"m >> 1 = {m >> 1}") # Bitwise right shift
print()
# Demonstration of Ternary Operator (Conditional Expression)
a = 10
b = 20
print("Ternary Operator:")
largest = a if a > b else b
print(f"The largest number between {a} and {b} is: {largest}")
print()
# Demonstration of Membership Operators
string = "Hello"
print("Membership Operators:")
print(f"'H' in string: {'H' in string}") # Check if 'H' is in string
print(f"'Z' in string: {'Z' in string}") # Check if 'Z' is in string
print(f"'H' not in string: {'H' not in string}") # Check if 'H' is not in string
print()
# Demonstration of Identity Operators
x = [1, 2, 3]
y = [1, 2, 3]
z=x
print("Identity Operators:")
print(f"x is y: {x is y}") # Check if x and y are the same object (False)
print(f"x is z: {x is z}") # Check if x and z are the same object (True)
print(f"x is not y: {x is not y}") # Check if x and y are not the same object (True)
print(f"x == y: {x == y}") # Check if x and y have the same content (True)
'''OUTPUT:
Arithmetic Operators:
a + b = 13
a-b=7
a * b = 30
a / b = 3.3333333333333335
a%b=1
a ** b = 1000
a // b = 3
Relational Operators:
Prem’s Python Lab Programs
a > b = True
a < b = False
a == b = False
a != b = True
a >= b = True
a <= b = False
Assignment Operators:
c=5
c += 2: 7
c -= 1: 6
c *= 3: 18
c /= 4: 4.5
c %= 2: 0.5
c **= 3: 0.125
c //= 2: 0.0
Logical Operators:
x and y = False
x or y = True
not x = False
Bitwise Operators:
m&n=1
m|n=7
m^n=6
~m = -6
m << 1 = 10
m >> 1 = 2
Ternary Operator:
The largest number between 10 and 20 is: 20
Membership Operators:
'H' in string: True
'Z' in string: False
'H' not in string: False
Identity Operators:
x is y: False
x is z: True
x is not y: True
x == y: True
'''
# 5.Write a program to add and multiply complex numbers
# Input: Two complex numbers
complex1 = complex(input("Enter the first complex number (e.g., 2+3j): "))
complex2 = complex(input("Enter the second complex number (e.g., 1+2j): "))
Prem’s Python Lab Programs
# Addition of complex numbers
addition_result = complex1 + complex2
# Multiplication of complex numbers
multiplication_result = complex1 * complex2
# Display results
print(f"Addition of {complex1} and {complex2} = {addition_result}")
print(f"Multiplication of {complex1} and {complex2} = {multiplication_result}")
'''OUTPUT:
Enter the first complex number (e.g., 2+3j): 4+3j
Enter the second complex number (e.g., 1+2j): 2+1j
Addition of (4+3j) and (2+1j) = (6+4j)
Multiplication of (4+3j) and (2+1j) = (5+10j)
'''
# 6. Write a program to print multiplication table of a given number
# Input: The number for which the multiplication table will be printed
number = int(input("Enter the number for which you want the multiplication table: "))
# Printing the multiplication table
print(f"Multiplication Table of {number}:")
for i in range(1, 11):
print(f"{number} x {i} = {number * i}")
''' OUTPUT
Enter the number for which you want the multiplication table: 5
Multiplication Table of 5:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
'''