We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
INDEX
Name:
Class:
Roll No.:
Subject: Python Programming Labs. Practical Title Page | Teacher's
No. No. Sign
1 Program to check whether a
number is prime or not
2 Program to find (m“n) (power
of anumber)
3. | Program using a
parameterized function to
check whether a number is
even or odd
4 | Program to find the sum of
the series: 1-2+3-4+...+
n
5 | Menu-driven program for
string operations using built-
in functions
5(a) | Find the frequency of a
character in a string
5(b) | Replace a character by
another character in a string
5(c) | Remove the first occurrence
of a character from a string
5(d) | Remove all occurrences of a
character from a string
6 | Program to find indices of all
occurrences of a substring in
astring
7(a) | Create a NumPy array filled
with 1's7(b)
Find maximum and minimum
values of a NumPy array
7(c)
Find the dot product of two
NumPy arrays
7(d)
Reshape a 1-D array into a 2-
D array
Program to count the
frequency of each letter ina
sentence using a dictionary
9(a)
Print tuple contents in two
separate lines
9(b)
Create another tuple
containing only even values
9c)
Concatenate two tuples
9(d)
Find maximum and minimum
values in a tuple
10
Program to copy alternate
(odd-numbered) lines from
one file to another
11
Program to handle
ZeroDivisionError
exception
12
Program to detect duplicate
numbers in a list using
exceptions
13
Program to plot sine, cosine,
polynomial, and exponential
curves
14
Program to plot monthly
profits of a company using a
line graphExperiment 1: Check Whether
a Number is Prime
«> Python QO ( Run
n = int(input("Enter a number: "))
if n <= 1:
print("Not Prime")
else:
flag = True
for i in range(2, int(n ** 0.5) + 1):
if n%i == 0:
flag = False
break
if flag:
print ("Prime Number")
else:
print("Not Prime")Experiment 2: Find m raised to
the power n
«» Python OQ (O Run
m = int(input("Enter base: "))
n = int(input("Enter power: "))
result = m ** n
printc"Answer =", result)Experiment 3: Parameterized
Function to Check Even or
Odd
«> Python O (> Run
def isEven(num):
if num % 2 == 0:
return True
else:
return False
n = int(input("Enter number: "))
if isEven(n):
print("Even")
else:
print("Odd")Experiment 4: Sum of Series
1-2+3-4+...
«> Python O (> Run
n = int(input("Enter number of terms: "))
sum = 0
for i in range(1, n+ 1):
if i% 2 == 0:
sum -= i
else:
sum += i
print("Sum =", sum)Experiment 5: Menu Driven
String Operations
<7 Python
while True:
@ (> Run
print("\nMENU")
print("1.
print("2.
print("3.
print("4.
print("5.
choice =
Frequency of Character")
Replace Character")
Remove First Occurrence")
Remove All Occurrences")
Exit")
int(input("Enter choice: "))
s = input("Enter String: ")
if choice ==
ch = input("Character: ")
print("Frequency =", [Link](ch))elif choice ==
old = input("Old Character; ")
new = input("New Character: ")
print("Result =", [Link](old, new))
elif choice ==
ch = input("Character to Remove: ")
pos = [Link](ch)
if pos != -1:
s = s[:pos] + s[pos + 1:]
print("Result =", s)
elif choice ==
ch = input("Character to Remove: ")
print¢"Result =", [Link](ch, ""))
else:
print("Invalid Choice")Experiment 6: Find All
Occurrences of Substring
«Python O ([ Run
s = input("Enter first string: ")
sub = input("Enter second string: ")
positions = []
start = 0
while True:
index = [Link](sub, start)
if index == -1:
break
positions .append( index)
start = index + 1if positions:
print(positions)
else:
print(-1)Experiment 7: NumPy Menu
Program
«> Python Q [> Run
import numpy as np
while True:
print("\nMENU")
Pieine Gal Aliedy, Of mOness)
print("2. Maximum and Minimum")
print("3. Dot Product")
print("4. Reshape Array")
Dig Qor sche)
ch = int(input("Choice: "))
if ch ==
r = int(input("Rows: "))
c = int(input("Columns: "))
print([Link]((r, c)))elif ch 2:
arr = [Link](list(map(int, input("Enter array elements: ").split())))
print("Maximum =", [Link](arr))
print("Minimum =", [Link](arr))
elif ch == 3:
a = [Link](list(map(int, input("Enter first array: ").split())))
b = [Link](list(map(int, input("Enter second array: ").split())))
print("Dot Product =", [Link](a, b))
elif ch == 4:
arr = [Link](list(map(int, input("Enter array elements: “).split())))
r = int(input("Rows: "))
c = int(input("Columns: "))
print([Link](r, c))
elif ch ==
break
else:
print("Invalid Choice")Experiment 8: Letter
Frequency Using Dictionary
«> Python QO (> Run
sentence = input("Enter a sentence: ")
count = {}
for ch in sentence:
if [Link]():
ch = [Link]()
count[ch] = [Link](ch, 0) + 1
print(count)
Experiment 9: Tuple
Operations«» Python @ [> Run
t1 = (1, 2, 5, 7, 9, 2, 4, 6, 8, 10)
print("First Half =", t1[:5])
print("Second Half =", t1[5:])
t2 = ()
for i in t1:
if i%2 ==
t2 += (i,)
print("Even Tuple =", t2)
t3 = (11, 13, 15)
print("Concatenated =", t1 + t3)
print("Maximum =", max(t1))
print("Minimum =", min(t1))Experiment 10: Copy Alternate
Lines
« Python O (> Run
"
f1 = open("[Link]", "r")
#2 = open("[Link]", "w")
lines = [Link]()
for i in range(0, len(lines), 2):
[Link](lines[i])
[Link]()
[Link]()
print("File Copied Successfully")Experiment 11:
ZeroDivisionError Handling
«> Python O (> Run
try:
ow
i]
int(input("Enter first number: "))
b = int(input("Enter second number: ")
print("Division =", a / b)
except ZeroDivisionError:
print("Cannot Divide by Zero")Experiment 12: Exception for
Duplicate Numbers
«> Python O O Run
# Experiment 12: Exception for Duplicate Numbers
st = list(map(int, input("Enter numbers separated by space: ").split()))
try:
if len(Ist) != len(set(1st))
raise Exception("Duplicate Numbers Found")
print("No Duplicates")
except Exception as e:
print(e)Experiment 13: Plot Sine,
Cosine, Polynomial and
Exponential Curves
« Python QO ( Run
import numpy as np
import [Link] as plt
x = [Link](-2, 2, 100)
[Link](x, [Link](x), label="Sine")
[Link](x, [Link](x), label="Cosine")
[Link](x, x**2, label="Polynomial")
[Link](x, [Link](x), label="Exponential" )
[Link]¢)
[Link]¢)
[Link]()Experiment 14: Company
Profit Line Plot
«» Python O ( Run
import [Link] as plt
months = [1,2,3,4,5,6,7,8,9,10,11,12]
profit = []
print("Enter profit for 12 months")
for i in range(12):
p = int(input())
[Link](p)
[Link](months, profit, marker="0")plt.
plt.
pltx
Olt,
xlabel("Month Number")
ylabel("Total Profit")
title("Company Profit Report")
grid()
. show( )