0% found this document useful (0 votes)
1 views19 pages

Python Lab Report BIM5th

The document contains a series of Python programs that perform various tasks, including calculating simple interest, finding the area and perimeter of a rectangle, counting vowels in a string, and determining the smallest of three numbers. It also includes programs for summing natural numbers, finding prime numbers, manipulating lists and sets, and using NumPy for array operations. Each program is accompanied by source code and expected output.

Uploaded by

Aarzan Acharya
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)
1 views19 pages

Python Lab Report BIM5th

The document contains a series of Python programs that perform various tasks, including calculating simple interest, finding the area and perimeter of a rectangle, counting vowels in a string, and determining the smallest of three numbers. It also includes programs for summing natural numbers, finding prime numbers, manipulating lists and sets, and using NumPy for array operations. Each program is accompanied by source code and expected output.

Uploaded by

Aarzan Acharya
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/ 19

1​ Write a program to find simple interest by taking user input.

1.1​ Source Code


# Program to calculate Simple Interest

# Taking user input


principal = float(input("Enter the Principal amount: "))
rate = float(input("Enter the Rate of Interest (% per annum): "))
time = float(input("Enter the Time (in years): "))

# Calculating Simple Interest


simple_interest = (principal * rate * time) / 100

# Displaying the result


print("Simple Interest = Rs.", simple_interest)

1.2​ Output
2​ Write a program to find area and perimeter of rectangle by taking inputs
from user.

2.1​ Source Code


# Program to calculate Area and Perimeter of a Rectangle

# Taking user input


length = float(input("Enter the length of the rectangle: "))
breadth = float(input("Enter the breadth of the rectangle: "))

# Calculating Area and Perimeter


area = length * breadth
perimeter = 2 * (length + breadth)

# Displaying the results


print("Area of Rectangle = ", area)
print("Perimeter of Rectangle = ", perimeter)

2.2​ Output
3​ Write a program to display the number of vowels in a given string.

3.1​ Source Code


string = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0

for char in string:


if char in vowels:
count += 1

print(f"Number of vowels: {count}")

3.2​ Output
4​ Write a program to take three numbers from user and display smallest
among them.

4.1​ Source Code


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
smallest = min(a, b, c)
print("Smallest number is:", smallest)

4.2​ Output
5​ Write a program to take a positive integer from user and find the sum of all
natural numbers from 1 to that number using for loop.

5.1​ Source Code


n = int(input("Enter a positive integer: "))
total = 0
for i in range(1, n+1):
total += i
print("Sum of natural numbers:", total)

5.2​ Output
6​ Write a program to find smallest and largest number among 10 numbers
stored in a list.

6.1​ Source Code


numbers = [23, 1, 45, 78, 3, 56, 12, 99, 34, 67]

smallest = min(numbers)
largest = max(numbers)

print(f"Smallest number: {smallest}")


print(f"Largest number: {largest}")

6.2​ Output
7​ Write a program to display prime numbers up to 100.

7.1​ Source Code


print("Prime numbers up to 100:")
for num in range(2, 101):
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, end=" ")

7.2​ Output
8​ Write a program to illustrate list slicing in python.

8.1​ Source Code


lst = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

print(lst[2:6])
print(lst[:5])
print(lst[5:])

8.2​ Output
9​ Write a program to convert list of integers to a set.

9.1​ Source Code


# Program to convert list of integers to a set

numbers = [1, 2, 2, 3, 4, 4, 5, 5]
unique_numbers = set(numbers)

print("Original list:", numbers)


print("Converted set:", unique_numbers)

9.2​ Output
10​ Extract and display first 5 characters of the string using slicing.

10.1​ Source Code


# Program to extract and display first 5 characters using slicing

text = input("Enter a string: ")


print("First 5 characters:", text[:5])

10.2​ Output
11​ Extract and display characters from index 2 to 15 in interval of 3.

11.1​ Source Code


# Program to extract characters from index 2 to 15 with interval
of 3

text = input("Enter a string: ")


sliced_text = text[2:16:3]
print("Sliced characters (index 2 to 15, step 3):", sliced_text)

11.2​ Output
12​ Display all the characters in reverse order using negative indexing.

12.1​ Source Code


# Program to display all characters in reverse order using
negative indexing

text = input("Enter a string: ")


reversed_text = text[::-1]
print("Reversed string:", reversed_text)

12.2​ Output
13​ Write a program having a function that takes two arguments and returns a
value.

13.1​ Source Code


# Function to add two numbers

def add_numbers(a, b):


return a + b

# Taking input from user


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

result = add_numbers(num1, num2)


print("Sum:", result)

13.2​ Output
14​ Write a program using list comprehension to find square of odd numbers
stored in the list.

14.1​ Source Code


# Program to find square of odd numbers using list comprehension

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


odd_squares = [x**2 for x in numbers if x % 2 != 0]

print("Original list:", numbers)


print("Square of odd numbers:", odd_squares)

14.2​ Output
15​ Write a program to demonstrate how elements in set are added, updated and
deleted.

15.1​ Source Code


# Initialize a set
my_set = {1, 2, 3}
print("Initial set:", my_set)

# Add an element
my_set.add(4)
print("After adding 4:", my_set)

# Update set with multiple elements


my_set.update([5, 6])
print("After updating with [5, 6]:", my_set)

# Delete an element
my_set.remove(2)
print("After removing 2:", my_set)

15.2​ Output
16​ Write a program using recursive function to find factorial of a number.

16.1​ Source Code


def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)

# Taking input from user


num = int(input("Enter a number: "))

if num < 0:
print("Factorial is not defined for negative numbers.")
else:
result = factorial(num)
print(f"Factorial of {num} is: {result}")

16.2​ Output
17​ WAP to create a 5x4 integer array from a range between 100 to 200 such that
the difference between each element in 5 using arange() and reshape()
functions.

17.1​ Source Code


import numpy as np

# Create array using arange and reshape


arr = np.arange(100, 200, 5).reshape(5, 4)

# Display the array


print("5x4 Integer Array:\n", arr)

17.2​ Output
18​ WAP to initialize a 1D NumPy array with values from 1 to 15(inclusive),
define a condition to select all elements greater than 7 and use Boolean
indexing to extract elements that satisfy this condition.

18.1​ Source Code


import numpy as np

# Initialize a 1D array with values from 1 to 15


arr = np.arange(1, 16)

# Define the condition: elements greater than 7


condition = arr > 7

# Apply boolean indexing to extract matching elements


result = arr[condition]

# Display the result


print("Original array:", arr)
print("Elements greater than 7:", result)

18.2​ Output
19​ Write a program to initialize two 2D NumPy arrays: matrix1 of shape (2,3)
with values[[1,2,3],[4,5,6]] and matrix2 of shape (3,2) with values [[7,8],
[9,10], [11,12]] then perform following operations and display the results.
a)​ Calculate the dot product of matrix1 and matrix2.
b)​ Find the transpose of the resulting matrix.

19.1​ Source Code


import numpy as np

# Initialize matrix1 and matrix2


matrix1 = np.array([[1, 2, 3], [4, 5, 6]])
matrix2 = np.array([[7, 8], [9, 10], [11, 12]])

# a. Calculate the dot product


dot_product = np.dot(matrix1, matrix2)

# b. Find the transpose of the resulting matrix


transpose_result = dot_product.T

# Display the results


print("Matrix 1:\n", matrix1)
print("Matrix 2:\n", matrix2)
print("Dot Product:\n", dot_product)
print("Transpose of Dot Product:\n", transpose_result)

19.2​ Output

You might also like