0% found this document useful (0 votes)
9 views5 pages

Week-2 Lab Programs

Uploaded by

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

Week-2 Lab Programs

Uploaded by

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

7.

Experiment: Write a program to define a function with multiple return values

Aim: To define a function with multiple return values

Program:

def arith(x,y):
add=x+y
sub=x-y
mul=x*y
div=x/y
mod=x%y
fd=x//y
exp=x**y
return add,sub,mul,div,mod,fd,exp

a=int(input("Enter First Number:"))


b=int(input("Enter Second Number:"))

addition,minus,prod,divide,modulus,floor,power=arith(a,b)

print("Addition is:",addition)
print("Subtraction is:",minus)
print("Multiplication is:",prod)
print("Division is:",divide)
print("Modulo division is:",modulus)
print("floor Division is:",floor)
print("Exponent is:",power)

Output:

Enter First Number:20


Enter Second Number:10
Addition is: 30
Subtraction is: 10
Multiplication is: 200
Division is: 2.0
Modulo division is: 0
floor Division is: 2
Exponent is: 10240000000000
8. Experiment: Write a program to define a function using default arguments

Aim: To define a function using default arguments

Program:

def student(firstname, lastname ='Mark', standard ='Fifth'):


print(firstname, lastname, 'studies in', standard, 'Standard')

# 1 positional argument
student('John')

# 3 positional arguments
student('John', 'Gates', 'Seventh')

# 2 positional arguments
student('John', 'Gates')
student('John', 'Seventh')

Output:

John Mark studies in Fifth Standard


John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard

9. Experiment: 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:

str = input("Enter any String:")


counter = 0
for i in str:
counter += 1
print("Length of the string is:",counter)

Output:

Enter any String:Aditya


Length of the string is: 6
10. Experiment: Write 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:

Main_String = input("Enter the Main String:")


Sub_String = input("Enter the Sub String:")
index = Main_String.find(Sub_String)

if(index>0):
print("The Substring is Present in Main String at",index,"Position")
else:
print("The Substring is not Present in Main String")

Output:

Enter the Main String:Aditya College of Engineering


Enter the Sub String:College
The Substring is Present in Main String at 7 Position

11. Experiment: Write a python program to perform the given operations on a list:
i. Addition ii. Insertion iii. slicing
Aim: To perform the given operations on a list: i. Addition ii. Insertion iii. slicing

Program:

# Initialize an example list

my_list = [10, 20, 30, 40, 50]

# i. Addition - Add elements to the list (append or extend)


# Adding a single element to the list
my_list.append(60)
print("After Addition (append 60):", my_list)

# Adding multiple elements to the list


my_list.extend([70, 80])
print("After Addition (extend [70, 80]):", my_list)

# ii. Insertion - Insert an element at a specific position in the list


# Inserting 25 at index 2
my_list.insert(2, 25)
print("After Insertion (insert 25 at index 2):", my_list)

# iii. Slicing - Extract a sublist using slicing


# Get a sublist from index 1 to 4 (not including index 4)
sliced_list = my_list[1:4]
print("Sliced List (from index 1 to 4):", sliced_list)

# You can also slice in reverse


sliced_reverse = my_list[-4:-1]
print("Sliced List (from index -4 to -1):", sliced_reverse)

Output:

After Addition (append 60): [10, 20, 30, 40, 50, 60]
After Addition (extend [70, 80]): [10, 20, 30, 40, 50, 60, 70, 80]
After Insertion (insert 25 at index 2): [10, 20, 25, 30, 40, 50, 60, 70, 80]
Sliced List (from index 1 to 4): [20, 25, 30]
Sliced List (from index -4 to -1): [60, 70, 80]

12. Experiment: Write a python 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:

# Initialize a sample list


my_list = [10, 20, 30, 40, 50, 60, 70]

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


list_length = len(my_list)
print("Length of the list:", list_length)

# 2. `max()` - Returns the largest item in the list


max_value = max(my_list)
print("Maximum value in the list:", max_value)

# 3. `min()` - Returns the smallest item in the list


min_value = min(my_list)
print("Minimum value in the list:", min_value)

# 4. `sum()` - Returns the sum of all elements in the list


total_sum = sum(my_list)
print("Sum of all elements in the list:", total_sum)

# 5. `reverse()` - Reverses the elements of the list in place


my_list.reverse() # This changes the original list
print("List after reversing:", my_list)

Output:

Length of the list: 7


Maximum value in the list: 70
Minimum value in the list: 10
Sum of all elements in the list: 280
List after reversing: [70, 60, 50, 40, 30, 20, 10]

You might also like