python lab
python lab
# 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
# Example usage
c1 = complex(2, 3)
c2 = complex(1, 4)
output
# 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)
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]
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