UNIT-1
1. Write a program to find the largest element among three
Numbers.
# Input from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
# Check for the largest number
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
# Output the result
print("The largest number is:", largest)
OUT PUT:
Enter the first number: 34
Enter the second number: 21
Enter the third number: 33
The largest number is: 34.0
2. Write a Program to display all prime numbers within an
interval
# Input the interval from the user
start = int(input("Enter the start of interval: "))
end = int(input("Enter the end of interval: "))
print(f"Prime numbers between {start} and {end} are:")
# Loop through the interval
for num in range(start, end + 1):
if num > 1: # 1 is not a prime number
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
break
else:
print(num, end=" ")
OUT PUT:
Enter the start of interval: 10
Enter the end of interval: 30
Prime numbers between 10 and 30 are:
11 13 17 19 23 29
3. Write a program to swap two numbers without using a
temporary variable.
# Input from the user
a = int(input("Enter first number (a): "))
b = int(input("Enter second number (b): "))
# Swapping without a temporary variable
a=a+b
b=a-b
a=a-b
# Output the result
print("After swapping:")
print("a =", a)
print("b =", b)
OUTPUT:
Enter first number (a): 10
Enter second number (b): 25
After swapping:
a = 25
b = 10
4. Demonstrate the following Operators in Python with suitable
examples.
i) Arithmetic Operators ii) Relational Operators iii) Assignment
Operatorsiv) Logical Operators v) Bit wise Operators vi) Ternary
Operator vii) Membership Operatorsviii)Identity Operators
i) Arithmetic Operators
a = 10
b=3
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
print()
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
ii) Relational Operators
a=10
b=5
print("Relational Operators:")
print("a == b:", a == b) # Equal to
print("a != b:", a != b) # Not equal to
print("a > b:", a > b) # Greater than
print("a < b:", a < b) # Less than
print("a >= b:", a >= b) # Greater than or equal to
print("a <= b:", a <= b) # Less than or equal to
print()
OUTPUT:
Relational Operators:
a == b: False
a != b: True
a > b: True
a < b: False
a >= b: True
a <= b: False
iii) Assignment Operators
print("Assignment Operators:")
x=5
print("x =", x)
x += 3 # Equivalent to x = x + 3
print("x += 3 =>", x)
x *= 2 # Equivalent to x = x * 2
print("x *= 2 =>", x)
x -= 1
print("x -= 1 =>", x)
x /= 2
print("x /= 2 =>", x)
x %= 3
print("x %= 3 =>", x)
x **= 2
print("x **= 2 =>", x)
print()
OUTPUT:
Assignment Operators:
x = 5
x += 3 => 8
x *= 2 => 16
x -= 1 => 15
x /= 2 => 7.5
x %= 3 => 1.5
x **= 2 => 2.25
iv) Logical Operators
print("Logical Operators:")
p = True
q = False
print("p and q:", p and q)
print("p or q:", p or q)
print("not p:", not p)
print()
OUTPUT:
Logical Operators:
p and q: False
p or q: True
not p: False
v) Bit wise Operators
print("Bitwise Operators:")
a=5 # 0101 in binary
b=3 # 0011 in binary
print("a & b =", a & b) # AND
print("a | b =", a | b) # OR
print("a ^ b =", a ^ b) # XOR
print("~a =", ~a) # NOT
print("a << 1 =", a << 1) # Left Shift
print("a >> 1 =", a >> 1) # Right Shift
print()
OUTPUT:
Bitwise Operators:
a & b = 1
a | b = 7
a ^ b = 6
~a = -6
a << 1 = 10
a >> 1 = 2
vi) Ternary Operator
print("Ternary Operator:")
num = 7
result = "Even" if num % 2 == 0 else "Odd"
print(f"{num} is", result)
print()
OUTPUT:
Ternary Operator:
7 is Odd
vii) Membership Operators
print("Membership Operators:")
list1 = [1, 2, 3, 4, 5]
print("3 in list1:", 3 in list1)
print("6 not in list1:", 6 not in list1)
print()
OUTPUT:
Membership Operators:
3 in list1: True
6 not in list1: True
viii)Identity Operators
print("Identity Operators:")
x = [1, 2]
y=x
z = [1, 2]
print("x is y:", x is y) # True because both refer to same object
print("x is z:", x is z) # False because they refer to different objects
print("x == z:", x == z) # True because they have the same contents
OUTPUT:
Identity Operators:
x is y: True
x is z: False
x == z: True
5. Write a program to add and multiply complex
numbers
# Input complex numbers from the user
a = complex(input("Enter the first complex number (e.g., 2+3j): "))
b = complex(input("Enter the second complex number (e.g., 1+4j): "))
# Addition
sum_result = a + b
# Multiplication
product_result = a * b
# Display results
print("\nResults:")
print("Sum of", a, "and", b, "is:", sum_result)
print("Product of", a, "and", b, "is:", product_result)
OUTPUT:
Enter the first complex number (e.g., 2+3j): 3+4j
Enter the second complex number (e.g., 1+4j): 4+5j
Results:
Sum of (3+4j) and (4+5j) is: (7+9j)
Product of (3+4j) and (4+5j) is: (-8+31j)
6. Write a program to print multiplication table of a given
number.
# Input from the user
num = int(input("Enter a number to print its multiplication table: "))
# Display the multiplication table
print(f"\nMultiplication Table for {num}:")
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
OUTPUT:
Enter a number to print its multiplication table: 6
Multiplication Table for 6:
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60