0% found this document useful (0 votes)
30 views9 pages

Unit - 1

The document contains a series of Python programs demonstrating basic programming concepts. It includes programs for finding the largest of three numbers, displaying prime numbers in an interval, swapping two numbers, demonstrating various operators, adding and multiplying complex numbers, and printing a multiplication table. Each program is accompanied by input prompts and output statements to illustrate their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views9 pages

Unit - 1

The document contains a series of Python programs demonstrating basic programming concepts. It includes programs for finding the largest of three numbers, displaying prime numbers in an interval, swapping two numbers, demonstrating various operators, adding and multiplying complex numbers, and printing a multiplication table. Each program is accompanied by input prompts and output statements to illustrate their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT-1

1.program to Find the Largest Element Among Three


Numbers

# Program to find the largest among three numbers


# Input three numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
# Find the largest number
if num1>= num2 and num1 >= num3: largest=num1
elif num2>=num1 and num2 >= num3: largest=num2
else: largest=num3
print("The largest number is", largest)
2. Program to Display All Prime Numbers Within an
Interva
# Program to display all prime numbers within an
interval
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
# Input interval
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:")
for num in range(start, end + 1):
if is_prime(num):
print(num)
3. Program to Swap Two Numbers Without Using a
Temporary Variable

# Program to swap two numbers without using a


temporary variable
# Input two numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Swap numbers
num1, num2 = num2, num1
print("After swapping:")
print("First number:", num1)
print("Second number:", num2)
4. Demonstrate Various Operators in Python

# Demonstrating various operators in Python


# i) 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
# ii) Relational Operators
print("\nRelational Operators:")
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
print(f"a < b: {a < b}") # Less than
print(f"a >= b: {a >= b}") # Greater than or equal to
print(f"a <= b: {a <= b}") # Less than or equal to
# iii) Assignment Operators
print("\nAssignment Operators:")
a += b
print(f"a += b: {a}")
a -= b
print(f"a -= b: {a}")
a *= b
print(f"a *= b: {a}")
a /= b
print(f"a /= b: {a}")
a %= b
print(f"a %= b: {a}")
a **= b
print(f"a **= b: {a}")
a //= b
print(f"a //= b: {a}")
# iv) Logical Operators
print("\nLogical Operators:")
x = True
y = False
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
# v) Bitwise Operators
print("\nBitwise Operators:")
a = 10 # 1010 in binary
b = 4 # 0100 in binary
print(f"a & b: {a & b}") # Bitwise AND
print(f"a | b: {a | b}") # Bitwise OR
print(f"a ^ b: {a ^ b}") # Bitwise XOR
print(f"~a: {~a}") # Bitwise NOT
print(f"a << 2: {a << 2}") # Bitwise left shift
print(f"a >> 2: {a >> 2}") # Bitwise right shift
# vi) Ternary Operator
print("\nTernary Operator:")
a, b = 10, 20
max_num = a if a > b else b
print(f"Maximum of {a} and {b} is {max_num}")
# vii) Membership Operators
print("\nMembership Operators:")
lst = [1, 2, 3, 4, 5]
print(f"3 in list: {3 in lst}")
print(f"6 not in list: {6 not in lst}")
# viii) Identity Operators
print("\nIdentity Operators:")
a=5
b=5
print(f"a is b: {a is b}")
print(f"a is not b: {a is not b}")
c = [1, 2, 3]
d = [1, 2, 3]
print(f"c is d: {c is d}")
print(f"c is not d: {c is not d}")

5. Program to Add and Multiply Complex Numbers

# Program to add and multiply complex numbers


# Input two complex numbers
num1 = complex(input("Enter first complex number (in
form x+yj): "))
num2 = complex(input("Enter second complex number
(in form x+yj): "))
# Add complex numbers
sum_result = num1 + num2
# Multiply complex numbers
mul_result = num1 * num2
print(f"Sum of {num1} and {num2} is {sum_result}")
print(f"Product of {num1} and {num2} is {mul_result}")

6. Program to Print Multiplication Table of a Given


Number

# Program to print multiplication table of a given


number
# Input a number
num=int(input("Enter a number: "))
print(f"Multiplication table of {num}:")
for i in range(1,11):
print(f"{num} x {i}={num *i}")

You might also like