0% found this document useful (0 votes)
4 views39 pages

Python Programming Lab Manual

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)
4 views39 pages

Python Programming Lab Manual

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

PYTHON PROGRAMMING – LAB MANUAL

Practical No.: 1
Title: Basic Python Syntax and Control Statements

Aim:To understand variables, data types, conditional statements and loops in


Python.

Tools: Jupyter Notebook / VS Code

Platform: Python 3.11.10

Program 1: Variables, Data Types & Operations


Algorithm:

1.​ Declare variables of different data types.​

2.​ Print their values.​

3.​ Perform arithmetic operations on two numbers.​

Code & Output:


Program 2: Conditional Statements
Algorithm:

1.​ Read marks and age.​

2.​ Check voting eligibility (age ≥ 18).​

3.​ Display grade based on marks.​

Code & output:


Program 3: Loops (For & While)
Algorithm:

1.​ Print numbers 1 to 10.​

2.​ Print table of 5.​

3.​ Print star pattern.​

Code & Output:


Conclusion
In this practical, we successfully:

●​ Declared variables of different data types.​

●​ Performed arithmetic operations.​

●​ Used conditional statements (if-elif-else) for decision-making.​

●​ Applied loops (for and while) to repeat tasks.​

●​ Printed number sequences, tables, and patterns using loops.


Practical No.: 2
Title: Data Structures – Lists, Tuples, Sets, Dictionaries

Aim:To explore Python’s core data structures: Lists, Tuples, Sets, and
Dictionaries.

Tools Required:Jupyter Notebook / VS Code

Platform: Python 3.11.10

PROGRAM 1: List Operations


Algorithm:

1.​ Create a list.​

2.​ Perform insert, append, remove, and slicing operations.​

3.​ Display results.​

Code & Output


PROGRAM 2: Tuple Operations
Algorithm:

1.​ Create a tuple.​

2.​ Access elements using indexing and slicing.​

3.​ Count elements.​

Code & Output :


PROGRAM 3: Set Operations
Algorithm:

1.​ Create sets.​

2.​ Perform union, intersection, and difference.​

Code & Output :


PROGRAM 4: Dictionary Operations
Algorithm:

1.​ Create a dictionary.​

2.​ Add, update, and delete elements.​

3.​ Print keys and values.​

Code & Output:

Conclusion:
In this practical, we explored Python’s core data structures:

●​ Performed insertion, deletion, and slicing on lists.​

●​ Accessed and counted elements in tuples.​


●​ Applied union, intersection, and difference on sets.​

●​ Added, updated, and removed data in dictionaries.​

These data structures help store and manage data efficiently in Python.

Practical No.: 3
Title: Functions and Recursion

Aim:To define and call functions, understand parameter types, return values,
and implement recursion.

Tools Required:Jupyter Notebook / VS Code

Platform: Python 3.11.10

Program 1: Simple Function (definition, parameters, return)


Algorithm:

1.​ Define a function add(a, b) that returns sum.​

2.​ Call the function with values and print result.​

Code & Output:


Program 2: Function with Default & Keyword Arguments
Algorithm:

1.​ Define greet(name, msg="Hello").​

2.​ Call with and without keyword/default args.​

Code:
Program 3: Recursive Factorial
Algorithm:

1.​ If n == 0 or 1 return 1.​

2.​ Else return n * factorial(n-1).​

Code& Output:

Program 4: Recursive Fibonacci (n-th term)


Algorithm:

1.​ If n == 0 return 0; if n == 1 return 1.​

2.​ Else return fib(n-1) + fib(n-2).​


(Note: simple recursion; for large n use memoization.)​

Code & Output:


Program 5: Recursion with Memoization (efficient Fibonacci)
Algorithm:

1.​ Use a cache (dictionary) to store computed values.​

2.​ Return cached value if present, otherwise compute and store.​

Code & Output:


Conclusion:
In this practical we:

●​ Defined and called functions with positional, default, and keyword arguments.​

●​ Used return to get results from functions.​

●​ Implemented recursion (factorial, Fibonacci).​

●​ Demonstrated memoization to optimize recursive algorithms.

Practical No.: 4
Title: Lambda and Built-in Functions

Aim:To use anonymous functions (lambda) and built-in functional tools map,
filter, reduce, zip, and enumerate.

Tools Required:Jupyter Notebook / VS Code

Platform: Python 3.11.10

Program 1: Lambda (anonymous functions)


Algorithm:
1.​ Define simple lambda functions for add and square.​

2.​ Call and print results.​

Code & Output :

Program 2: map (apply function to all items)


Algorithm:

1.​ Create a list of numbers.​

2.​ Use map with lambda to square each number.​

3.​ Convert result to list and print.​

Code & Output


Program 3: filter (select items by condition)
Algorithm:

1.​ Use filter with lambda to keep even numbers.​

2.​ Convert to list and print.​

Code& Output:

Program 4: reduce (aggregate values)


Algorithm:

1.​ Use reduce to compute sum or product of list.​

2.​ Print result.​


(reduce is in functools)​

Code & Output:


Program 5: zip (combine iterables) and enumerate (indexing)
Algorithm:

1.​ Create parallel lists (names, ages).​

2.​ Use zip to pair them.​

3.​ Use enumerate to print indexed pairs.​

Code & Output :


Program 6: Combined example (map + filter + reduce)
Algorithm:

1.​ Square numbers, keep only squares > 10, sum them.​

Code & Output :

Conclusion:
In this practical we:

●​ Used lambda for concise anonymous functions.​

●​ Applied map to transform sequences.​

●​ Used filter to select items by condition.​

●​ Used reduce to aggregate values.​

●​ Combined zip and enumerate to pair and index items.​


These built-ins simplify functional-style operations and make code concise and expressive.
Practical No.: 5
Title: Classes and Objects

Aim:To implement classes, constructors, methods, and create objects in


Python.

Tools Required:Jupyter Notebook / VS Code

Platform: Python 3.11.10

Program 1: Basic Class with Constructor and Methods


Algorithm:

1.​ Define a Student class with __init__ to initialize name and roll.​

2.​ Add a method display() to print student details.​

3.​ Create object(s) and call methods.​

Code & Output:


Program 2: Class with Methods (setters/getters) and Encapsulation
Algorithm:

1.​ Use a "private" attribute (_marks) and methods to set/get it.​

2.​ Show controlled access.​

Code & Output:


Program 3: Class Variable, Instance Variable, and str
Algorithm:

1.​ Use a class variable college.​

2.​ Implement __str__ for readable print.​

Code & Output:


Program 4: Inheritance (Single Inheritance)
Algorithm:

1.​ Create Person base class.​

2.​ Derive Teacher class that inherits and adds subject attribute.​

Code & Output:


Program 5: Simple Example of Polymorphism (Method Overriding)
Algorithm:

1.​ Show two classes with same method name speak() implemented differently.​

2.​ Call speak() on each.​

Code & Output:


Conclusion
In this practical, we successfully:

●​ Implemented classes and constructors (__init__).​

●​ Created objects and called instance methods.​

●​ Used class variables and instance variables.​

●​ Applied encapsulation using setter/getter style methods.​

●​ Demonstrated inheritance and method overriding (polymorphism).​

These OOP concepts help model real-world entities and promote code reusability and organization.

Practical No.: 6
Title: Inheritance and Polymorphism
Aim:To demonstrate single inheritance, multilevel inheritance, and method
overriding using polymorphism.

Tools Required:Jupyter Notebook / VS Code

Platform: Python 3.11.10

Program 1: Single Inheritance


Algorithm:

1.​ Create a base class Person.​

2.​ Create a derived class Student inheriting from Person.​

3.​ Use super() to access parent constructor.​

4.​ Display details.​

Code & Output:


Program 2: Multilevel Inheritance
Algorithm:

1.​ Create base class Animal.​

2.​ Create intermediate class Dog.​

3.​ Create derived class Puppy inheriting from Dog.​

4.​ Call methods from each level.​

Code & Output:


Program 3: Method Overriding (Polymorphism)
Algorithm:

1.​ Create base class Shape.​

2.​ Create derived classes Circle and Square overriding area() method.​

3.​ Call the method through different objects to show polymorphism.​

Code & Output :


Program 4: Polymorphism using Common
Method Names
Algorithm:

1.​ Define multiple classes with same method name speak().​

2.​ Call the method on each object demonstrating polymorphism.​

Code & Output:


Conclusion:
In this practical, we:

●​ Implemented single inheritance to reuse parent class attributes and methods.​

●​ Demonstrated multilevel inheritance to show multi-step class derivation.​

●​ Applied method overriding to redefine parent class methods in child classes.​

●​ Showed polymorphism, where the same method name performs different actions based on the object.​

These concepts improve code reusability and support flexible object-oriented design.
Practical No.: 7
Title: Web App Using Flask

Aim:To create a basic Flask web application with routing.

Tools Required:Flask, VS Code

Platform: Python 3.11.10

Algorithm:
1.​ Install Flask using pip install flask.​

2.​ Import Flask class in Python file.​

3.​ Create a Flask object (app).​

4.​ Define routes using @[Link]().​

5.​ Return simple HTML responses.​

6.​ Run the application using [Link]().​

Program: Basic Flask Web Application


How to Run (Command):
python [Link]

Then open browser and visit:

●​ [Link] → Home​

●​ [Link] → About​

●​ [Link] → Contact​

Sample Output (Browser View):


Home Page:
Welcome to My Flask Web App

About Page:
This is the About Page
Contact Page:
Contact: info@[Link]

Conclusion:
In this practical, we:

●​ Installed and configured Flask.​

●​ Created a simple web application using Flask.​

●​ Implemented routing for multiple pages (home, about, contact).​

●​ Learned how Flask handles HTTP requests and responses.​

This practical introduces the basics of building web applications in Python using Flask.

Practical No.: 8
Title: Form Handling in Flask with SQLite

Aim:To create an HTML form in Flask and store submitted data into an
SQLite database.

Tools Required:Flask, SQLite, VS Code (or any code editor)

Platform: Python 3.x

Algorithm:
1.​ Create Flask app and configure SQLite database.​

2.​ Create an HTML form template for user input (name, email, message).​

3.​ Create route to render form (GET) and to handle submission (POST).​

4.​ Validate and insert form data into SQLite DB.​

5.​ Show success/failure message or display stored records.​


Files & Code

How to Run
1.​ Create project folder, save [Link] and templates/ files as above.​

(Optional) create virtualenv and install Flask:​



python -m venv venv
source venv/bin/activate # Linux/macOS
# venv\Scripts\activate # Windows
pip install flask

2.​

Run:​

python [Link]

3.​ Open browser: [Link] → Fill form → Submit → View records at /records.

Conclusion:
In this practical, we:
●​ Created a Flask web app with routes for form display and submission.​

●​ Implemented simple validation and stored form data into an SQLite database.​

●​ Displayed stored records in a tabular format.​


This demonstrates basic form handling and data persistence using Flask + SQLite.​
Practical No.: 9
Title: Data Analysis with NumPy and Pandas

Aim:
To manipulate arrays using NumPy and perform data analysis using Pandas DataFrame.

Tools Required:
Jupyter Notebook, NumPy, Pandas

Platform: Python 3.12.3

Program 1: NumPy Array Creation and


Operations
Algorithm:
1.​ Import NumPy.​

2.​ Create 1D and 2D arrays.​

3.​ Perform basic operations (sum, mean, reshape).​

4.​ Print results.​

Code & Output:


Program 2: NumPy – Element-wise and Matrix
Operations
Algorithm:
1.​ Create two arrays.​

2.​ Perform add, multiply, and matrix multiplication.​


Code & Output:

Program 3: Pandas – Creating and Displaying a


DataFrame
Algorithm:
1.​ Import Pandas.​

2.​ Create a dictionary of lists.​

3.​ Convert it into a DataFrame.​

4.​ Display first few rows.​


Code & Output:

Program 4: Pandas – Filtering and Basic


Statistics
Algorithm:
1.​ Use DataFrame from previous program.​

2.​ Filter rows based on condition.​

3.​ Compute mean, max, and min.​


Code & Output:

Program 5: Pandas – Reading CSV File


(Use any sample CSV)

Algorithm:
1.​ Read CSV file using read_csv.​

2.​ Print first rows.​

Code & Output:


Conclusion:
In this practical, we:

●​ Created and manipulated arrays using NumPy.​

●​ Performed mathematical and matrix operations.​

●​ Created and analyzed Pandas DataFrames.​

●​ Filtered data and calculated basic statistical values.​

These tools are essential for data analysis and preprocessing in Python.

You might also like