0% found this document useful (0 votes)
2 views4 pages

Python Functions and List Operations Guide

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Python Functions and List Operations Guide

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

UNIT-2

1. Write a python program to define a function with multiple return values


# Function to perform basic arithmetic operations and return multiple
results
def calculate_operations(a, b):
sum_result = a + b
diff_result = a - b
prod_result = a * b
if b != 0:
div_result = a / b
else:
div_result = None # to avoid division by zero

# Returning multiple values


return sum_result, diff_result, prod_result, div_result

# Main program
x = 20
y=5

# Call the function and unpack the returned values


sum_val, diff_val, prod_val, div_val = calculate_operations(x, y)

# Display the results


print("Sum:", sum_val)
print("Difference:", diff_val)
print("Product:", prod_val)
print("Division:", div_val)

OUTPUT:
Sum: 25
Difference: 15
Product: 100
Division: 4.0
2. Write a python program to define a function using default arguments
# Function with default arguments
def greet(name="Guest", message="Welcome to Python
programming!"):
print("Hello", name + "!")
print(message)

# Calling the function with no arguments


greet()

# Calling the function with one argument


greet("Renuka")

# Calling the function with both arguments


greet("Renuka", "Hope you're enjoying learning Python!")
OUTPUT:
Hello Guest!
Welcome to Python programming!

Hello Renuka!
Welcome to Python programming!

Hello Renuka!
Hope you're enjoying learning Python!
3. write a python program to find the length of the string without using any
library functions
# Function to find the length of a string manually
def find_length(s):
count = 0
for character in s:
count += 1
return count

# Main program
input_string = input("Enter a string: ")
length = find_length(input_string)

print("Length of the string:", length)


OUTPUT:
Enter a string: Hello World
Length of the string: 11
4. write a python program to check if the substring is present in a given
string or not
# Using 'in' operator
main_string = input("Enter the main string: ")
sub_string = input("Enter the substring to search: ")

if sub_string in main_string:
print("Substring is present in the main string.")
else:
print("Substring is not present in the main string.")
OUTPUT:
Enter the main string: Python programming is fun
Enter the substring to search: program
Substring is present in the main string.
5. write a python program to perform the given operations on a list:
i)addition ii)insertion iii)slicing
# Initial list
my_list = [10, 20, 30, 40, 50]

# i) Addition (append an element at the end)


my_list.append(60)
print("After addition (append 60):", my_list)

# ii) Insertion (insert at a specific position)


my_list.insert(2, 25) # Insert 25 at index 2
print("After insertion (insert 25 at index 2):", my_list)

# iii) Slicing (extract a portion of the list)


sliced_list = my_list[1:5] # Get elements from index 1 to 4
print("Sliced list (index 1 to 4):", sliced_list)
OUTPUT:
After addition (append 60): [10, 20, 30, 40, 50, 60]
After insertion (insert 25 at index 2): [10, 20, 25, 30, 40, 50, 60]
Sliced list (index 1 to 4): [20, 25, 30, 40]
6. Write a python program to perform any 5 built-in functions by taking any
list
# Sample list
numbers = [10, 5, 20, 15, 5]

print("Original List:", numbers)

# 1. len() - Returns the number of elements in the list


print("Length of the list:", len(numbers))

# 2. max() - Returns the maximum element


print("Maximum value in the list:", max(numbers))

# 3. min() - Returns the minimum element


print("Minimum value in the list:", min(numbers))

# 4. [Link](x) - Counts how many times x appears in the list


print("Count of 5 in the list:", [Link](5))

# 5. [Link]() - Sorts the list in ascending order


[Link]()
print("Sorted list:", numbers)
OUTPUT:
Original List: [10, 5, 20, 15, 5]
Length of the list: 5
Maximum value in the list: 20
Minimum value in the list: 5
Count of 5 in the list: 2
Sorted list: [5, 5, 10, 15, 20]

You might also like