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

KVR - Python Lab Manual

The CS 408 Python Programming Lab course at Government Polytechnic, Parkal, aims to equip students with practical skills in Python programming through various exercises, including basic programs, data structures, GUI applications, and database connections. The document outlines course outcomes, contents, and specific programming tasks, such as calculating factorials, checking Armstrong numbers, and performing matrix operations. Additionally, it provides details on popular Python IDEs and includes sample input/output for several programming exercises.
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 views92 pages

KVR - Python Lab Manual

The CS 408 Python Programming Lab course at Government Polytechnic, Parkal, aims to equip students with practical skills in Python programming through various exercises, including basic programs, data structures, GUI applications, and database connections. The document outlines course outcomes, contents, and specific programming tasks, such as calculating factorials, checking Armstrong numbers, and performing matrix operations. Additionally, it provides details on popular Python IDEs and includes sample input/output for several programming exercises.
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

CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

CS 408 PYTHON PROGRAMING LAB

Course Outcomes :

On completion of the course, the student should be able to;

[Link] REDDY, Lecturer in ECE


1
1
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Course Contents :

1. Familiarize with python IDE


2. Exercise on Basic python programs(factorial, Armstrong number, area of the
circle, Fibonacci)
3. Write a python programs to perform matrix addition and multiplication
4. Write a python programs on various data structures
5. Exercise programs on Threads
6. Exercise programs on Exceptions
7. Write a python program to achieve thread synchronization in multithreaded
environment
8. Design Graphical user interface application using different widgets 9. Design
GUI using different Geometry Managers
10. Develop a python program to handle events generated by various widgets
11. Develop a python program to open, close, read, write, and append data into
the files
12 Develop a python program to connect to MySql database

[Link] REDDY, Lecturer in ECE


2
2
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

13. Develop a python program for creation of table, insert a row in a table, and
update an entry in a table
14. Develop a python program to execute stored procedures [Link] the
installation of various packages and modules numpy, pandas, and matplotlib.

[Link] REDDY, Lecturer in ECE


3
3
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Familiarize with python IDE

What is a Python IDE?

A Python IDE is a software application that provides a complete


environment to write, edit, run, and debug Python programs easily.
Instead of using multiple tools separately, an IDE combines everything in one
place.

Why do we need a Python IDE?

Using an IDE helps beginners and professionals to:

• Write code faster


• Find errors easily
• Run and test programs smoothly
• Manage projects efficiently

Main Components of a Python IDE

A typical Python IDE includes:

1. Code Editor
o Used to write Python programs
o Supports syntax highlighting and indentation
2. Interpreter / Runner
o Executes Python code line by line
o Shows output and errors
3. Debugger
o Helps find and fix errors step by step
o Supports breakpoints and variable inspection
o

[Link] REDDY, Lecturer in ECE


4
4
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

4. Console / Terminal
o Runs Python commands interactively
5. File & Project Manager
o Organizes Python files and folders

Popular Python IDEs


[Link] (Python’s Default IDE)

• Comes pre-installed with Python


• Best for beginners
• Simple and lightweight

Features:

o Interactive shell
o Basic editor
o Syntax highlighting

Example:

print("Hello, Python!")

[Link] REDDY, Lecturer in ECE


5
5
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

2. VS Code (Visual Studio Code)

• Free and very popular


• Requires Python extension
• Suitable for students and professionals

Features:

• Smart code completion


• Debugging support
• Git integration
• Extensions for Python, Django, Flask

[Link] REDDY, Lecturer in ECE


6
6
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

3. PyCharm

• Dedicated Python IDE

• Available in Community (free) and Professional (paid) versions

Features:

Advanced debugging

• Auto code suggestions


• Project-based development

[Link] REDDY, Lecturer in ECE


7
7
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 1
Exercise on Basic Python Programs

1(a) Program to Find Factorial of a Number

Aim

To write and execute a Python program to find the factorial of a given number.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Read an integer n

3. Initialize fact = 1

4. Multiply numbers from 1 to n

5. Display factorial value

6. Stop

Program Code

[Link] REDDY, Lecturer in ECE


8
8
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Input

Enter a number: 5

Sample Output

Factorial = 120

Result

Thus, the Python program to find the factorial of a number was executed successfully.

Viva Voce Questions

1. What is factorial?

2. What is the use of for loop?

3. What happens if input is 0?

4. Which data type is used for factorial?

[Link] REDDY, Lecturer in ECE


9
9
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

1(b) Program to Check Armstrong Number

Aim

To write and execute a Python program to check whether a given number is an


Armstrong number.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Read a number n

3. Find the sum of cubes of digits

4. Compare the sum with original number

5. Display result

6. Stop

Program Code

[Link] REDDY, Lecturer in ECE


10
10
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Input

Enter a number: 153

Sample Output

Armstrong Number

Result

Thus, the Python program to check Armstrong number was executed successfully.

Viva Voce Questions

1. What is an Armstrong number?

2. Which operator is used to extract digits?

3. What is the use of while loop?

[Link] REDDY, Lecturer in ECE


11
11
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

1(c) Program to Find Area of a Circle

Aim

To write and execute a Python program to calculate the area of a circle.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Read radius r

3. Calculate area = π × r × r

4. Display the area

5. Stop

Program Code

Sample Input

Enter radius: 7

Sample Output

Area of Circle = 153.86

Result

Thus, the Python program to find the area of a circle was executed successfully.

[Link] REDDY, Lecturer in ECE


12
12
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Viva Voce Questions

1. What is the value of π?

2. Why is radius declared as float?

3. What is an expression?

[Link] REDDY, Lecturer in ECE


13
13
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

1(d) Program to Generate Fibonacci Series

Aim

To write and execute a Python program to generate the Fibonacci series.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Read number of terms n

3. Initialize first two numbers

4. Generate next terms by addition

5. Display the series

6. Stop

Program Code

Sample Input

Enter number of terms: 6

Sample Output

Fibonacci Series:

011235

[Link] REDDY, Lecturer in ECE


14
14
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Result

Thus, the Python program to generate Fibonacci series was executed successfully.

Viva Voce Questions

1. What is Fibonacci series?

2. How many variables are used?

3. What is tuple assignment?

[Link] REDDY, Lecturer in ECE


15
15
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 2
Decision Making and Looping Programs

2(a) Program to Check Whether a Number is Even or Odd

Aim

To write and execute a Python program to check whether a given number is even or odd
using decision making statements.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Read an integer number

3. Check if the number is divisible by 2

4. If true, print Even

5. Else, print Odd

6. Stop

Program Code

[Link] REDDY, Lecturer in ECE


16
16
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Input

Enter a number: 9

Sample Output

Odd Number

Result

Thus, the Python program to check even or odd number was executed successfully.

Viva Voce Questions

1. What is decision making in Python?

2. Which operator is used to find remainder?

3. What is the use of if–else?

[Link] REDDY, Lecturer in ECE


17
17
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

2(b) Program to Find Largest of Three Numbers

Aim

To write and execute a Python program to find the largest of three numbers.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Read three numbers a, b, c

3. Compare values using if–elif–else

4. Display the largest number

5. Stop

Program Code

Sample Input

Enter first number: 10

Enter second number: 25

Enter third number: 15

[Link] REDDY, Lecturer in ECE


18
18
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Output

B is the largest

Result

Thus, the Python program to find the largest of three numbers was executed
successfully.

Viva Voce Questions

1. What is elif?

2. What are relational operators?

3. Can we solve this using nested if?

[Link] REDDY, Lecturer in ECE


19
19
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

2(c) Program to Check Leap Year

Aim

To write and execute a Python program to check whether a given year is a leap year.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Read year value

3. Check leap year condition

4. Display result

5. Stop

Program Code

Sample Input

Enter year: 2024

Sample Output

Leap Year

Result

Thus, the Python program to check leap year was executed successfully.

[Link] REDDY, Lecturer in ECE


20
20
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Viva Voce Questions

1. What is a leap year?

2. Why is modulo operator used?

3. Explain logical operators used.

[Link] REDDY, Lecturer in ECE


21
21
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

2(d) Program to Print Multiplication Table

Aim

To write and execute a Python program to print the multiplication table of a given
number using loop.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Read an integer n

3. Use for loop from 1 to 10

4. Print multiplication table

5. Stop

Program Code

Sample Input

Enter a number: 5

Sample Output

5x1=5

5 x 2 = 10

5 x 3 = 15

5 x 4 = 20

5 x 5 = 25

[Link] REDDY, Lecturer in ECE


22
22
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

5 x 6 = 30

5 x 7 = 35

5 x 8 = 40

5 x 9 = 45

5 x 10 = 50

Result

Thus, the Python program to print multiplication table was executed successfully.

Viva Voce Questions

1. What is a loop?

2. Difference between for and while

3. What does range() function do?

[Link] REDDY, Lecturer in ECE


23
23
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 3
Matrix Addition and Matrix Multiplication

3(a) Program to Perform Matrix Addition

Aim

To write and execute a Python program to perform addition of two matrices.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Read number of rows and columns

3. Read elements of first matrix

4. Read elements of second matrix

5. Add corresponding elements

6. Display the resultant matrix

7. Stop

[Link] REDDY, Lecturer in ECE


24
24
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Program Code

Sample Input

Enter number of rows: 2

Enter number of columns: 2

Enter elements of Matrix A:

1 2

3 4

Enter elements of Matrix B:

5 6

7 8

[Link] REDDY, Lecturer in ECE


25
25
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Output

Resultant Matrix after Addition:

[6, 8]

[10, 12]

Result

Thus, the Python program to perform matrix addition was executed successfully.

Viva Voce Questions

1. What is a matrix?

2. How are matrices stored in Python?

3. What is list of lists?

4. What is matrix addition rule?

[Link] REDDY, Lecturer in ECE


26
26
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

3(b) Program to Perform Matrix Multiplication

Aim

To write and execute a Python program to perform multiplication of two matrices.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Read order of two matrices

3. Read elements of both matrices

4. Multiply matrices using nested loops

5. Display resultant matrix

6. Stop

Program Code

[Link] REDDY, Lecturer in ECE


27
27
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Input

Enter rows of first matrix: 2

Enter columns of first matrix: 2

Enter rows of second matrix: 2

Enter columns of second matrix: 2

Enter elements of Matrix A:

1 2

3 4

Enter elements of Matrix B:

5 6

7 8

[Link] REDDY, Lecturer in ECE


28
28
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Output

Resultant Matrix after Multiplication:

[19, 22]

[43, 50]

Result

Thus, the Python program to perform matrix multiplication was executed successfully.

Viva Voce Questions

1. What is matrix multiplication?

2. What condition must be satisfied for multiplication?

3. What is the role of the third loop?

4. Can matrix multiplication be done using libraries?

[Link] REDDY, Lecturer in ECE


29
29
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 4
Programs on Python Data Structures

4(a) Program on List Operations

Aim

To write and execute a Python program to perform various list operations such as
creation, insertion, deletion, and traversal.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Create a list with elements

3. Insert an element into the list

4. Delete an element from the list

5. Display the list

6. Stop

Program Code

[Link] REDDY, Lecturer in ECE


30
30
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Output

Original List: [10, 20, 30, 40]

After Insertion: [10, 20, 30, 40, 50]

After Deletion: [10, 30, 40, 50]

Result

Thus, the Python program to perform list operations was executed successfully.

Viva Voce Questions

1. What is a list?

2. Is list mutable?

3. Difference between append() and insert()?

[Link] REDDY, Lecturer in ECE


31
31
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

4(b) Program on Tuple Operations

Aim

To write and execute a Python program to demonstrate tuple operations.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Create a tuple

3. Access elements using index

4. Display the tuple

5. Stop

Program Code

Sample Output

Tuple: (10, 20, 30, 40)

First element: 10

Last element: 40

[Link] REDDY, Lecturer in ECE


32
32
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Result

Thus, the Python program to perform tuple operations was executed successfully.

Viva Voce Questions

1. What is a tuple?

2. Is tuple mutable?

3. Difference between list and tuple?

[Link] REDDY, Lecturer in ECE


33
33
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

4(c) Program on Set Operations

Aim

To write and execute a Python program to perform set operations.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Create two sets

3. Perform union, intersection, and difference

4. Display results

5. Stop

Program Code

Sample Output

Union: {1, 2, 3, 4, 5, 6}

Intersection: {3, 4}

Difference: {1, 2}

[Link] REDDY, Lecturer in ECE


34
34
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Result

Thus, the Python program to perform set operations was executed successfully.

Viva Voce Questions

1. What is a set?

2. Does set allow duplicate elements?

3. What is intersection?

[Link] REDDY, Lecturer in ECE


35
35
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

4(d) Program on Dictionary Operations

Aim

To write and execute a Python program to perform dictionary operations.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Create a dictionary

3. Add and update elements

4. Display dictionary

5. Stop

Program Code

Sample Output

Original Dictionary: {'name': 'Ravi', 'roll': 101, 'marks': 85}

Updated Dictionary: {'name': 'Ravi', 'roll': 101, 'marks': 90, 'grade': 'A'}

[Link] REDDY, Lecturer in ECE


36
36
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Result

Thus, the Python program to perform dictionary operations was executed successfully.

Viva Voce Questions

1. What is a dictionary?

2. How are keys and values stored?

3. Is dictionary mutable?

[Link] REDDY, Lecturer in ECE


37
37
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 5
Exercise Programs on Multithreading

5(a) Program to Create a Thread Using threading Module

Aim

To write and execute a Python program to create and run a thread using the threading
module.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Import threading module

3. Define a function to be executed by thread

4. Create a thread object

5. Start the thread

6. Stop

Program Code

[Link] REDDY, Lecturer in ECE


38
38
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Output

Thread is running

Result

Thus, the Python program to create a thread using the threading module was executed
successfully.

Viva Voce Questions

1. What is a thread?

2. What is multithreading?

3. Which module is used for threading in Python?

4. What is the purpose of start() method?

[Link] REDDY, Lecturer in ECE


39
39
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

5(b) Program to Demonstrate Multiple Threads

Aim

To write and execute a Python program to demonstrate multiple threads executing


simultaneously.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Import threading module

3. Define two functions

4. Create two thread objects

5. Start both threads

6. Stop

Program Code

[Link] REDDY, Lecturer in ECE


40
40
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Output

Task 1 running

Task 2 running

Task 1 running

Task 2 running

Task 1 running

Task 2 running

([Link])

Result

Thus, the Python program to demonstrate multiple threads was executed successfully.

Viva Voce Questions

1. What is concurrent execution?

2. Why output order changes in multithreading?

3. What is sleep() function?

4. Can Python run threads in parallel?

[Link] REDDY, Lecturer in ECE


41
41
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

5(c) Program Using Thread Class (Extending Thread)

Aim

To write and execute a Python program by extending the Thread class.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Import threading module

3. Create a class extending Thread

4. Override run() method

5. Create object and start thread

6. Stop

Program Code

Sample Output

Thread class is running

Result

Thus, the Python program using Thread class was executed successfully.

[Link] REDDY, Lecturer in ECE


42
42
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Viva Voce Questions

1. What is Thread class?

2. Difference between function thread and class thread?

3. What is run() method?

[Link] REDDY, Lecturer in ECE


43
43
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 6
Exercise Programs on Exception Handling

6(a) Program Using try and except Blocks

Aim

To write and execute a Python program to demonstrate exception handling using try
and except blocks.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Read two numbers

3. Perform division operation

4. Handle exceptions using try and except

5. Display result

6. Stop

Program Code

[Link] REDDY, Lecturer in ECE


44
44
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Input / Output

Enter first number: 10

Enter second number: 0

Error occurred

Result

Thus, the Python program demonstrating exception handling using try and except
blocks was executed successfully.

Viva Voce Questions

1. What is an exception?

2. What is the purpose of try block?

3. What happens if exception is not handled?

[Link] REDDY, Lecturer in ECE


45
45
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

6(b) Program to Handle ZeroDivisionError

Aim

To write and execute a Python program to handle ZeroDivisionError.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Read two numbers

3. Perform division

4. Catch ZeroDivisionError

5. Display message

6. Stop

Program Code

Sample Input / Output

Enter numerator: 5

Enter denominator: 0

Cannot divide by zero

Result

Thus, the Python program to handle ZeroDivisionError was executed successfully.

[Link] REDDY, Lecturer in ECE


46
46
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Viva Voce Questions

1. What is ZeroDivisionError?

2. When does this error occur?

3. Can multiple except blocks be used?

[Link] REDDY, Lecturer in ECE


47
47
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

6(c) Program Using else and finally Blocks

Aim

To write and execute a Python program demonstrating the use of else and finally blocks
in exception handling.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Read two numbers

3. Perform division inside try

4. Execute else if no exception

5. Execute finally always

6. Stop

Program Code

Sample Input / Output

Enter first number: 10

Enter second number: 2

Result: 5.0

Execution completed

[Link] REDDY, Lecturer in ECE


48
48
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Result

Thus, the Python program demonstrating try, except, else, and finally blocks was
executed successfully.

Viva Voce Questions

1. What is the use of finally block?

2. When is else block executed?

3. Can finally block be skipped?

[Link] REDDY, Lecturer in ECE


49
49
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 7
Thread Synchronization in Multithreaded Environment

7(a) Program to Demonstrate Thread Synchronization Using Lock

Aim

To write and execute a Python program to achieve thread synchronization in a


multithreaded environment using Lock.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Import threading module

3. Create a shared variable

4. Create a lock object

5. Define a function to access shared resource

6. Acquire lock before accessing shared data

7. Release lock after execution

8. Stop

[Link] REDDY, Lecturer in ECE


50
50
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Program Code

Sample Output

Count: 1

Count: 2

Count: 3

Count: 4

Count: 5

Count: 6

Count: 7

Count: 8

Count: 9

Count: 10

Final Count: 10

[Link] REDDY, Lecturer in ECE


51
51
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Result

Thus, the Python program to achieve thread synchronization using Lock was executed
successfully.

Viva Voce Questions

1. What is thread synchronization?

2. Why is synchronization needed?

3. What is a lock?

4. What happens if lock is not used?

[Link] REDDY, Lecturer in ECE


52
52
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

7(b) Program to Demonstrate Thread Synchronization Using Semaphore

Aim

To write and execute a Python program to achieve thread synchronization using


Semaphore.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Import threading module

3. Create a semaphore object

4. Define a function to access shared resource

5. Acquire semaphore before execution

6. Release semaphore after execution

7. Stop

Program Code

[Link] REDDY, Lecturer in ECE


53
53
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Output

Thread-1 entered critical section

Thread-1 leaving critical section

Thread-2 entered critical section

Thread-2 leaving critical section

Result

Thus, the Python program demonstrating thread synchronization using Semaphore was
executed successfully.

Viva Voce Questions

1. What is semaphore?

2. Difference between lock and semaphore?

3. What is critical section?

4. Can semaphore value be greater than 1?

[Link] REDDY, Lecturer in ECE


54
54
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 8
Design a Graphical User Interface (GUI) Application Using Different
Widgets
([Link])

8(a) GUI Application Using Label, Entry, and Button Widgets

Aim

To design and develop a Python GUI application using different widgets such as Label,
Entry, and Button.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Import tkinter module

3. Create main application window

4. Add Label, Entry, and Button widgets

5. Define button action

6. Display the GUI

7. Stop

[Link] REDDY, Lecturer in ECE


55
55
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Program Code

Sample Output

• A window appears with:

o A label asking for name

o An entry box

o A button

• On clicking the button, the message “Hello <name>” is displayed.

[Link] REDDY, Lecturer in ECE


56
56
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Output

Result

Thus, the Python program to design a GUI application using Label, Entry, and Button
widgets was executed successfully.

Viva Voce Questions

1. What is GUI?

2. Which module is used to design GUI in Python?

3. What is the purpose of mainloop()?

4. What is a widget?

[Link] REDDY, Lecturer in ECE


57
57
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

8(b) GUI Application Using Checkbutton and Radiobutton

Aim

To design and develop a Python GUI application using Checkbutton and Radiobutton
widgets.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Import tkinter module

3. Create main window

4. Add Checkbutton and Radiobutton widgets

5. Display the window

6. Stop

Program Code

[Link] REDDY, Lecturer in ECE


58
58
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Output

• A GUI window with:

o One Checkbutton

o Two Radiobuttons

Output:

Result

Thus, the Python program to design a GUI application using Checkbutton and
Radiobutton widgets was executed successfully.

Viva Voce Questions

1. What is Checkbutton?

2. What is Radiobutton?

3. Difference between Checkbutton and Radiobutton?

4. What is IntVar()?

[Link] REDDY, Lecturer in ECE


59
59
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

8(c) GUI Application Using Listbox Widget

Aim

To design and develop a Python GUI application using Listbox widget.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Import tkinter module

3. Create main window

4. Add Listbox widget

5. Insert items into listbox

6. Display window

7. Stop

Program Code

[Link] REDDY, Lecturer in ECE


60
60
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Output

• A GUI window displaying a list of programming languages.

Result

Thus, the Python program to design a GUI application using Listbox widget was
executed successfully.

Viva Voce Questions

1. What is Listbox?

2. How are items added to Listbox?

3. Can multiple selections be allowed?

[Link] REDDY, Lecturer in ECE


61
61
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 9
Design GUI Using Different Geometry Managers
([Link])

9(a) GUI Design Using pack() Geometry Manager

Aim

To design and develop a Python GUI application using the pack() geometry manager.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Import tkinter module

3. Create the main window

4. Add widgets (Label, Button)

5. Arrange widgets using pack()

6. Display the window

7. Stop

[Link] REDDY, Lecturer in ECE


62
62
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Program Code

Sample Output

• A window with labels arranged at top and bottom

• A button placed in the center

Result

Thus, the Python program to design a GUI using pack() geometry manager was executed
successfully.

Viva Voce Questions

1. What is a geometry manager?

2. What is the use of pack()?

[Link] REDDY, Lecturer in ECE


63
63
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

3. What are the sides available in pack()?

9(b) GUI Design Using grid() Geometry Manager

Aim

To design and develop a Python GUI application using the grid() geometry manager.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Import tkinter module

3. Create main window

4. Place widgets in rows and columns using grid()

5. Display window

6. Stop

Program Code

[Link] REDDY, Lecturer in ECE


64
64
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Output

• A login-style form with labels and entry boxes arranged in rows and columns

Result

Thus, the Python program to design a GUI using grid() geometry manager was executed
successfully.

Viva Voce Questions

1. What is the use of grid()?

2. What are row and column parameters?

3. Can pack() and grid() be used together?

[Link] REDDY, Lecturer in ECE


65
65
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

9(c) GUI Design Using place() Geometry Manager

Aim

To design and develop a Python GUI application using the place() geometry manager.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Import tkinter module

3. Create main window

4. Place widgets using x and y coordinates

5. Display window

6. Stop

Program Code

[Link] REDDY, Lecturer in ECE


66
66
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Output

• A window where widgets are placed using absolute positions

Result

Thus, the Python program to design a GUI using place() geometry manager was
executed successfully.

Viva Voce Questions

1. What is place() geometry manager?

2. Difference between pack(), grid(), and place()?

3. Which geometry manager uses coordinates?

[Link] REDDY, Lecturer in ECE


67
67
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 10
Event Handling in Graphical User Interface
([Link])

10(a) Program to Handle Button Click Event

Aim

To write and execute a Python program to handle a button click event generated by a
GUI widget.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Import tkinter module

3. Create main window

4. Add Button widget

5. Define event handling function

6. Bind function to button click

7. Display window

8. Stop

[Link] REDDY, Lecturer in ECE


68
68
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Program Code

Sample Output

• A window with a button

• On clicking the button, the message “Button Clicked” is displayed

Result

Thus, the Python program to handle button click event was executed successfully.

Viva Voce Questions

1. What is an event?

2. What is event handling?

3. Which parameter is used to handle button click?

[Link] REDDY, Lecturer in ECE


69
69
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

10(b) Program to Handle Mouse Events

Aim

To write and execute a Python program to handle mouse click events in a GUI
application.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Import tkinter module

3. Create main window

4. Bind mouse event to function

5. Display mouse position

6. Stop

Program Code

[Link] REDDY, Lecturer in ECE


70
70
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Output

• On clicking anywhere in the window, mouse x and y coordinates are displayed

Result

Thus, the Python program to handle mouse events was executed successfully.

Viva Voce Questions

1. What is <Button-1>?

2. What information does event object contain?

3. What is event binding?

[Link] REDDY, Lecturer in ECE


71
71
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

10(c) Program to Handle Keyboard Events

Aim

To write and execute a Python program to handle keyboard events in a GUI application.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Import tkinter module

3. Create main window

4. Bind keyboard event

5. Display pressed key

6. Stop

Program Code

[Link] REDDY, Lecturer in ECE


72
72
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Sample Output

• On pressing any key, the pressed key character is displayed

Result

Thus, the Python program to handle keyboard events was executed successfully.

Viva Voce Questions

1. What is a keyboard event?

2. What does [Link] represent?

3. Difference between key press and key release events?

[Link] REDDY, Lecturer in ECE


73
73
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 11
File Handling in Python

11(a) Program to Open and Close a File

Aim

To write and execute a Python program to open and close a file.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Open a file in write mode

3. Close the file

4. Display confirmation message

5. Stop

Program Code

Sample Output

File opened successfully

File closed successfully

Result

Thus, the Python program to open and close a file was executed successfully.

[Link] REDDY, Lecturer in ECE


74
74
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Viva Voce Questions

1. What is file handling?

2. What does open() function do?

3. Why is it important to close a file?

[Link] REDDY, Lecturer in ECE


75
75
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

11(b) Program to Write Data into a File

Aim

To write and execute a Python program to write data into a file.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Open a file in write mode

3. Write data into the file

4. Close the file

5. Stop

Program Code

Sample Output

Data written into file successfully

([Link]¡txt.file)

[Link] REDDY, Lecturer in ECE


76
76
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Result

Thus, the Python program to write data into a file was executed successfully.

Viva Voce Questions

1. What is write mode?

2. What happens if file already exists?

3. Difference between write and append mode?

[Link] REDDY, Lecturer in ECE


77
77
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

11(c) Program to Read Data from a File

Aim

To write and execute a Python program to read data from a file.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Open the file in read mode

3. Read file content

4. Display content

5. Close the file

6. Stop

Program Code

Sample Output File Content:

Welcome to Python File Handling

This is a sample file

Result

Thus, the Python program to read data from a file was executed successfully.

Viva Voce Questions

1. What is read mode?

2. Difference between read() and readline()?

3. What happens if file does not exist?

[Link] REDDY, Lecturer in ECE


78
78
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

11(d) Program to Append Data into a File

Aim

To write and execute a Python program to append data into a file.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

Algorithm

1. Start

2. Open the file in append mode

3. Add new data to the file

4. Close the file

5. Stop

Program Code

Sample Output

Data appended successfully

([Link]¡txt.file)

Result

Thus, the Python program to append data into a file was executed successfully.

Viva Voce Questions

1. What is append mode?

2. Does append overwrite data?

3. What is the file pointer?

[Link] REDDY, Lecturer in ECE


79
79
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 12
Python Program to Connect to MySQL Database

Aim

To write and execute a Python program to connect to a MySQL database using Python.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

• MySQL Server

• MySQL Connector for Python (mysql-connector-python)

Algorithm

1. Start

2. Import [Link] module ( pip install mysql-connector-python)

3. Establish connection with MySQL server using host, user, password

4. Check whether the connection is successful ( pip show mysql-connector-


python)

5. Display appropriate message

6. Close the connection

7. Stop

pip install mysql-connector-python

[Link] REDDY, Lecturer in ECE


80
80
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Program Code

Sample Output

Connected to MySQL database successfully

MySQL connection closed

Result

Thus, the Python program to connect to the MySQL database was executed
successfully.

Viva Voce Questions

1. What is a database?

2. Which module is used to connect Python with MySQL?

3. What are the parameters required for database connection?

4. What is the use of is_connected() method?

5. Why should database connections be closed?

[Link] REDDY, Lecturer in ECE


81
81
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Important Notes

• Ensure MySQL server is running before executing the program

• Username and password must match MySQL credentials

• Install connector using:

pip install mysql-connector-python

[Link] REDDY, Lecturer in ECE


82
82
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 13
Database Operations: Create Table, Insert Record, and Update Record

Aim

To write and execute a Python program to create a table, insert a record, and update a
record in a MySQL database.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

• MySQL Server

• MySQL Connector for Python (mysql-connector-python)

Algorithm

1. Start

2. Import [Link] module

3. Establish connection with MySQL database

4. Create a table using SQL query

5. Insert records into the table

6. Update records in the table

7. Display confirmation messages

8. Close database connection

9. Stop

[Link] REDDY, Lecturer in ECE


83
83
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Program Code

Sample Output

Table created successfully

Record inserted successfully

Record updated successfully

Result

Thus, the Python program to create a table, insert a record, and update a record in
MySQL database was executed successfully.

[Link] REDDY, Lecturer in ECE


84
84
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Viva Voce Questions

1. What is SQL?

2. What is a table?

3. What is primary key?

4. What is the use of commit()?

5. What happens if commit is not used?

Important Notes

• Database testdb must exist before running the program

• Table creation uses IF NOT EXISTS to avoid errors

• commit() is mandatory for insert and update operations

[Link] REDDY, Lecturer in ECE


85
85
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 14
Execute Stored Procedures Using Python
Aim

To write and execute a Python program to call and execute a stored procedure in a
MySQL database.

Software Requirements

• Operating System: Windows / Linux

• Python Version: Python 3.x

• Editor/IDE: IDLE / VS Code / PyCharm

• MySQL Server

• MySQL Connector for Python (mysql-connector-python)

Pre-requisite (Stored Procedure Creation in MySQL)

DELIMITER //

CREATE PROCEDURE get_student()

BEGIN

SELECT * FROM student;

END //

DELIMITER ;

([Link]¡)

Algorithm

1. Start

2. Import [Link] module

3. Establish connection with MySQL database

4. Create cursor object

5. Call stored procedure using callproc()

6. Fetch and display results

7. Close database connection

8. Stop

[Link] REDDY, Lecturer in ECE


86
86
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Program Code

Sample Output

(101, 'Ravi', 90)

([Link])

Result

Thus, the Python program to execute a stored procedure in MySQL database was
executed successfully.

Viva Voce Questions

1. What is a stored procedure?

2. Why are stored procedures used?

3. What is the use of callproc()?

4. Can stored procedures accept parameters?

5. Where are stored procedures stored?

Important Notes

• Stored procedure must be created before running Python program

• Table student should exist with records

[Link] REDDY, Lecturer in ECE


87
87
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Experiment – 15
Installation and Demonstration of NumPy, Pandas, and Matplotlib
Aim

To demonstrate the installation and basic usage of Python packages NumPy, Pandas,
and Matplotlib.

Software Requirements

• Python 3.x

• Internet connection

• Command Prompt / Terminal

15(a) Installation of Required Packages

Aim

To install NumPy, Pandas, and Matplotlib using pip.

Algorithm

1. Start

2. Open Command Prompt

3. Execute pip install commands

4. Verify installation

5. Stop

Command

pip install numpy pandas matplotlib

Sample Output

Successfully installed numpy pandas matplotlib

Result

Thus, the required Python packages were installed successfully.

Viva Voce Questions

1. What is pip?

2. What is a package?

3. Why are external libraries required?

[Link] REDDY, Lecturer in ECE


88
88
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

15(b) Program Using NumPy

Aim

To write and execute a Python program using NumPy for array operations.

Algorithm

1. Import numpy module

2. Create a NumPy array

3. Perform array operations

4. Display result

5. Stop

Program Code

Sample Output

Array: [1 2 3 4 5]

Sum: 15

Mean: 3.0

Result

Thus, the Python program using NumPy was executed successfully.

Viva Voce Questions

1. What is NumPy?

2. What is ndarray?

3. Why is NumPy faster than lists?

[Link] REDDY, Lecturer in ECE


89
89
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

15(c) Program Using Pandas

Aim

To write and execute a Python program using Pandas to create and display a
DataFrame.

Algorithm

1. Import pandas module

2. Create dictionary data

3. Convert dictionary to DataFrame

4. Display DataFrame

5. Stop

Program Code

Sample Output

Name Marks

0 Ravi 85

1 Anu 90

2 Kiran 78

Result

Thus, the Python program using Pandas DataFrame was executed successfully.

Viva Voce Questions

1. What is Pandas?

2. What is DataFrame?

3. Difference between Series and DataFrame?

[Link] REDDY, Lecturer in ECE


90
90
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

15(d) Program Using Matplotlib

Aim

To write and execute a Python program using Matplotlib to plot a simple graph.

Algorithm

1. Import [Link] module

2. Provide x and y values

3. Plot the graph

4. Display the graph

5. Stop

Program Code

Sample Output

• A line graph is displayed showing x vs y values.

[Link] REDDY, Lecturer in ECE


91
91
CS 408 PYTHON PROGRAMING LAB GOVERNMENT POLYTECHNIC, PARKAL

Result

Thus, the Python program using Matplotlib to plot a graph was executed successfully.

Viva Voce Questions

1. What is Matplotlib?

2. What is [Link]()?

3. What types of graphs can Matplotlib draw?

[Link] REDDY, Lecturer in ECE


92
92

You might also like