Unit - I
# Example usage
1. Find the largest element among three numbers. a=5
def find_largest(a, b, c): b = 10
if a >= b and a >= c: a, b = swap_numbers(a, b)
return a print(f"After swapping: a = {a}, b = {b}")
elif b >= a and b >= c:
return b output
else:
return c After swapping: a = 10, b = 5
# Example usage
a=5 4. Demonstrate the following Operators in Python
b = 10 with suitable examples.
c=3
print(f"The largest number among {a}, {b}, i) Arithmetic Operators
and {c} is {find_largest(a, b, c)}")
a = 10
output b=5
The largest number among 5, 10, and 3 is 10 print(f"Addition: {a} + {b} = {a + b}")
print(f"Subtraction: {a} - {b} = {a - b}")
2. Display all prime numbers within an interval. print(f"Multiplication: {a} * {b} = {a * b}")
def is_prime(num): print(f"Division: {a} / {b} = {a / b}")
if num <= 1: print(f"Modulus: {a} % {b} = {a % b}")
return False print(f"Exponentiation: {a} ** {b} = {a **
for i in range(2, int(num ** 0.5) + 1): b}")
if num % i == 0: print(f"Floor Division: {a} // {b} = {a // b}")
return False
return True output
def display_primes(start, end): Addition: 10 + 5 = 15
primes = [] Subtraction: 10 - 5 = 5
for num in range(start, end + 1): Multiplication: 10 * 5 = 50
if is_prime(num): Division: 10 / 5 = 2.0
primes.append(num) Modulus: 10 % 5 = 0
return primes Exponentiation: 10 ** 5 = 100000
Floor Division: 10 // 5 = 2
# Example usage
start = 10 ii) Relational Operators
end = 50
print(f"Prime numbers between {start} and print(f"{a} > {b} = {a > b}")
{end} are: {display_primes(start, end)}") print(f"{a} < {b} = {a < b}")
print(f"{a} == {b} = {a == b}")
output print(f"{a} != {b} = {a != b}")
print(f"{a} >= {b} = {a >= b}")
Prime numbers between 10 and 50 are: [11, print(f"{a} <= {b} = {a <= b}")
13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
output
3. Swap two numbers without using a temporary
variable. 10 > 5 = True
def swap_numbers(a, b): 10 < 5 = False
a, b = b, a 10 == 5 = False
return a, b 10 != 5 = True
10 >= 5 = True
10 <= 5 = False output
iii) Assignment Operators a & b: 0
a | b: 15
c=a a ^ b: 15
print(f"c = {c}") ~a: -11
c += b a << 1: 20
print(f"c += {b}: {c}") a >> 1: 5
c -= b
print(f"c -= {b}: {c}") vi) Ternary Operator
c *= b
print(f"c *= {b}: {c}") max_val = a if a > b else b
c /= b print(f"The greater value between {a} and {b}
print(f"c /= {b}: {c}") is {max_val}")
c %= b
print(f"c %= {b}: {c}") output
c **= b The greater value between 10 and 5 is 10
print(f"c **= {b}: {c}")
c //= b vii) Membership Operators
print(f"c //= {b}: {c}") list_numbers = [1, 2, 3, 4, 5]
print(f"3 in list_numbers: {3 in
output list_numbers}")
print(f"6 in list_numbers: {6 in
c = 10 list_numbers}")
c += 5: 15
c -= 5: 10 output
c *= 5: 50
c /= 5: 10.0 3 in list_numbers: True
c %= 5: 0.0 6 in list_numbers: False
c **= 5: 0.0
c //= 5: 0.0 viii) Identity Operators
iv) Logical Operators x = [1, 2, 3]
y = [1, 2, 3]
print(f"a > b and a > 0: {a > b and a > 0}") z=x
print(f"a < b or a > 0: {a < b or a > 0}") print(f"x is z: {x is z}")
print(f"not(a > b): {not(a > b)}") print(f"x is y: {x is y}")
print(f"x == y: {x == y}")
output
output
a > b and a > 0: True x is z: True
a < b or a > 0: True x is y: False
not(a > b): False x == y: True
v) Bitwise Operators 4. Add and multiply complex numbers.
print(f"a & b: {a & b}") def add_complex(c1, c2):
print(f"a | b: {a | b}") return c1 + c2
print(f"a ^ b: {a ^ b}")
print(f"~a: {~a}")
print(f"a << 1: {a << 1}")
print(f"a >> 1: {a >> 1}") def multiply_complex(c1, c2):
return c1 * c2
# Example usage
c1 = complex(2, 3)
c2 = complex(1, 4)
print(f"Addition of {c1} and {c2} is
{add_complex(c1, c2)}")
print(f"Multiplication of {c1} and {c2} is
{multiply_complex(c1, c2)}")
output
Addition of (2+3j) and (1+4j) is (3+7j)
Multiplication of (2+3j) and (1+4j) is (-10+11j)
5. Print multiplication table of a given number
def print_multiplication_table(n, up_to=10):
for i in range(1, up_to + 1):
print(f"{n} x {i} = {n * i}")
# Example usage
n=5
print(f"Multiplication table of {n}:")
print_multiplication_table(n)
output
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
Unit-II output
Length of the string: 13
6. Function with Multiple Return Values
def calculate_area_perimeter(length, width): 10. Check if Substring is Present in Given String
area = length * width def is_substring_present(main_string, sub_string):
perimeter = 2 * (length + width) main_len = len(main_string)
return area, perimeter sub_len = len(sub_string)
# Example usage for i in range(main_len - sub_len + 1):
length = 5 if main_string[i:i+sub_len] == sub_string:
width = 3 return True
area, perimeter = calculate_area_perimeter(length, return False
width)
print(f"Area: {area}, Perimeter: {perimeter}") # Example usage
main_string = "Hello, world!"
output sub_string = "world"
Area: 15, Perimeter: 16 print(f"Is '{sub_string}' present in '{main_string}':
{is_substring_present(main_string, sub_string)}")
7. Function with Default Arguments
def greet(name, message="Hello"): output
return f"{message}, {name}!" Is 'world' present in 'Hello, world!': True
# Example usage 11. Operations on a List
print(greet("Alice")) # Initializing the list
print(greet("Bob", "Good morning")) my_list = [1, 2, 3, 4, 5]
Output: # i. Addition
Hello, Alice! # Adding an element to the end of the list
Good morning, Bob! my_list.append(6)
print("After addition:", my_list)
8. Find Length of String Without Using Library
Functions # ii. Insertion
def string_length(s): # Inserting an element at a specific index
length = 0 my_list.insert(3, 10) # Inserting 10 at index 3
for _ in s: print("After insertion:", my_list)
length += 1
return length # iii. Slicing
# Slicing the list
# Example usage sliced_list = my_list[2:5]
s = "Hello, world!" print("Sliced list:", sliced_list)
print(f"Length of the string: {string_length(s)}")
output
9. Find Length of String Without Using Library After addition: [1, 2, 3, 4, 5, 6]
Functions After insertion: [1, 2, 3, 10, 4, 5, 6]
def string_length(s): Sliced list: [3, 10, 4]
length = 0
for _ in s: 12. Perform 5 Built-in Functions on a List
length += 1 # Initializing the list
return length sample_list = [4, 7, 1, 8, 3, 6, 2]
# Example usage # 1. len()
s = "Hello, world!" length_of_list = len(sample_list)
print(f"Length of the string: {string_length(s)}") print("Length of the list:", length_of_list)
# 2. max() print(vowel_count)
max_element = max(sample_list)
print("Maximum element in the list:", max_element) output
3
# 3. min()
15. Write a program to check if a given key exists
min_element = min(sample_list)
in a dictionary or not.
print("Minimum element in the list:", min_element)
PYTHON CODE
# 4. sorted() # Define the dictionary
sorted_list = sorted(sample_list) sample_dict = {"name": "John", "age": 21,
print("Sorted list:", sorted_list) "college": "ASTC College"}
# 5. sum() # Check if the key exists
sum_of_elements = sum(sample_list) key_to_check = "age"
print("Sum of elements in the list:", sum_of_elements) exists = key_to_check in sample_dict
output
# Print whether the key exists
print(f"Key '{key_to_check}' exists in the
Length of the list: 7 dictionary: {exists}")
Maximum element in the list: 8
Minimum element in the list: 1 output
Sorted list: [1, 2, 3, 4, 6, 7, 8] Key 'age' exists in the dictionary: True
Sum of elements in the list: 31
16. Write a program to add a new key-value pair
III unit to an existing dictionary.
PYTHON CODE
13. Write a program to create tuples (name, age, # Define the dictionary
address, college) for at least two members sample_dict = {"name": "John", "age": 21,
and concatenate the tuples and print the "college": "ASTC College"}
concatenated tuples.
# Add a new key-value pair
PYTHON CODE sample_dict["address"] = "123 Main St"
# Define the tuples # Print the updated dictionary
member1 = ("John", 21, "123 Main St", "ABC print(sample_dict)
College") output
member2 = ("Alice", 22, "456 Elm St", "XYZ {'name': 'John', 'age': 21, 'college': 'ASTC College',
University") 'address': '123 Main St'}
# Concatenate the tuples
concatenated_tuple = member1 + member2 17. Write a program to sum all the items in a given
# Print the concatenated tuple dictionary.
print(concatenated_tuple)
PYTHON CODE
output # Define the dictionary with numerical values
('John', 21, '123 Main St', 'ABC College', 'Alice', 22, sample_dict = {"item1": 10, "item2": 20, "item3":
'456 Elm St', 'XYZ University') 30}
14. Write a program to count the number of # Sum all the items in the dictionary
vowels in a string (No control flow allowed). total_sum = sum(sample_dict.values())
PYTHON CODE
# Print the sum of the items
# Define the string print(total_sum)
input_string = "Hello World"
# Count vowels output
vowel_count = sum(map(input_string.lower().count, 60
"aeiou"))
# Print the number of vowels