Python Programming
B.Tech. – II Year I Semester
Course Code Category Name of the Course L T P C
24SE05202S SE Python Programming 0 1 2 2
(Common for all branches of Engineering)
UNIT-I
Experiments:
1.1. Write a Python program to find the largest element among three Numbers.
1.2. Write a Python Program to display all prime numbers within an interval.
1.3. Write a Python program to swap two numbers without using a temporary variable.
1.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.
1.5. Write a program to add and multiply complex numbers
1.6. Write a program to print multiplication table of a given number.
1.1. Write a Python program to find the largest element among three
Numbers?
AIM: To develop a Python program that takes three numbers as input from
the user and determines which one is the largest among them using conditional
statements.
Description: This program takes three numbers, compares them using Python's if-
elif-else conditional statements, and then prints the largest number.
We use comparison operators:
> (greater than): To check if one number is larger than the others.
elif: To check the next possibility if the first condition is false.
Mrs.N.Tejaswini ,Assistant professor Page 1
Python Programming
else: To handle the remaining case (if the previous conditions fail).
Conditions Explained:
1. if (a >= b and a >= c):
Checks if a is greater than or equal to both b and c.
If true, a is the largest.
2. elif (b >= a and b >= c):
If a is not the largest, it checks if b is greater than or equal to both a
and c.
3. else:
If neither a nor b is the largest, then c must be the largest.
Code:
def find_largest(num1, num2, num3):
"""Finds the largest of three numbers."""
if num1 >= num2 and num1 >= num3:
largest = num1
elif num2 >= num1 and num2 >= num3:
largest = num2
else:
largest = num3
return largest
# Get input from the user
number1 = float(input("Enter the first number: "))
Mrs.N.Tejaswini ,Assistant professor Page 2
Python Programming
number2 = float(input("Enter the second number: "))
number3 = float(input("Enter the third number: "))
# Find and print the largest number
result = find_largest(number1, number2, number3)
print(f"The largest number is: {result}")
OUTPUT:
Enter the first number: 2
Enter the second number: 3
Enter the third number: 4
The largest number is: 4.0
1.2. Write a Python Program to display all prime numbers within an
interval?
AIM: To write a Python program to display all prime numbers between a user-
defined interval.
Description:
A prime number is a number greater than 1 that has exactly two factors:
1 and itself.
This program:
Takes two inputs: start and end of the interval.
Checks each number in that interval.
Prints it if it is a prime number.
Explanation of the Logic:
1. Input the starting and ending numbers from the user.
2. Use a for loop to check every number in that range.
3. For each number n in the range:
o Skip if n <= 1 (not prime).
o Use a for loop from 2 to n-1 (or better: √n) to check if it has any
divisors.
o If no divisors are found, it’s a prime number.
4. Print all prime numbers found.
Mrs.N.Tejaswini ,Assistant professor Page 3
Python Programming
Code:
# Python program to display all the prime numbers within an interval.
lower = 100
upper = 200
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
OUTPUT:
Prime numbers between 100 and 200 are:
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
Mrs.N.Tejaswini ,Assistant professor Page 4
Python Programming
1.3. Write a Python program to swap two numbers without using a
temporary variable.
AIM:
To write a Python program that swaps the values of two variables without using a
third (temporary) variable.
Description:
Swapping means interchanging the values of two variables.
Python allows swapping without a third variable by using:
1. Arithmetic operations (+ and - or * and /)
2. Tuple unpacking (most Pythonic and clean way)
Code:
def swap_numbers(a, b):
"""Swaps two numbers without using a temporary variable."""
a, b = b, a
return a, b
# Get input from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Swap the numbers
num1, num2 = swap_numbers(num1, num2)
# Print the swapped numbers
print(f"After swapping: num1 = {num1}, num2 = {num2}")
OUTPUT:
Enter the first number: 23
Enter the second number: 34
After swapping: num1 = 34.0, num2 = 23.0
Mrs.N.Tejaswini ,Assistant professor Page 5
Python Programming
1.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.
1.4.i) Arithmetic Operators in Python with suitable examples
AIM:
To demonstrate the use of Arithmetic Operators in Python using examples.
# Demonstration of Arithmetic Operators in Python
# Let's define two numbers
a = 10
b=3
# Displaying the values
print("a =", a)
print("b =", b)
# 1. Addition
print("Addition (a + b):", a + b)
# 2. Subtraction
print("Subtraction (a - b):", a - b)
# 3. Multiplication
print("Multiplication (a * b):", a * b)
Mrs.N.Tejaswini ,Assistant professor Page 6
Python Programming
# 4. Division (returns float)
print("Division (a / b):", a / b)
# 5. Floor Division (returns integer part only)
print("Floor Division (a // b):", a // b)
# 6. Modulus (remainder)
print("Modulus (a % b):", a % b)
# 7. Exponentiation (a to the power of b)
print("Exponentiation (a ** b):", a ** b)
Output:
a = 10
b=3
Addition (a + b): 13
Subtraction (a - b): 7
Multiplication (a * b): 30
Division (a / b): 3.3333333333333335
Floor Division (a // b): 3
Modulus (a % b): 1
Exponentiation (a ** b): 1000
ii) Relational Operators/ Comparision operators
Explanation:
Relational (or comparison) operators are used to compare two values. The result is
always either True or False.
Code:
a = 10
b=5
print("a =", a)
Mrs.N.Tejaswini ,Assistant professor Page 7
Python Programming
print("b =", b)
print("a > b:", a > b) # Greater than
print("a < b:", a < b) # Less than
print("a == b:", a == b) # Equal to
print("a != b:", a != b) # Not equal to
print("a >= b:", a >= b) # Greater than or equal to
print("a <= b:", a <= b) # Less than or equal to
Output
a = 10
b=5
a > b: True
a < b: False
a == b: False
a != b: True
a >= b: True
a <= b: False
iii)Assignment Operators:
Explanation:
Assignment operators assign or update values. Shortcuts like += help simplify code.
Code:
x = 10
print("Initial x =", x)
x += 5 # Same as x = x + 5
print("x after += 5:", x)
x -= 3 # Same as x = x - 3
print("x after -= 3:", x)
x *= 2 # x = x * 2
Mrs.N.Tejaswini ,Assistant professor Page 8
Python Programming
print("x after *= 2:", x)
x /= 4 # x = x / 4
print("x after /= 4:", x)
x %= 3 # x = x % 3
print("x after %= 3:", x)
x **= 2 # x = x ** 2
print("x after **= 2:", x)
x //= 2 # x = x // 2
print("x after //= 2:", x)
Output
Initial x = 10
x after += 5: 15
x after -= 3: 12
x after *= 2: 24
x after /= 4: 6.0
x after %= 3: 0.0
x after **= 2: 0.0
x after //= 2: 0.0
iv) logical operators:
Explanation:
Used for combining boolean values. Returns True or False.
Code:
a = True
b = False
print("a and b:", a and b) # Returns True if both are True
print("a or b:", a or b) # Returns True if at least one is True
print("not a:", not a) # Reverses True to False
Output
Mrs.N.Tejaswini ,Assistant professor Page 9
Python Programming
a and b: False
a or b: True
not a: False
v) Bitwise operators:
Explanation:
Used for performing operations on binary bits of integers.
Code:
a = 5 # binary: 0101
b = 3 # binary: 0011
print("a & b:", a & b) # AND -> 0001 (1)
print("a | b:", a | b) # OR -> 0111 (7)
print("a ^ b:", a ^ b) # XOR -> 0110 (6)
print("~a:", ~a) # NOT -> flips bits (result: -6)
print("a << 1:", a << 1) # Left shift -> 1010 (10)
print("a >> 1:", a >> 1) # Right shift -> 0010 (2)
Output
a & b: 1
a | b: 7
a ^ b: 6
~a: -6
a << 1: 10
a >> 1: 2
vi)Terinary operators:
Explanation:
A one-line replacement for if-else.
Mrs.N.Tejaswini ,Assistant professor Page 10
Python Programming
Code:
a = 10
b = 20
# Syntax: value_if_true if condition else value_if_false
max_value = a if a > b else b
print("Larger number is:", max_value)
OUTPUT:
Larger number is: 20
vii) membership operators:
Explanation:
Checks whether a value exists inside a sequence like a list, string, or tuple.
Code:
my_list = [1, 2, 3, 4]
print("3 in my_list:", 3 in my_list) # True
print("5 not in my_list:", 5 not in my_list) # True
Output
3 in my_list: True
5 not in my_list: True
viii) Identity Operators
Explanation:
Checks whether two variables refer to the same object (same memory location).
Code:
a = [1, 2, 3]
b=a
Mrs.N.Tejaswini ,Assistant professor Page 11
Python Programming
c = [1, 2, 3]
print("a is b:", a is b) # True (same object)
print("a is c:", a is c) # False (same content, different objects)
print("a == c:", a == c) # True (same content)
print("a is not c:", a is not c) # True
OUTPUT:
a is b: True
a is c: False
a == c: True
a is not c: True
1.5. Write a Python program to add, subtract, multiply and
division of two complex numbers
Explanation:
Python provides built-in support for complex numbers.
A complex number has the form a + bj, where:
o a is the real part,
o b is the imaginary part,
o and j is the imaginary unit.
You can directly create complex numbers using complex() or by writing them
like 3 + 4j.
Python supports direct arithmetic on complex numbers using:
o + for addition
o - for subtraction
o * for multiplication
o / for division
Code:
# Program to perform arithmetic operations on complex numbers
# Taking two complex numbers
c1 = complex(4, 5) # 4 + 5j
Mrs.N.Tejaswini ,Assistant professor Page 12
Python Programming
c2 = complex(2, 3) # 2 + 3j
# Display the complex numbers
print("First Complex Number:", c1)
print("Second Complex Number:", c2)
# Addition
add_result = c1 + c2
print("Addition:", add_result)
# Subtraction
sub_result = c1 - c2
print("Subtraction:", sub_result)
# Multiplication
mul_result = c1 * c2
print("Multiplication:", mul_result)
# Division
div_result = c1 / c2
print("Division:", div_result)
Output:
First Complex Number: (4+5j)
Second Complex Number: (2+3j)
Addition: (6+8j)
Subtraction: (2+2j)
Multiplication: (-7+22j)
Division: (1.7692307692307692-0.15384615384615394j)
1.6. Write a program to print multiplication table of a given
number.
Explanation:
The program asks the user to enter a number.
Mrs.N.Tejaswini ,Assistant professor Page 13
Python Programming
A for loop from 1 to 10 is used to multiply the number with each value in that
range.
The result of each multiplication is displayed in a formatted way.
Code:
# Program to print the multiplication table of a given number
# Taking input from user
num = int(input("Enter a number to print its multiplication table: "))
# Displaying the table
print(f"\nMultiplication Table for {num}:\n")
for i in range(1, 11):
result = num * i
print(f"{num} x {i} = {result}")
Output:
Enter a number to print its multiplication table: 7
Multiplication Table for 7:
7x1=7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Mrs.N.Tejaswini ,Assistant professor Page 14