Unit 2
Strings, Functions, Lists
7. Write a program to define a function with multiple return values.
Aim:
To write a Python function that takes two numbers and returns their sum, difference, and product.
Algorithm:
1. Start.
2. Define a function calculate(a, b) with two parameters.
3. Inside the function:
4. Compute sum = a + b
5. Compute diff = a - b
6. Compute prod = a * b
7. Return all three values using a tuple.
8. Call the function and unpack the results into variables.
9. Print the results.
10. End.
Program :
# Function with multiple return values
def calculate(a, b):
sum_val = a + b
diff_val = a - b
prod_val = a * b
# Returning multiple values
return sum_val, diff_val, prod_val
# Taking inputs
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
# Function call and unpacking return values
s, d, p = calculate(x, y)
# Display results
print(f"Sum = {s}")
print(f"Difference = {d}")
print(f"Product = {p}")
Output:
Sum = xxxx
Difference = xxxx
Product = xxxxx
Result:
8. Write a program to define a function using default arguments.
Aim:
To write a Python function that greets a user with a default message if no name is provided.
Algorithm:
1. Start.
2. Define a function greet() with a parameter name having a default value "Guest".
3. If the user provides a name → use it.
4. Otherwise, use the default value "Guest".
5. Print a greeting message.
6. End.
Program :
# Function with default argument
def greet(name="Guest"):
print(f"Hello, {name}! Welcome to Python programming.")
# Calling function with a name
greet("Satish")
# Calling function without a name (uses default value)
greet()
Output:
Hello, xxxxxxx! Welcome to Python programming.
Hello, xxxxx! Welcome to Python programming.
Result
9. Write a program to find the length of the string without using any library functions.
Aim:
To write a Python program that calculates the length of a string without using built-in functions.
Algorithm:
1. Start.
2. Take a string as input from the user.
3. Initialize a counter variable count = 0.
4. Loop through each character in the string:
o For every character, increase the counter by 1.
5. After the loop ends, count will store the length of the string.
6. Print the result.
7. End.
Program :
# Program to find length of string without using len()
# Input string
s = input("Enter a string: ")
# Initialize counter
count = 0
# Loop through each character
for ch in s:
count += 1 # Increment counter
# Print result
print("Length of the string is:", count)
Output:
Enter a string: xxxxxxx
Length of the string is: xxxxx
Result:
10. Write a program to check if the substring is present in a given string or not.
Aim:
To write a Python program that checks if a given substring is present in a string.
Algorithm:
1. Start.
2. Read a string (main string) from the user.
3. Read another string (substring) from the user.
4. Use the in operator to check if the substring exists in the main string.
5. If true, print "Substring is present".
6. Otherwise, print "Substring is not present".
7. End.
Program :
# Program to check if a substring is present in a given string
# Input from user
main_string = input("Enter the main string: ")
sub_string = input("Enter the substring to check: ")
# Check if substring is present
if sub_string in main_string:
print(f"Yes, '{sub_string}' is present in the string.")
else:
print(f"No, '{sub_string}' is NOT present in the string.")
Output:
Enter the main string:
Enter the substring to check:
xxx, 'xxxxx' is xxx present in the string.
Result:
11. Write a program to perform the given operations on a list:
i. addition ii. Insertion iii. Slicing
Aim:
To write a Python program to perform operations on a list such as addition, insertion, and slicing.
Algorithm:
1. Start.
2. Create a list with some predefined elements.
3. Perform addition of a new element using append().
4. Insert an element at a specific index using insert(position, value).
5. Perform slicing of the list using [start:end].
6. Print results after each operation.
7. End.
Program :
# Step 1: Create a predefined list
numbers = [10, 20, 30, 40, 50]
print("Original List:", numbers)
# Step 2: Addition (append an element at the end)
[Link](60)
print("After Addition (append 60):", numbers)
# Step 3: Insertion (insert at a specific position)
[Link](2, 25) # Insert 25 at index 2
print("After Insertion (insert 25 at index 2):", numbers)
# Step 4: Slicing (extract part of the list)
sliced_list = numbers[1:4] # Elements from index 1 to 3
print("Sliced List (from index 1 to 3):", sliced_list)
Output:
Original List: XXXXXX
After Addition (append 60): XXXXX
After Insertion (insert 25 at index 2): XXXXXX
Sliced List (from index 1 to 3): XXXXX
Result:
12. Write a program to perform any 5 built-in functions by taking any list
Aim:
To write a Python program that demonstrates five built-in functions on a list.
Algorithm:
1. Start.
2. Define a list with some values.
3. Perform 5 different built-in list functions:
4. len() → To find the length of the list.
5. max() → To find the maximum element.
6. min() → To find the minimum element.
7. sum() → To calculate the sum of elements.
8. sorted() → To return a sorted version of the list.
9. Print results of each function.
10. End
Program :
# Step 1: Create a sample list
numbers = [10, 50, 30, 20, 40]
print("Original List:", numbers)
# Step 2: Perform built-in functions
print("Length of list:", len(numbers)) # len()
print("Maximum element:", max(numbers)) # max()
print("Minimum element:", min(numbers)) # min()
print("Sum of elements:", sum(numbers)) # sum()
print("Sorted list:", sorted(numbers)) # sorted()
Output:
Original List: [10, 50, 30, 20, 40]
Length of list: XXXX
Maximum element: XXXX
Minimum element: XXXXX
Sum of elements: XXXXX
Sorted list:XXXXX
Result: