PYTHON PROGRAMMING 1
WEEK - II
Aim:
1. Write a program to define a function with multiple return values.
2. Write a program to define a function using default arguments.
3. Write a program to find the length of the string without using any library
functions.
4. Write a program to check if the substring is present in a given string or
not.
5. Write a program to perform the given operations on a list: Write a
program to define a function with multiple return values.
i. Addition
ii. Insertion
iii. slicing
6. Write a program to perform any 5 built-in functions by taking any list.
Description:
Python Functions is a block of statements that does a specific task. The idea
is to put some commonly or repeatedly done task together and make a
function so that instead of writing the same code again and again for different
inputs, we can do the function calls to reuse code contained in it over and
over again.
Benefits of Using Functions
Code Reuse
Reduced code length
Increased redability of code
II-I CSE @2024 BATCH SREC NANDYAL
PYTHON PROGRAMMING 2
[Link] a program to define a function with multiple return values.
Aim: To define a function with multiple return values.
Program:
def calculate(a, b):
addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b if b != 0 else "undefined"
return addition, subtraction, multiplication, division
add, sub, mul, div = calculate(30, 15)
print("Addition:", add)
print("Subtraction:", sub)
print("Multiplication:", mul)
print("Division:", div)
II-I CSE @2024 BATCH SREC NANDYAL
PYTHON PROGRAMMING 3
Excepted Output:
Addition: 45
Subtraction: 15
Multiplication: 450
Division: 2.0
Output:
Addition: 45
Subtraction: 15
Multiplication: 450
Division: 2.0
II-I CSE @2024 BATCH SREC NANDYAL
PYTHON PROGRAMMING 4
[Link] a program to define a function using default arguments.
Aim: To define a function using default arguments.
Program:
def greet(name="Guest", message="Welcome to Python Programming!"):
print(f"Hello {name}, {message}")
greet()
greet("Gayathri")
greet("Gayathri", "Hope you are doing well!")
II-I CSE @2024 BATCH SREC NANDYAL
PYTHON PROGRAMMING 5
Excepted Output:
Hello Guest, Welcome to Python Programming!
Hello Gayathri, Welcome to Python Programming!
Hello Gayathri, Hope you are doing well!
Output:
Hello Guest, Welcome to Python Programming!
Hello Gayathri, Welcome to Python Programming!
Hello Gayathri, Hope you are doing well!
II-I CSE @2024 BATCH SREC NANDYAL
PYTHON PROGRAMMING 6
3. Write a program to find the length of the string without using any
library functions.
Aim: To find the length of the string without using any library functions.
Program:
def find_length(s):
count = 0
for char in s:
count += 1
return count
text = "Gayathri"
length = find_length(text)
print("The length of the string is:", length)
II-I CSE @2024 BATCH SREC NANDYAL
PYTHON PROGRAMMING 7
Excepted Output:
The length of the string is: 8
Output:
The length of the string is: 8
II-I CSE @2024 BATCH SREC NANDYAL
PYTHON PROGRAMMING 8
[Link] a program to check if the substring is present in a given string
or not.
Aim: To check if the substring is present in a given string or not.
Program:
def check_substring(main_str, sub_str):
m=0
n=0
while m < len(main_str) and n < len(sub_str):
if main_str[m] == sub_str[n]:
m += 1
n += 1
if n == len(sub_str):
return True
else:
m=m-n+1
n=0
return False
main_string = "Hello Gayathri, welcome to Python programming"
substring = "Gayathri"
if check_substring(main_string, substring):
print("Substring is present in the given string.")
else:
print("Substring is NOT present in the given string.")
II-I CSE @2024 BATCH SREC NANDYAL
PYTHON PROGRAMMING 9
Excepted Output:
Substring is present in the given string.
Output:
Substring is present in the given string.
II-I CSE @2024 BATCH SREC NANDYAL
PYTHON PROGRAMMING 10
[Link] a program to perform the given operations on a list: Write a
program to define a function with multiple return values.
i. Addition
ii. Insertion
iii. slicing
Aim: To perform the given operations on a list: Write a program to define
a function with multiple return values.
i. Addition
ii. Insertion
iii. slicing
Program:
def list_operations(my_list, element_to_add, element_to_insert, position, start,
end):
my_list.append(element_to_add)
my_list.insert(position, element_to_insert)
sliced_list = my_list[start:end]
return my_list, sliced_list
numbers = [10, 20, 30, 40, 50]
updated_list, sliced = list_operations(numbers, 60, 25, 2, 1, 4)
print("Updated List after Addition & Insertion:", updated_list)
print("Sliced List:", sliced)
II-I CSE @2024 BATCH SREC NANDYAL
PYTHON PROGRAMMING 11
Excepted Output:
Updated List after Addition & Insertion: [10, 20, 25, 30, 40, 50, 60]
Sliced List: [20, 25, 30]
Output:
Updated List after Addition & Insertion: [10, 20, 25, 30, 40, 50, 60]
Sliced List: [20, 25, 30]
II-I CSE @2024 BATCH SREC NANDYAL
PYTHON PROGRAMMING 12
6. Write a program to perform any 5 built-in functions by taking any list.
Aim: To perform any 5 built-in functions by taking any list.
Program:
numbers = [10, 20, 5, 40, 30]
print("Original List:", numbers)
print("Length of list:", len(numbers))
print("Maximum element:", max(numbers))
print("Minimum element:", min(numbers))
print("Sum of elements:", sum(numbers))
print("Sorted List:", sorted(numbers))
II-I CSE @2024 BATCH SREC NANDYAL
PYTHON PROGRAMMING 13
Excepted Output:
Original List: [10, 20, 5, 40, 30]
Length of list: 5
Maximum element: 40
Minimum element: 5
Sum of elements: 105
Sorted List: [5, 10, 20, 30, 40]
Output:
Original List: [10, 20, 5, 40, 30]
Length of list: 5
Maximum element: 40
Minimum element: 5
Sum of elements: 105
Sorted List: [5, 10, 20, 30, 40]
II-I CSE @2024 BATCH SREC NANDYAL