Python Manual Complete
Python Manual Complete
Python Programming
(1BPLC105B) / Laboratory
M Manual Prepared by
D Dr. Swetha G, Associate Professor
Prof. Kruthi T C, Assistant Professor
Department To arise as an excellent learning center in the field of Computer Science & Engineering by fostering a
skilled, innovative professionals to build a strong nation.
Vision
Department • To provide a conceptual foundation that caters the career required to adopt for changing
Mission technology in computer science.
• To bridge the gap between academics and the latest tools, technologies in the area of
hardware and software.
• To set out co-curricular open doors for student’s participation in advancements and recent
trends.
• To explore the potential and excel the students towards research to attain Novelty.
Program PEO1: Proficient to recognize contemporary issues and provide solutions using broad knowledge
Educational
of computer science.
Objectives
(PEOs) PEO2: Ability to plan, analyze, design, evolve project implementing capabilities and skills in IT
industry.
PEO3: Drive to adapt new computing technologies lifelong to acquire professional greatness.
PEO4: Possess professional, ethical, social responsibilities, communicational skills and team
work needed for a successful professional carrier.
Program PSO1: Apply the software practices, principals to design and analyse the complex computer based system.
Specific PSO2: Design, implement and validate system software and application software to the various societal
Outcomes needs.
(PSOs)
Program Outcomes (POs)
PO1 Engineering Knowledge: Apply knowledge of mathematics and science, with fundamentals of
Computer Science & Engineering to be able to solve complex engineering problems related to CSE.
PO2 Problem Analysis: Identify, Formulate, review research literature and analyse complex
engineering problems related to CSE and reaching substantiated conclusions using first principles
of mathematics, natural sciences and engineering sciences.
PO3 Design/Development of Solutions: Design solutions for complex engineering problems related to
CSE and design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety and the cultural societal and environmental
considerations.
PO4 Conduct Investigations of Complex Problems: Use research–based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
PO5 Modern Tool Usage: Create, Select and apply appropriate techniques, resources and modern
engineering and IT tools including prediction and modelling to computer science related complex
engineering activities with an understanding of the limitations.
PO6 The Engineer and Society: Apply Reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
CSE professional engineering practice.
PO7 Environment and Sustainability: Understand the impact of the CSE professional engineering
solutions in societal and environmental contexts and demonstrate the knowledge of, and need for
sustainable development.
PO8 Ethics: Apply Ethical Principles and commit to professional ethics and responsibilities and norms
of the engineering practice.
PO9 Individual and Team Work: Function effectively as an individual and as a member or leader in
diverse teams and in multidisciplinary Settings.
PO10 Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large such as able to comprehend and with write
effective reports and design documentation, make effective presentations and give and receive clear
instructions.
PO11 Project Management and Finance: Demonstrate knowledge and understanding of the
engineering management principles and apply these to one’s own work, as a member and leader in
a team, to manage projects and in multi-disciplinary environments.
PO12 Life-Long Learning: Recognize the need for and have the preparation and ability to engage in
independent and life-long learning the broadest context of technological change.
Programming Exercises:
[Link] Experiments Page number
1. a. Develop a python program to read 2 numbers from the keyboard and perform the basic 1-2
arithmetic operations based on the choice. (1-Add, 2-Subtract, 3-Multiply, 4-Divide).
b. Develop a program to read the name and year of birth of a person. Display whether the
person is a senior citizen or not.
2. a. Develop a program to generate Fibonacci sequence of length (N). Read N from the 3-5
console.
b. Write a python program to create a list and perform the following operations
• Inserting an element
• Removing an element
• Appending an element
• Displaying the length of the list
• Popping an element
• Clearing the list
3. a Read N numbers from the console and create a list. Develop a program to print mean, 6-7
variance and standard deviation with suitable messages.
b. Read a multi-digit number (as chars) from the console. Develop a program to print the
frequency of each digit with a suitable message.
4. Develop a program to print 10 most frequently appearing words in a text file. [Hint: Use a 8
dictionary with distinct words and their frequency of occurrences. Sort the dictionary in the
reverse order of frequency and display the dictionary slice of the first 10 items.
5. Develop a program to read 6 subject marks from the keyboard for a student. Generate a 9
report that displays the marks from the highest to the lowest score attained by the student.
[Read the marks into a 1-Dimesional array and sort using the Bubble Sort technique].
6. Develop a program to sort the contents of a text file and write the sorted contents into a 10
separate text file. [Hint: Use string methods strip(), len(), list methods sort(), append(), and
file methods open(), readlines(), and write()].
7. Develop a function named DivExp which takes TWO parameters a, b, and returns a value c 11
(c=a/b). Write a suitable assertion for a>0 in the function DivExp and raise an exception for
when b=0. Develop a suitable program that reads two console values and calls the function
DivExp.
8. Define a function that takes TWO objects representing complex numbers and returns a new 12
complex number with the sum of two complex numbers. Define a suitable class ‘Complex’ to
represent the complex number. Develop a program to read N (N >=2) complex numbers and
to compute the addition of N complex numbers.
9. Text Analysis Tool: Build a tool that analyses a paragraph: frequency of each word, longest 13
word, number of sentences, etc
10. Develop Data Summary Generator: Read a CSV file (like COVID data or weather stats), 14
convert to dictionary form, and allow the user to run summary queries: max, min, average by
column.
11. Develop Student Grade Tracker: Accept multiple students’ names and marks. Store them in a 15
list of tuples or dictionaries. Display summary reports (average, topper, etc.).
12. Develop a program to display contents of a folder recursively (Directory) having sub-folders 16
and files (name and type).
Extra program
13. Develop a program to implement a simple ATM Simulator with PIN verification, balance 17-18
inquiry, deposit, and withdrawal operations.
14. Develop a program to implement a Password Strength Checker that evaluates a password 19
based on length, uppercase/lowercase letters, digits, and special characters.
Python Programming
1. a. Develop a python program to read 2 numbers from the keyboard and perform the basic arithmetic
operations based on the choice. (1-Add, 2-Subtract, 3-Multiply, 4-Divide).
Sample Output
Select Operation:
1. Add
2. Subtract
3. Multiply
4. Divide
Enter first number: 10
Enter second number: 5
Enter your choice (1-4): 3
The product of 10.0 and 5.0 is: 50.0
1b. Develop a program to read the name and year of birth of a person. Display whether the person is a
senior citizen or not. print("Select Operation:")
Sample Output
Hello xyz!
2. a. Develop a program to generate Fibonacci sequence of length (N). Read N from the console.
SAMPLE OUTPUT :
Input
How many terms? 6
Output
Fibonacci sequence: 0
1
1
2
3
5
2b. Write a python program to create a list and perform the following operations
• Inserting an element
• Removing an element
• Appending an element
• Displaying the length of the list
• Popping an element
• Clearing the list
SAMPLE OUTPUT :
1. Insert an element
2. Remove an element
3. Append an element
5. Pop an element
8. Exit
3. a. Read N numbers from the console and create a list. Develop a program to print mean, variance and
standard deviation with suitable messages.
SAMPLE OUTPUT:
3 b. Read a multi-digit number (as chars) from the console. Develop a program to print the frequency of
each digit with a suitable message.
SAMPLE OUTPUT:
digit frequency
1 1
2 2
3 1
4 2
5 3
6 2
7 1
8 2
4) Develop a program to print 10 most frequently appearing words in a text file. [Hint: Use a dictionary
with distinct words and their frequency of occurrences. Sort the dictionary in the reverse order of
frequency and display the dictionary slice of the first 10 items.
How It Works:
Example:
Output:
python : 3
is : 3
fun : 1
and : 1
easy. : 1
powerful. : 1
5a) Develop a program to read 6 subject marks from the keyboard for a student. Generate a report that
displays the marks from the highest to the lowest score attained by the student. [Read the marks into a 1-
Dimesional array and sort using the Bubble Sort technique].
sample output:
90
88
80
75
70
62
6) Develop a program to sort the contents of a text file and write the sorted contents into a separate text
file. [Hint: Use string methods strip(), len(), list methods sort(), append(), and file methods open(),
readlines(), and write()].
SAMPLE OUTPUT:
Banana
Apple
Mango
Orange
Grapes
Apple
Banana
Grapes
Mango
Orange
7) Develop a function named DivExp which takes TWO parameters a, b, and returns a value c (c=a/b).
Write a suitable assertion for a>0 in the function DivExp and raise an exception for when b=0. Develop a
suitable program that reads two console values and calls the function DivExp.
SAMPLE OUTPUT:
Case 2: a <= 0
Case 3: b = 0
8) Define a function that takes TWO objects representing complex numbers and returns a new complex
number with the sum of two complex numbers. Define a suitable class ‘Complex’ to represent the complex
number. Develop a program to read N (N >=2) complex numbers and to compute the addition of N
complex numbers.
SAMPLE OUTPUT:
Enter how many complex numbers you want to add (N >= 2): 3
Enter Complex Number 1:
Enter real part: 2
Enter imaginary part: 3
Enter Complex Number 2:
Enter real part: 4
Enter imaginary part: 5
Enter Complex Number 3:
Enter real part: 1
Enter imaginary part: 2
The sum of all complex numbers is:
7.0 + 10.0i
9) Text Analysis Tool: Build a tool that analyses a paragraph: frequency of each word, longest word,
number of sentences, etc
Sample Output
Input:
Enter a paragraph:
Python is simple. Python is powerful and easy to learn. I love Python!
Output:
Word Frequency:
python : 3
is : 2
simple : 1
powerful : 1
and : 1
easy : 1
to : 1
learn : 1
i:1
love : 1
13 | P a g e Dept of CSE ,RRIT
Python Programming
10) Develop Data Summary Generator: Read a CSV file (like COVID data or weather stats), convert to dictionary
form, and allow the user to run summary queries: max, min, average by column.
Date,Temperature,Rainfall
2025-09-01,30,5
2025-09-02,32,0
2025-09-03,29,10
2025-09-04,31,2
Output:
11) Develop Student Grade Tracker: Accept multiple students’ names and marks. Store them in a list of
tuples or dictionaries. Display summary reports (average, topper, etc.)
SAMPLE OUTPUT:
12) Develop a program to display contents of a folder recursively (Directory) having sub-folders and files
(name and type).
Sample Output:
File: [Link]
File: [Link]
Folder: SubFolder1
File: [Link]
File: [Link]
Folder: SubFolder2
File: [Link]
Extra Program
13. Develop a program to implement a simple ATM Simulator with PIN verification, balance
inquiry, deposit, and withdrawal operations.
Sample output
14. Develop a program to implement a Password Strength Checker that evaluates a password based on length,
uppercase/lowercase letters, digits, and special characters.
Sample output
Viva questions:
1. What is Python?
Python is a high-level, interpreted, general-purpose programming language known for its simplicity and readability.
2. Who developed Python and when?
Python was developed by Guido van Rossum and first released in 1991.
3. What are the key features of Python?
Easy to read and write
Interpreted
Dynamically typed
Object-oriented
Large standard library
4. What is PEP 8?
PEP 8 is the Python Enhancement Proposal that provides coding style guidelines for Python.
5. How is Python interpreted?
Python code is executed line by line using the Python interpreter, which translates Python code into bytecode.
6. What are Python variables?
Variables are used to store data values in Python. You don’t need to declare their type explicitly.
7. How do you create a variable in Python?
x = 10
name = "Alice"
8. What are Python data types?
Common data types: int, float, str, bool, list, tuple, set, dict.
9. What is a list in Python?
A list is an ordered, mutable collection of elements.
my_list = [1, 2, 3]
10. What is a tuple?
A tuple is an ordered, immutable collection of elements.
my_tuple = (1, 2, 3)
11. Difference between list and tuple?
List: mutable, slower
Tuple: immutable, faster
12. What is a dictionary?
A dictionary stores key-value pairs.
my_dict = {"name": "Alice", "age": 20}
13. What is a set in Python?
A set is an unordered collection of unique elements.
my_set = {1, 2, 3}
14. What are Python operators?
Operators are symbols that perform operations on variables. Examples: +, -, *, /, %, **, //.
15. What is the difference between / and //?
/ returns a float division result
// returns an integer division result
16. What is a function in Python?
A function is a block of reusable code that performs a specific task.
17. How do you define a function in Python?
def add(a, b):
return a + b
18. What are Python loops?
Loops are used to execute a block of code multiple times. Types: for and while.
19. Difference between for and while loop?
for: Iterates over a sequence
while: Runs until a condition is false
20. What is an if-else statement?
It allows conditional execution of code blocks.
21. How do you write an if-else statement?
if x > 0:
print("Positive")
20 | P a g e Dept of CSE ,RRIT
Python Programming
else:
print("Non-positive")
22. What is indentation in Python?
Indentation is used to define blocks of code; Python does not use braces {}.
23. What are Python strings?
Strings are sequences of characters enclosed in quotes.
s = "Hello"
24. How do you access characters in a string?
By indexing: s[0] gives 'H'.
25. What is slicing?
Slicing extracts a part of a string or list:
s[0:3] # 'Hel'
26. What is the difference between mutable and immutable objects?
Mutable: Can be changed (list, dict, set)
Immutable: Cannot be changed (tuple, str, int, float)
27. What is Python’s None?
None represents the absence of a value or a null value.
28. What is Python’s pass statement?
pass is a null statement; it does nothing. Useful as a placeholder.
29. What is a Python module?
A module is a file containing Python code (functions, classes, variables) that can be imported.
30. How do you import a module?
import math
31. What is Python’s import statement?
It allows you to use functions, classes, and variables from another module.
32. What is Python’s dir() function?
dir() lists all the attributes and methods of an object.
33. What are Python comments?
Comments are lines ignored by the interpreter. Single-line: # comment, multi-line: ''' comment '''.
34. Difference between == and is?
== checks value equality
is checks object identity (same memory location)
35. What is Python’s len() function?
len() returns the length of an object (string, list, tuple, etc.)
36. What is Python’s type() function?
type() returns the type of an object.
37. What are Python boolean values?
Boolean values are True or False.
38. What is a Python list comprehension?
A concise way to create lists:
squares = [x*x for x in range(5)]
39. What is Python’s range() function?
range() generates a sequence of numbers.
for i in range(5): # 0 to 4
print(i)
40. What are Python exceptions?
Exceptions are runtime errors that can be handled using try-except.
41. How do you handle exceptions?
try:
x = 1/0
except ZeroDivisionError:
print("Cannot divide by zero")
42. What is Python’s finally block?
finally executes code no matter what, used for cleanup.
43. What are Python classes and objects?