1.
Define a function with multiple return values
def calculate(a, b):
addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b if b != 0 else None
return addition, subtraction, multiplication, division
# Testing the function
a, b = 10, 5
add, sub, mul, div = calculate(a, b)
print("Addition:", add)
print("Subtraction:", sub)
print("Multiplication:", mul)
print("Division:", div)
2. Define a function using default arguments
def greet(name="Guest", message="Welcome"):
print(f"Hello, {name}! {message}")
# Testing the functiongreet("Alice", "Good to see you!")
greet("Bob")
greet() # Uses both default values
3. Find the length of a string without using any library functions
def string_length(s):
length = 0
for char in s:
length += 1
return length
# Testing the function
test_string = "Hello, World!"
print("Length of the string:", string_length(test_string))
4. Check if a substring is present in a given string
def is_substring_present(main_string, substring):
return substring in main_string
# Testing the function
main_string = "OpenAI is creating advanced AI models."
substring = "AI"
print("Is substring present?", is_substring_present(main_string, substring))
5. Perform addition, insertion, and slicing on a list
my_list = [1, 2, 3, 4, 5]
# i. Addition
my_list.append(6)
print("After addition:", my_list)
# ii. Insertion
my_list.insert(2, 10) # Insert 10 at index 2
print("After insertion:", my_list)
# iii. Slicing
sliced_list = my_list[1:4] # Slice from index 1 to 3
print("Sliced list:", sliced_list)
6. Perform any 5 built-in functions on a list
my_list = [5, 2, 9, 1, 5]
# 1. Length of the list
print("Length of the list:", len(my_list))
# 2. Maximum element
print("Maximum element:", max(my_list))
# 3. Minimum element
print("Minimum element:", min(my_list))
# 4. Sum of elements
print("Sum of elements:", sum(my_list))
# 5. Sorting the list
sorted_list = sorted(my_list)
print("Sorted list:", sorted_list)