Module 1: Python Basics – 20 Worked-Out Programs
(Program + Output + Explanation)
1. Hello World Program
Program
print("Hello World")
Output
Hello World
Explanation
The print() function displays the text "Hello World" on the screen.
2. Addition of Two Numbers
Program
a = 10
b = 20
sum = a + b
print("Sum =", sum)
Output
Sum = 30
Explanation
The values 10 and 20 are stored in variables a and b. The + operator adds
them and stores the result in sum.
3. Subtraction of Two Numbers
Program
a = 50
b = 20
result = a - b
print("Difference =", result)
Output
Difference = 30
Explanation
The - operator subtracts the value of b from a.
4. Multiplication of Two Numbers
Program
a = 10
b=5
result = a * b
print("Product =", result)
Output
Product = 50
Explanation
The * operator multiplies two numbers.
5. Division of Two Numbers
Program
a = 20
b=4
result = a / b
print("Division =", result)
Output
Division = 5.0
Explanation
The / operator performs floating-point division.
6. Find Area of Rectangle
Program
length = 10
width = 5
area = length * width
print("Area =", area)
Output
Area = 50
Explanation
Area of a rectangle is calculated using:
Area = Length × Width
7. Swap Two Numbers
Program
a = 10
b = 20
a, b = b, a
print("a =", a)
print("b =", b)
Output
a = 20
b = 10
Explanation
Python allows swapping variables without using a temporary variable.
8. Check Even or Odd Number
Program
num = 8
if num % 2 == 0:
print("Even")
else:
print("Odd")
Output
Even
Explanation
If the remainder after division by 2 is zero, the number is even.
9. Check Positive, Negative or Zero
Program
num = -5
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
Output
Negative
Explanation
The program uses if-elif-else to determine the sign of the number.
10. Find Largest of Two Numbers
Program
a = 15
b = 25
if a > b:
print(a, "is larger")
else:
print(b, "is larger")
Output
25 is larger
Explanation
The program compares two numbers using the > operator.
11. Find Largest of Three Numbers
Program
a = 10
b = 50
c = 30
largest = max(a, b, c)
print("Largest =", largest)
Output
Largest = 50
Explanation
The max() function returns the largest value among the arguments.
12. Check Voting Eligibility
Program
age = 20
if age >= 18:
print("Eligible for Voting")
else:
print("Not Eligible")
Output
Eligible for Voting
Explanation
A person can vote if age is 18 years or above.
13. Multiplication Table
Program
num = 5
for i in range(1, 11):
print(num, "x", i, "=", num * i)
Output
5 x 1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Explanation
The for loop generates multiplication values from 1 to 10.
14. Sum of First N Natural Numbers
Program
n = 10
sum = 0
for i in range(1, n + 1):
sum += i
print("Sum =", sum)
Output
Sum = 55
Explanation
The loop adds numbers from 1 to 10.
15. Factorial of a Number
Program
n=5
fact = 1
for i in range(1, n + 1):
fact *= i
print("Factorial =", fact)
Output
Factorial = 120
Explanation
Factorial of 5:
5 × 4 × 3 × 2 × 1 = 120
16. Fibonacci Series
Program
a=0
b=1
for i in range(10):
print(a, end=" ")
a, b = b, a + b
Output
0 1 1 2 3 5 8 13 21 34
Explanation
Each term is obtained by adding the previous two terms.
17. Reverse a Number
Program
num = 1234
reverse = 0
while num > 0:
digit = num % 10
reverse = reverse * 10 + digit
num = num // 10
print(reverse)
Output
4321
Explanation
Digits are extracted one by one from the right and added in reverse order.
18. Count Digits in a Number
Program
num = 12345
count = 0
while num > 0:
count += 1
num = num // 10
print("Digits =", count)
Output
Digits = 5
Explanation
Each iteration removes one digit until the number becomes zero.
19. Check Prime Number
Program
num = 17
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print("Prime Number")
else:
print("Not Prime")
Output
Prime Number
Explanation
A prime number has only two factors: 1 and itself.
20. Function to Add Two Numbers
Program
def add(a, b):
return a + b
result = add(10, 20)
print("Sum =", result)
Output
Sum = 30
Explanation
The function add() receives two values, adds them, and returns the result.
Important Viva Questions from Module 1
1. What is Python?
2. What is a variable?
3. Difference between int and float.
4. What are operators?
5. Difference between = and ==.
6. What is an if statement?
7. Difference between for loop and while loop.
8. What is a function?
9. What is the purpose of return?
10. What is a prime number?
11. What is factorial?
12. What is the Fibonacci series?
13. What is scope?
14. What are local and global variables?
15. What is dynamic typing in Python?
Module 2: Collection Data Types – 20 Worked-Out Programs
(Program + Output + Explanation)
Topics Covered:
Lists
Tuples
Sets
List to Tuple Conversion
List to Set Conversion
1. Create and Display a List
Program
fruits = ["Apple", "Banana", "Mango"]
print(fruits)
Output
['Apple', 'Banana', 'Mango']
Explanation
A list is created using square brackets []. It stores multiple values in a
single variable.
2. Access List Elements
Program
numbers = [10, 20, 30, 40]
print("First Element =", numbers[0])
print("Last Element =", numbers[-1])
Output
First Element = 10
Last Element = 40
Explanation
List elements are accessed using indexing. Positive indexing starts from 0
and negative indexing starts from -1.
3. List Slicing
Program
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4])
Output
[20, 30, 40]
Explanation
Slicing extracts a portion of a list from index 1 to index 3.
4. Add Element Using append()
Program
numbers = [10, 20, 30]
[Link](40)
print(numbers)
Output
[10, 20, 30, 40]
Explanation
append() adds an element at the end of the list.
5. Insert Element at Specific Position
Program
numbers = [10, 20, 30]
[Link](1, 15)
print(numbers)
Output
[10, 15, 20, 30]
Explanation
insert(index, value) adds an element at a specified position.
6. Remove Element from List
Program
numbers = [10, 20, 30, 40]
[Link](20)
print(numbers)
Output
[10, 30, 40]
Explanation
remove() deletes the specified value from the list.
7. Remove Element Using pop()
Program
numbers = [10, 20, 30, 40]
[Link]()
print(numbers)
Output
[10, 20, 30]
Explanation
pop() removes the last element by default.
8. Sort a List
Program
numbers = [50, 10, 30, 20]
[Link]()
print(numbers)
Output
[10, 20, 30, 50]
Explanation
sort() arranges elements in ascending order.
9. Reverse a List
Program
numbers = [10, 20, 30, 40]
[Link]()
print(numbers)
Output
[40, 30, 20, 10]
Explanation
reverse() changes the order of elements.
10. Find Maximum and Minimum Element
Program
numbers = [10, 50, 20, 80, 30]
print("Maximum =", max(numbers))
print("Minimum =", min(numbers))
Output
Maximum = 80
Minimum = 10
Explanation
max() returns the largest value and min() returns the smallest value.
11. Find Sum of List Elements
Program
numbers = [10, 20, 30, 40]
print("Sum =", sum(numbers))
Output
Sum = 100
Explanation
sum() adds all elements in the list.
12. Count Occurrences of an Element
Program
numbers = [1, 2, 1, 3, 1, 4]
print([Link](1))
Output
Explanation
count() returns the number of times an element appears.
13. Create and Access a Tuple
Program
student = ("Raj", 20, "BCA")
print(student)
print(student[0])
Output
('Raj', 20, 'BCA')
Raj
Explanation
A tuple is created using parentheses () and accessed using indexing.
14. Tuple Slicing
Program
t = (10, 20, 30, 40, 50)
print(t[1:4])
Output
(20, 30, 40)
Explanation
Tuple slicing works like list slicing.
15. Tuple Unpacking
Program
student = ("Raj", 20, "BCA")
name, age, course = student
print(name)
print(age)
print(course)
Output
Raj
20
BCA
Explanation
Tuple elements can be assigned directly to variables.
16. Tuple Concatenation
Program
t1 = (1, 2, 3)
t2 = (4, 5, 6)
print(t1 + t2)
Output
(1, 2, 3, 4, 5, 6)
Explanation
The + operator joins two tuples.
17. Create a Set
Program
s = {10, 20, 30, 40}
print(s)
Output
{40, 10, 20, 30}
Explanation
Sets are unordered collections of unique elements. Order may vary.
18. Union of Two Sets
Program
A = {1, 2, 3}
B = {3, 4, 5}
print(A | B)
Output
{1, 2, 3, 4, 5}
Explanation
Union combines all unique elements from both sets.
19. Intersection of Two Sets
Program
A = {1, 2, 3}
B = {2, 3, 4}
print(A & B)
Output
{2, 3}
Explanation
Intersection returns only common elements.
20. List to Tuple and List to Set Conversion
Program
numbers = [10, 20, 20, 30, 30, 40]
t = tuple(numbers)
s = set(numbers)
print("Tuple =", t)
print("Set =", s)
Output
Tuple = (10, 20, 20, 30, 30, 40)
Set = {40, 10, 20, 30}
Explanation
tuple() converts a list into a tuple.
set() converts a list into a set and removes duplicates.
Bonus Programs (Frequently Asked in Practical Exams)
Program 21: Difference of Two Sets
A = {1, 2, 3, 4}
B = {3, 4, 5}
print(A - B)
Output
{1, 2}
Program 22: Symmetric Difference
A = {1, 2, 3}
B = {3, 4, 5}
print(A ^ B)
Output
{1, 2, 4, 5}
Program 23: Remove Duplicates from a List
numbers = [1, 2, 2, 3, 3, 4]
unique = list(set(numbers))
print(unique)
Output
[1, 2, 3, 4]
Program 24: Find Length of List
numbers = [10, 20, 30, 40]
print(len(numbers))
Output
Program 25: Search an Element in a List
numbers = [10, 20, 30, 40]
if 30 in numbers:
print("Found")
else:
print("Not Found")
Output
Found
Important Viva Questions from Module 2
1. What is a list?
2. Difference between list and tuple.
3. Why are tuples immutable?
4. What is indexing?
5. What is slicing?
6. Difference between append() and insert().
7. Difference between remove() and pop().
8. What is tuple unpacking?
9. What is a set?
10. Why are sets unordered?
11. What is union?
12. What is intersection?
13. What is difference operation?
14. What is symmetric difference?
15. Why convert a list to a set?
16. How are duplicates removed from a list?
17. What is list concatenation?
18. What is tuple concatenation?
19. Can sets contain duplicate values?
20. What is the purpose of count()?
Module 3: Strings – 20 Worked-Out Programs
(Program + Output + Explanation)
Topics Covered:
Creating Strings
String Traversal
Multiline Strings
Concatenation
Escape Sequences
String Slicing
String Functions
1. Create and Display a String
Program
name = "Python Programming"
print(name)
Output
Python Programming
Explanation
A string is a sequence of characters enclosed in quotes.
2. Access Characters Using Indexing
Program
text = "Python"
print("First Character =", text[0])
print("Last Character =", text[-1])
Output
First Character = P
Last Character = n
Explanation
Positive indexing starts from 0, while negative indexing starts from -1.
3. String Slicing
Program
text = "Python"
print(text[1:5])
Output
ytho
Explanation
Slicing extracts characters from index 1 to index 4.
4. Reverse a String Using Slicing
Program
text = "Python"
print(text[::-1])
Output
nohtyP
Explanation
[::-1] reverses the string.
5. Traverse a String Using for Loop
Program
text = "Python"
for ch in text:
print(ch)
Output
P
y
t
h
o
n
Explanation
The loop accesses one character at a time.
6. Count Characters in a String
Program
text = "Python"
print("Length =", len(text))
Output
Length = 6
Explanation
len() returns the total number of characters.
7. Convert String to Uppercase
Program
text = "python"
print([Link]())
Output
PYTHON
Explanation
upper() converts all letters to uppercase.
8. Convert String to Lowercase
Program
text = "PYTHON"
print([Link]())
Output
python
Explanation
lower() converts all letters to lowercase.
9. Capitalize First Character
Program
text = "python programming"
print([Link]())
Output
Python programming
Explanation
capitalize() converts the first character into uppercase.
10. Convert String to Title Case
Program
text = "python programming language"
print([Link]())
Output
Python Programming Language
Explanation
title() capitalizes the first letter of every word.
11. Remove Leading and Trailing Spaces
Program
text = " Python Programming "
print([Link]())
Output
Python Programming
Explanation
strip() removes spaces from both ends.
12. Replace Characters in a String
Program
text = "Hello World"
print([Link]("World", "Python"))
Output
Hello Python
Explanation
replace() substitutes one substring with another.
13. Split a String into Words
Program
text = "Python Java C++"
print([Link]())
Output
['Python', 'Java', 'C++']
Explanation
split() converts a string into a list of words.
14. Join List Elements into a String
Program
languages = ["Python", "Java", "C++"]
print("-".join(languages))
Output
Python-Java-C++
Explanation
join() combines list elements into a single string.
15. Find a Substring
Program
text = "Python Programming"
print([Link]("Program"))
Output
Explanation
find() returns the starting index of the substring.
16. Count Occurrences of a Character
Program
text = "banana"
print([Link]("a"))
Output
Explanation
count() returns the number of occurrences.
17. Check Starting and Ending Characters
Program
text = "Python"
print([Link]("Py"))
print([Link]("on"))
Output
True
True
Explanation
These functions verify whether the string starts or ends with specific
characters.
18. Demonstrate Escape Sequences
Program
print("Hello\nWorld")
print("Python\tProgramming")
print("He said \"Hello\"")
Output
Hello
World
Python Programming
He said "Hello"
Explanation
\n → New line
\t → Tab space
\" → Double quote
19. Multiline String
Program
text = """Python
Programming
Language"""
print(text)
Output
Python
Programming
Language
Explanation
Triple quotes allow strings to span multiple lines.
20. Check Whether a String is a Palindrome
Program
text = "madam"
if text == text[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
Output
Palindrome
Explanation
A palindrome reads the same forward and backward.
Bonus Frequently Asked Practical Programs
21. Count Vowels in a String
Program
text = "Python Programming"
count = 0
for ch in [Link]():
if ch in "aeiou":
count += 1
print("Vowels =", count)
Output
Vowels = 4
22. Count Words in a Sentence
Program
sentence = "Python is easy to learn"
words = [Link]()
print("Words =", len(words))
Output
Words = 5
23. Check Whether String Contains Digits
Program
text = "Python123"
print([Link]())
Output
True
24. Check Alphabetic String
Program
text = "Python"
print([Link]())
Output
True
25. Reverse Each Word in a Sentence
Program
sentence = "Python Programming"
words = [Link]()
for word in words:
print(word[::-1], end=" ")
Output
nohtyP gnimmargorP
Important Viva Questions from Module 3
1. What is a string?
2. Why are strings immutable?
3. Difference between indexing and slicing.
4. What is negative indexing?
5. What is string traversal?
6. What is a multiline string?
7. What are escape sequences?
8. Difference between upper() and lower().
9. Difference between split() and join().
10. What is the use of strip()?
11. Difference between find() and count().
12. What does replace() do?
13. What is a palindrome string?
14. How do you reverse a string?
15. What is string concatenation?
16. What is string repetition?
17. What is startswith()?
18. What is endswith()?
19. What is the purpose of len()?
20. Why are strings widely used in programming?
Module 4: Modules, Packages and Libraries – 20 Worked-Out
Programs
(Program + Output + Explanation)
Topics Covered:
Modules
Packages
Import Statement
NumPy
Pandas
Matplotlib
PART A: MODULES & IMPORT STATEMENTS
1. Import Math Module
Program
import math
print([Link](25))
Output
5.0
Explanation
The math module contains mathematical functions. sqrt() returns the
square root of a number.
2. Find Value of π
Program
import math
print([Link])
Output
3.141592653589793
Explanation
[Link] stores the value of π.
3. Find Power Using math Module
Program
import math
print([Link](2, 3))
Output
8.0
Explanation
pow(x,y) calculates x raised to the power y.
4. Import Specific Function
Program
from math import sqrt
print(sqrt(81))
Output
9.0
Explanation
Only the sqrt() function is imported from the math module.
5. Use Alias with Import
Program
import math as m
print([Link](5))
Output
120
Explanation
as creates an alias (short name) for the module.
6. Generate Random Number
Program
import random
print([Link](1,10))
Sample Output
Explanation
randint(a,b) generates a random integer between a and b.
Note: Output may vary every time.
7. Display Current Date and Time
Program
import datetime
print([Link]())
Sample Output
2026-06-13 14:30:25.456789
Explanation
now() returns the current date and time.
PART B: NUMPY PROGRAMS
8. Create NumPy Array
Program
import numpy as np
arr = [Link]([10,20,30,40])
print(arr)
Output
[10 20 30 40]
Explanation
[Link]() creates a NumPy array.
9. Create 2D NumPy Array
Program
import numpy as np
arr = [Link]([
[1,2],
[3,4]
])
print(arr)
Output
[[1 2]
[3 4]]
Explanation
A two-dimensional array contains rows and columns.
10. Find Shape of Array
Program
import numpy as np
arr = [Link]([
[1,2],
[3,4]
])
print([Link])
Output
(2, 2)
Explanation
shape returns the number of rows and columns.
11. Find Size of Array
Program
import numpy as np
arr = [Link]([
[1,2],
[3,4]
])
print([Link])
Output
Explanation
size returns the total number of elements.
12. Find Sum and Mean
Program
import numpy as np
arr = [Link]([10,20,30,40])
print("Sum =", [Link]())
print("Mean =", [Link]())
Output
Sum = 100
Mean = 25.0
Explanation
sum() adds all elements.
mean() calculates average.
13. Create Array of Zeros
Program
import numpy as np
print([Link](5))
Output
[0. 0. 0. 0. 0.]
Explanation
Creates an array containing zeros.
14. Create Array of Ones
Program
import numpy as np
print([Link](5))
Output
[1. 1. 1. 1. 1.]
Explanation
Creates an array containing ones.
PART C: PANDAS PROGRAMS
15. Create Pandas Series
Program
import pandas as pd
s = [Link]([10,20,30,40])
print(s)
Output
0 10
1 20
2 30
3 40
dtype: int64
Explanation
A Series is a one-dimensional labeled array.
16. Create DataFrame
Program
import pandas as pd
data = {
"Name":["Raj","Amit"],
"Age":[20,21]
}
df = [Link](data)
print(df)
Output
Name Age
0 Raj 20
1 Amit 21
Explanation
A DataFrame is a table consisting of rows and columns.
17. Display First Records Using head()
Program
import pandas as pd
data = {
"Marks":[80,75,90,85,88]
}
df = [Link](data)
print([Link]())
Output
Marks
0 80
1 75
2 90
3 85
4 88
Explanation
head() displays the first rows of a DataFrame.
18. Display Dataset Information
Program
import pandas as pd
data = {
"Name":["Raj","Amit"],
"Age":[20,21]
}
df = [Link](data)
[Link]()
Output
<class '[Link]'>
RangeIndex: 2 entries, 0 to 1
Data columns (total 2 columns):
Name 2 non-null object
Age 2 non-null int64
Explanation
info() provides details about rows, columns, and data types.
PART D: MATPLOTLIB PROGRAMS
19. Draw a Line Graph
Program
import [Link] as plt
x = [1,2,3,4]
y = [2,4,6,8]
[Link](x,y)
[Link]("Line Graph")
[Link]("X Axis")
[Link]("Y Axis")
[Link]()
Output
📈 A line graph is displayed showing points:
(1,2), (2,4), (3,6), (4,8)
Explanation
plot() creates a line graph. It is useful for showing trends.
20. Draw a Bar Chart
Program
import [Link] as plt
students = ["A","B","C"]
marks = [80,90,75]
[Link](students, marks)
[Link]("Student Marks")
[Link]()
Output
📊 A bar chart is displayed with:
A → 80
B → 90
C → 75
Explanation
bar() creates a bar chart used for comparison.
BONUS PRACTICAL PROGRAMS (Very Frequently Asked)
21. Pie Chart
Program
import [Link] as plt
sizes = [40,30,20,10]
[Link](sizes)
[Link]()
Output
🥧 Pie chart displayed.
22. Scatter Plot
Program
import [Link] as plt
x = [1,2,3,4]
y = [2,5,7,10]
[Link](x,y)
[Link]()
Output
🔵 Scatter plot displayed.
23. Read CSV File
Program
import pandas as pd
df = pd.read_csv("[Link]")
print([Link]())
Sample Output
Name Marks
0 Raj 80
1 Amit 75
24. NumPy Array Multiplication
Program
import numpy as np
a = [Link]([1,2,3])
print(a * 2)
Output
[2 4 6]
25. Find Maximum Value in NumPy Array
Program
import numpy as np
arr = [Link]([10,20,50,30])
print([Link]())
Output
50
Important Viva Questions from Module 4
1. What is a module?
2. What is a package?
3. What is a library?
4. Difference between module and package.
5. Difference between package and library.
6. What is an import statement?
7. What is aliasing?
8. What is NumPy?
9. Advantages of NumPy arrays.
10. Difference between list and NumPy array.
11. What is a Series?
12. What is a DataFrame?
13. Difference between Series and DataFrame.
14. What does head() do?
15. What does info() do?
16. What is Matplotlib?
17. Difference between line graph and bar chart.
18. What is a scatter plot?
19. What is a pie chart?
20. Why is data visualization important?