Python Lab Handout ECE
Python Lab Handout ECE
ENGINEERING
Prepared by
This week, we focus on understanding Python's basic data types, operators, and commonly used
built-in functions. The exercises will provide hands-on experience with Python's core concepts.
Question:
Write a Python script to illustrate the following data types:
• Integer (int)
• Character (char)
• Float (float)
• String (str)
Test Cases:
• Standard Cases:
o Input: x = 5 (Integer)
Expected Output: x is of type <class 'int'>
Solution Explanation:
• Python treats characters as strings, so char data type is represented as a single character
string.
Question:
Write a Python program to perform the following expressions, illustrating operator precedence:
1. 5 + 3 * 2
2. 2 * 3**2
3. 2**3**2
4. (2**3)**
Test Cases:
• Standard Cases:
o Input: 5 + 3 * 2
Expected Output: 11
o Input: 2 * 3**2
Expected Output: 18
o Input: 2**3**2
Expected Output: 512
o Input: (2**3)**2
Expected Output: 64
Solution Explanation:
Question:
Write a Python program to demonstrate type conversion using functions such as int(), float(), str(),
etc.
Test Cases:
• Standard Cases:
o Input: int('10')
Expected Output: 10
o Input: float('3.14')
Expected Output: 3.14
o Input: str(5)
Expected Output: '5'
o Input: int(3.99)
Expected Output: 3
Solution Explanation:
• int(), float(), and str() are used to convert between data types.
Question:
Write a Python program to illustrate the following functions of the math module: pi, sqrt(), cos(), and
sin().
Test Cases:
• Standard Cases:
o Input: [Link]
Expected Output: 3.141592653589793
o Input: [Link](16)
Expected Output: 4.0
o Input: [Link](0)
Expected Output: 1.0
o Input: [Link]([Link] / 2)
Expected Output: 1.0
Program Solution:
Solution Explanation:
• [Link]() and [Link]() compute the cosine and sine of an angle (in radians).
• Solution Hints: For each program, students can refer to the math documentation or Python's
official website for more examples and detailed explanations.
• Test Automation: Implementing automated test cases with a test framework (e.g., unittest or
pytest) can help students check their program's correctness.
Objective:
This week, the focus is on writing programs without using control statements. We will work on basic
calculations, string manipulations, and simple mathematical operations.
Question:
Write a Python program to calculate simple interest. The formula for simple interest is:
Where:
Test Cases:
• Standard Cases:
o Input: P = 1000, R = 5, T = 2
Expected Output: Simple Interest = 100.0
o Input: P = 1500, R = 7, T = 3
Expected Output: Simple Interest = 315.0
Solution Explanation:
• The formula for simple interest is applied directly without any control flow (like if, else,
loops).
• The values of P, R, and T are multiplied and divided as per the formula.
Question:
Write a Python program to calculate compound interest. The formula for compound interest is:
Where:
Test Cases:
• Standard Cases:
o Input: P = 1000, R = 5, T = 2
Expected Output: Compound Interest = 102.5
o Input: P = 1500, R = 7, T = 3
Expected Output: Compound Interest = 349.086
Program Solution:
Solution Explanation:
• The compound interest formula is directly applied using Python's exponentiation operator
**.
Question:
Write a Python program to print the ASCII value of a given character. Use the ord() function to find
the ASCII value.
Test Cases:
• Standard Cases:
o Input: 'A'
Expected Output: ASCII value of 'A' = 65
o Input: 'a'
Expected Output: ASCII value of 'a' = 97
o Input: '1'
Expected Output: ASCII value of '1' = 49
Program Solution:
Here's the Lab Manual for Week 2: Programs Without Control Statements:
Question:
Write a Python program to perform operations on a 1D NumPy array, including indexing and slicing.
Test Cases:
• Standard Cases:
o Expected Output:
Element at index 2: 3
Solution Explanation:
• Indexing: To access an element, you can use the array's index. In this case, arr[2] accesses
the element at index 2.
• Slicing: You can extract a subarray using the slicing syntax. In this case, arr[1:4] returns the
elements from index 1 to 3 (index 4 is excluded).
Question:
Write a Python program to perform operations on a 2D NumPy array, including indexing, slicing, and
reshaping.
Test Cases:
• Standard Cases:
▪ Indexing: Print the element at position (1, 2) (second row, third column).
▪ Slicing: Print the first two rows and columns (first two rows and columns).
Element at position (1, 2): 6 Sliced array (first 2 rows and columns): [[1 2] [4 5]] Reshaped array (1D):
[1 2 3 4 5 6 7 8 9]
Solution Explanation:
• Indexing: arr2d[1, 2] accesses the element in the second row and third column.
• Slicing: arr2d[:2, :2] slices the array to get the first two rows and columns.
• Reshaping: [Link](-1) flattens the 2D array into a 1D array, where -1 allows NumPy to
automatically calculate the size of the new dimension.
• Solution Hints:
o Indexing: Students should use the correct index format (arr[<row>, <col>]) for 2D
arrays.
o Slicing: Encourage students to experiment with different slice ranges for both 1D and
2D arrays.
o Reshaping: Point out that reshaping does not change the underlying data but just
changes its view.
2. Write a python program to count the number of even and odd numbers upto the given range.
3. Write a python program to print the multiplication table for a given number.
4. Write a python program to display minimum and maximum among three numbers
Objective:
This week, students will focus on learning and implementing control statements in Python. They will
write programs that involve decision-making (if-else) and looping (for, while) constructs to solve
different problems.
Question:
Write a Python program to calculate the power of a number without using built-in functions.
Test Cases:
• Standard Case:
Solution Explanation:
• The power function calculates the power of a number using a loop. If the exponent is
positive, it multiplies the base by itself for the given number of times. If the exponent is
negative, it divides 1 by the base for the absolute value of the exponent.
Program 2: Counting the Number of Even and Odd Numbers Up to a Given Range
Question:
Write a Python program to count the number of even and odd numbers up to a given range.
Test Cases:
• Standard Case:
o Input: range_limit = 10
o Input: range_limit = 15
Solution Explanation:
• The program loops through the range from 1 to the specified limit. It uses the modulo
operator % to check if the number is divisible by 2, thereby categorizing it as even or odd.
Question:
Write a Python program to print the multiplication table for a given number.
Test Cases:
• Standard Case:
o Input: num = 5
▪ Expected Output:
python
Copy code
5x1=5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50
Program Solution:
Solution Explanation:
• The multiplication_table function iterates from 1 to 10, multiplying the input number by the
loop index i and printing the result in a readable format.
Question:
Write a Python program to display the minimum and maximum values among three numbers.
Test Cases:
• Standard Case:
Solution Explanation:
• The program uses Python’s built-in min() and max() functions to find the minimum and
maximum values among the three input numbers.
• Solution Hints:
o Program 2 (Even and Odd Count): Remind students about the modulo operator %,
which is useful for checking divisibility.
o Program 3 (Multiplication Table): This is a simple use case for the for loop. Ensure
students understand how to format output.
o Program 4 (Min and Max): This can also be done with conditionals (if-else), but
Python's built-in functions min() and max() provide an elegant solution.
2. Write a python program to display Fibonacci series using iteration and recursion.
3. Write a python program to find the factorial of a number with and without recursion
Objective:
This week, students will learn about functions in Python, focusing on recursion and iteration. They
will write programs that use both types of approaches to solve problems.
Question:
Write a Python program to check if a number is prime or not, both with and without recursion.
Test Cases:
• Standard Case:
o Input: num = 7
o Input: num = 10
o Input: num = 1
Solution Explanation:
• Without Recursion: The program checks if a number is divisible by any number from 2 to
num-1. If it is divisible by any, it is not prime.
• With Recursion: This program works similarly but uses recursion to check divisibility by
decrementing the divisor until it reaches 1.
Question:
Write a Python program to display the Fibonacci series using both iteration and recursion.
Test Cases:
• Standard Case:
o Input: n = 5
Solution Explanation:
• Iterative: The program initializes two variables a and b to represent the first two numbers in
the Fibonacci series and iterates to calculate and append the next numbers.
• Recursive: The program builds the Fibonacci series by calling the function recursively to
calculate previous values and appending the sum of the last two numbers to the series.
Question:
Write a Python program to find the factorial of a number, both with and without recursion.
Test Cases:
• Standard Case:
o Input: num = 5
o Input: num = 3
▪ Expected Output: 3! = 6
o Input: num = 0
▪ Expected Output: 0! = 1
Solution Explanation:
• Without Recursion: The program calculates the factorial by iterating through the range of
numbers from 1 to the given number and multiplying them together.
• With Recursion: The recursive function calls itself with a decrementing number until it
reaches the base case (0! = 1 or 1! = 1), then multiplies the numbers as the recursion
unwinds.
• Solution Hints:
o Program 1 (Prime Check): Encourage students to think about both base cases (1 and
numbers less than 1) and the loop/recursion logic for checking divisibility.
o Program 2 (Fibonacci): Make sure students understand how recursion and iteration
can both be used to solve the same problem and discuss the efficiency differences.
o Program 3 (Factorial): Point out the similarities between the iterative and recursive
approaches for calculating factorials and discuss how recursion can be more elegant
but also less efficient for large numbers.
2. Write a python program to determine number of times a given letter occurs in a string
4. Illustrate in operator and write a python program to count number of lowercase characters in a
string. 5. Write a program to replace all the occurrences of letter ‘a’ with letter ‘x’ in a string.
Objective:
This week, students will work with various string operations and built-in functions in Python. The
goal is to practice string manipulation, searching, and replacement techniques using Python’s string
methods.
Question:
Write a Python program to illustrate the usage of common string built-in functions such as upper(),
lower(), replace(), split(), join(), and find().
Test Cases:
• Standard Case:
▪ Expected Output:
▪ Joined: Hello-World
Solution Explanation:
• The program demonstrates multiple string functions in Python. Each function is applied to
the string s to perform operations like changing case, replacing characters, splitting into a list,
and joining the elements of a list back into a string.
Question:
Write a Python program to determine the number of times a given letter occurs in a string.
Test Cases:
• Standard Case:
▪ Expected Output: 3
▪ Expected Output: 2
Solution Explanation:
• The program uses the built-in count() function of the string class to count how many times
the specified letter appears in the string.
Question:
Write a Python program to check if a string is a palindrome.
Test Cases:
• Standard Case:
o Input: s = "madam"
o Input: s = "hello"
Solution Explanation:
• The program checks if the string is equal to its reverse (s[::-1]). If they are the same, the
string is a palindrome.
Program 4: Illustrating the in Operator and Counting Number of Lowercase Characters in a String
Question:
Illustrate the use of the in operator and write a Python program to count the number of lowercase
characters in a string.
Test Cases:
• Standard Case:
▪ Expected Output:
▪ Lowercase letters: 8
Solution Explanation:
• The in operator checks if a substring exists within a string. The count_lowercase function
counts how many characters in the string are lowercase using islower().
Program 5: Replacing All Occurrences of Letter ‘a’ with Letter ‘x’ in a String
Question:
Write a Python program to replace all occurrences of the letter ‘a’ with the letter ‘x’ in a string.
Test Cases:
• Standard Case:
o Input: s = "banana"
o Input: s = "apple"
Solution Explanation:
• The program uses the replace() function to replace all occurrences of the letter 'a' with 'x' in
the string.
• Solution Hints:
o Program 4 (Lowercase Count): Discuss how string iteration with conditions can help
count specific types of characters (e.g., lowercase).
3. Write a python program to find the largest and smallest number in a list.
5. Write a python program to remove the duplicate items from a list. 6. Write a python program to
find sum of elements in a list
Objective:
In this week, students will practice working with lists, understanding common list operations, and
passing lists as arguments to functions. The goal is to familiarize students with list functions,
iteration, and list manipulation in Python.
Question:
Write a Python program to implement the following list functions:
• len()
• extend()
• sort()
• append()
• insert()
• remove()
Test Cases:
• Standard Case:
▪ Expected Output:
▪ Length of list: 6
Solution Explanation:
• The program demonstrates how to use various list methods like len(), extend(), sort(),
append(), insert(), and remove() to manipulate a list.
Question:
Write a Python program to pass a list as an argument to a function.
Test Cases:
• Standard Case:
Solution Explanation:
• This program demonstrates how to pass a list to a function. The function sum_of_list()
calculates the sum of all elements in the list using Python's built-in sum() function.
Question:
Write a Python program to find the largest and smallest number in a list.
Test Cases:
• Standard Case:
▪ Expected Output:
▪ Largest number: 99
▪ Smallest number: 4
Solution Explanation:
• The program uses Python's built-in max() and min() functions to find the largest and smallest
numbers in a list.
Question:
Write a Python program to merge two lists and then sort the resulting list.
Test Cases:
• Standard Case:
Solution Explanation:
• The program merges the two lists using the + operator and then sorts the resulting list using
the sort() method.
Question:
Write a Python program to remove duplicate items from a list.
Test Cases:
• Standard Case:
Solution Explanation:
• The program converts the list to a set, which automatically removes duplicates since sets do
not allow duplicate elements. Then, it converts the set back to a list.
Question:
Write a Python program to find the sum of elements in a list.
Test Cases:
• Standard Case:
Solution Explanation:
• The program uses the built-in sum() function to calculate the sum of the elements in the list.
• Solution Hints:
o Program 1 (List Functions): Discuss the importance of list operations like appending,
inserting, and removing elements. Also, explain how to sort lists and extend them
with other elements.
o Program 2 (Passing List to a Function): Explain how Python allows passing mutable
objects like lists to functions, which can then modify the original list if needed.
o Program 3 (Largest and Smallest Number): Explain how max() and min() work
efficiently to find extreme values in a list.
o Program 4 (Merging Lists): Discuss how merging lists works and the efficiency of
sorting a list after combining elements.
o Program 6 (Sum of List Elements): Highlight the use of sum() and its computational
efficiency when working with numeric lists.
1. Write a program to create a list of tuples with the first element as the number and the second
element as the square of the first element.
2. Write a python program that takes the list of tuples and sorts the list of tuples in increasing order
by the last element in each tuple.
3. Write a program to implement the following dictionary methods a) keys() b) values() c)items() d)
pop() e)delete()
4. Write a python program to add a key value pair to a dictionary and update the dictionary based on
the key.
Objective:
In this week, students will work on operations related to tuples and dictionaries in Python. The goal
is to help students understand the basic concepts of tuples and dictionaries, as well as common
operations and methods that can be applied to them.
Question:
Write a program to create a list of tuples where the first element is a number and the second
element is the square of the number.
Test Cases:
• Standard Case:
o Input: n = 5
▪ Expected Output: [(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
Solution Explanation:
• The function create_tuples() takes a number n and generates a list of tuples where the first
element is the number and the second element is the square of the number. The for loop
inside a list comprehension iterates through the numbers from 1 to n and creates a tuple
with each number and its square.
Question:
Write a Python program that takes a list of tuples and sorts the list of tuples in increasing order
based on the last element of each tuple.
Test Cases:
• Standard Case:
Solution Explanation:
• The sort_by_last_element() function uses the sorted() function with a lambda function as
the key to sort the list of tuples by the second element (x[1]) of each tuple.
Question:
Write a program to implement the following dictionary methods:
• keys()
• values()
• items()
• pop()
• delete()
Test Cases:
• Standard Case:
▪ Expected Output:
▪ Values: [1, 2, 3]
Solution Explanation:
o pop(): Removes and returns the value associated with a specific key.
o del: Deletes a key-value pair from the dictionary by specifying the key.
Question:
Write a Python program to add a key-value pair to a dictionary and update the dictionary based on
the key.
Test Cases:
• Standard Case:
▪ Expected Output: Dictionary after adding and updating: {'a': 1, 'b': 2, 'c': 3}
Solution Explanation:
• The add_and_update() function simply assigns a new key-value pair to the dictionary. If the
key already exists, the function updates the value associated with that key.
Question:
Write a program to do a reverse dictionary lookup in Python, i.e., given a value, find the
corresponding key.
Test Cases:
• Standard Case:
Solution Explanation:
• The reverse_lookup() function iterates over the dictionary and checks if the given value
exists. If it does, the function returns the corresponding key. If not, it returns None.
• Solution Hints:
o Program 1 (Creating Tuples): Explain the concept of tuples and how they are
immutable. Discuss how tuple elements can be computed (e.g., squaring the
number).
o Program 2 (Sorting Tuples): Discuss the importance of sorting and how sorting
tuples works when using the last element of the tuple.
Objective:
In this week, students will learn and work with different file handling operations in Python. The goal
is to help students understand how to read from and write to files, manipulate file pointers, and
work with file methods such as read(), readline(), write(), seek(), and flush().
Question:
Write a program to demonstrate the following file handling methods:
• read()
• readline()
• readlines()
• write()
• writelines()
Test Cases:
• Test Case 1: Writing to a file and then reading it using read(), readline(), and readlines().
Solution Explanation:
• The program demonstrates the usage of write() to write multiple lines to a file.
• The read() method reads the entire content of the file, while readline() reads the file one line
at a time. readlines() reads all lines into a list.
• After each read operation, we use seek(0) to reset the file pointer to the beginning of the file
to allow re-reading.
Question:
Write a program to demonstrate the seek(), tell(), and flush() methods in a file.
Test Cases:
• Test Case 1: Using seek() to change the file pointer's position and then using tell() to get the
current position.
• Test Case 2: Writing data to a file and using flush() to flush the buffer.
Solution Explanation:
• The seek() method is used to move the file pointer to a specific position. In this case, we
move the pointer to the 6th byte.
• The tell() method returns the current position of the file pointer.
• The flush() method ensures that any buffered data is written to the file immediately.
In this week, students will learn and work with different file handling operations in Python. The goal
is to help students understand how to read from and write to files, manipulate file pointers, and
work with file methods such as read(), readline(), write(), seek(), and flush().
Question:
Write a program to demonstrate the following file handling methods:
• read()
• readline()
• readlines()
• write()
• writelines()
Test Cases:
• Test Case 1: Writing to a file and then reading it using read(), readline(), and readlines().
Program Solution:
python
Copy code
print("Using read():")
print("Using readline():")
print("Using readlines():")
Solution Explanation:
• The program demonstrates the usage of write() to write multiple lines to a file.
• The read() method reads the entire content of the file, while readline() reads the file one line
at a time. readlines() reads all lines into a list.
• After each read operation, we use seek(0) to reset the file pointer to the beginning of the file
to allow re-reading.
Question:
Write a program to demonstrate the seek(), tell(), and flush() methods in a file.
Test Cases:
• Test Case 1: Using seek() to change the file pointer's position and then using tell() to get the
current position.
• Test Case 2: Writing data to a file and using flush() to flush the buffer.
Program Solution:
python
Copy code
# Writing to a file
[Link]("Hello, World!\n")
[Link]("This is a test.\n")
print("Using tell():")
Solution Explanation:
• The seek() method is used to move the file pointer to a specific position. In this case, we
move the pointer to the 6th byte.
• The tell() method returns the current position of the file pointer.
• The flush() method ensures that any buffered data is written to the file immediately.
Question:
Write a Python program that generates 20 random numbers in the range of 1 to 100 and writes them
to a file.
Test Cases:
• Test Case 1: Generating 20 random numbers between 1 and 100 and writing them to a file.
Solution Explanation:
• The program generates 20 random numbers using the [Link]() function and writes
each number to a file called random_numbers.txt.
• After writing, the file is opened again to read and verify the contents.
• Program 1:
o Emphasize the differences between the read(), readline(), and readlines() methods,
and discuss when to use each based on the use case.
o Demonstrate the importance of file pointers and how operations like seek() and tell()
help manipulate them.
• Program 2:
o Explain that seek() moves the file pointer to a specified byte position, and tell()
reports the current byte position.
o Discuss flush() as a method to force write to the disk before closing the file, ensuring
no data is lost.
• Program 3:
o Explain how random number generation works in Python using the random module.
o Discuss the importance of file I/O operations, particularly writing random data to a
file for logging or testing.
2. Write a program to inspect data in DataFrame using head(), tail () and describe() functions in
pandas.
Objective:
In Week 10, students will explore the Pandas module, focusing on importing data, inspecting and
analyzing data in a DataFrame, and performing sorting and slicing operations. This will help them
handle real-world datasets effectively and manipulate data efficiently using Pandas.
Question:
Write a program to import data from a CSV file into a Pandas DataFrame.
Test Cases:
• Test Case 1: Importing a CSV file and displaying the first few rows using head().
• Test Case 2: Checking the structure of the DataFrame by printing the columns and the shape
of the DataFrame.
Solution Explanation:
• head() displays the first 5 rows of the DataFrame (this is the default behavior, but you can
specify the number of rows).
• columns shows the column names, and shape gives the number of rows and columns in the
DataFrame.
Program 2: Inspecting Data in DataFrame Using head(), tail(), and describe() Functions
Question:
Write a program to inspect data in a DataFrame using the head(), tail(), and describe() functions in
Pandas.
Test Cases:
• Test Case 3: Display descriptive statistics (mean, median, etc.) using describe().
Solution Explanation:
• tail() returns the last 5 rows by default, which can be changed by passing a number.
Question:
Write a program to perform sorting and slicing operations in Pandas.
Test Cases:
• Test Case 2: Slice the DataFrame to display rows from index 10 to 20 and columns "Name"
and "Age".
Solution Explanation:
• sort_values(by='Age') sorts the DataFrame by the "Age" column. You can specify
ascending=False for descending order.
• loc[10:20, ['Name', 'Age']] slices the DataFrame to display rows from index 10 to 20 and
specific columns, like "Name" and "Age". Replace these column names based on your actual
dataset.
• Program 1:
o Discuss how importing data from CSV files into a DataFrame is a crucial step in data
analysis. Ensure students understand the importance of setting the correct file path.
o Show the utility of head(), which helps to quickly glance at the dataset, and shape,
which tells about the dataset's dimensions.
• Program 2:
o The tail() method is useful to inspect the last rows of the dataset, especially when
dealing with large datasets.
• Program 3:
o Sorting is essential when preparing data for analysis, especially when dealing with
large datasets that need to be organized.
o Slicing is useful for focusing on specific rows and columns, which is especially helpful
in exploratory data analysis (EDA) and when performing operations like filtering or
grouping data.
2. Design and develop a GUI application using Label, Entry and Button widgets.
3. Design and develop a GUI application using Tkinter Geometry methods pack(), grid(), place().
4. Design and develop a GUI application using CheckButton and Radiobutton widgets.
Objective:
In Week 11, students will learn how to design and develop GUI (Graphical User Interface)
applications using the Tkinter module in Python. These applications will demonstrate the use of
various Tkinter widgets and layout methods such as labels, buttons, entry fields, and geometry
managers.
Question:
Design and develop a simple GUI application that displays the message "Hello World" when the
application is launched.
Test Cases:
• Test Case 1: Verify if the "Hello World" message is displayed correctly on the window.
Solution Explanation:
• [Link]() runs the Tkinter event loop to display the window until it is closed by the
user.
Program 2: Design and Develop a GUI Application Using Label, Entry, and Button Widgets
Question:
Design and develop a GUI application that contains a label, an entry field for user input, and a
button. When the button is clicked, the entered text is displayed in the label.
Test Cases:
• Test Case 1: Verify that the label displays the text entered in the entry field when the button
is clicked.
Solution Explanation:
• The button widget triggers the update_label() function, which retrieves the text from the
entry widget and updates the label.
Program 3: Design and Develop a GUI Application Using Tkinter Geometry Methods (pack(), grid(),
place())
Question:
Design and develop a GUI application that demonstrates the use of Tkinter geometry methods:
pack(), grid(), and place().
Test Cases:
Solution Explanation:
• pack() automatically places widgets in the available space, stacking them vertically.
• grid(row, column) places widgets in a grid layout, allowing precise positioning in rows and
columns.
Program 4: Design and Develop a GUI Application Using Checkbutton and Radiobutton Widgets
Question:
Design and develop a GUI application that uses a Checkbutton and a Radiobutton. The check button
should allow the user to select multiple options, and the radio button should allow the user to select
only one option from a set of choices.
Test Cases:
• Test Case 2: Verify that only one radio button can be selected at a time.
Solution Explanation:
• Radiobutton uses a StringVar() to store the selected value. Only one radio button can be
selected at a time from a group.
• When the button is clicked, the display_selection() function is triggered to show the selected
checkbutton and radiobutton values.
• Program 1: The "Hello World" program demonstrates the basic structure of any Tkinter
application. It’s a simple example to introduce students to Tkinter.
• Program 2: Using Label, Entry, and Button widgets helps students understand how to interact
with the user and capture input, which is an essential part of GUI programming.
• Program 3: The use of different geometry managers (pack(), grid(), and place()) will help
students choose the appropriate layout method for their application.
• Program 4: The Checkbutton and Radiobutton widgets are fundamental for creating forms
where users select multiple or single options, respectively.
2. Design and develop a GUI application using Listbox and Scrollbar widgets.
3. Design and develop a GUI application using Messagebox and File Dialog widget
Objective:
In Week 12, students will continue their exploration of GUI (Graphical User Interface) development
using Tkinter. This week’s focus will be on advanced Tkinter widgets such as Menu, Menubutton,
Listbox, Scrollbar, Messagebox, and File Dialog.
Program 1: Design and Develop a GUI Application Using Menu and Menubutton Widgets
Question:
Design and develop a GUI application that includes a menu bar with a few options and a menubutton
that allows the user to select different options.
Test Cases:
• Test Case 1: Verify that the menu options work and perform the intended actions when
clicked.
• Test Case 2: Verify that the Menubutton dropdown appears when clicked.
Solution Explanation:
• The Menu widget creates a menu bar, and add_command() is used to add items to the
menu.
• The Menubutton widget creates a button with a dropdown list, and we define the options
inside the dropdown menu.
Program 2: Design and Develop a GUI Application Using Listbox and Scrollbar Widgets
Question:
Design and develop a GUI application that uses a Listbox to display a list of items and a Scrollbar to
allow scrolling through the list if it exceeds the visible area.
Test Cases:
• Test Case 1: Verify that the listbox displays the items correctly.
• Test Case 2: Verify that the scrollbar works when the list is too long to fit in the listbox.
Solution Explanation:
• The Listbox widget displays a list of items, and Scrollbar allows the listbox to be scrollable.
Program 3: Design and Develop a GUI Application Using Messagebox and File Dialog Widgets
Question:
Design and develop a GUI application that uses a Messagebox to display a message and a File Dialog
widget to allow the user to open a file using a file explorer dialog.
Test Cases:
• Test Case 1: Verify that the messagebox displays the correct message.
• Test Case 2: Verify that the file dialog allows the user to open a file and display its path.
Solution Explanation:
• The [Link]() method opens a file dialog to allow the user to select and
open a file. The selected file path is shown using a messagebox.
• Program 1: The Menu and Menubutton widgets demonstrate how to create simple menus
and drop-down buttons in a GUI, which are common elements in desktop applications.
• Program 2: Using the Listbox and Scrollbar widgets teaches students how to display lists of
items with the ability to scroll through long lists, a feature often seen in applications like file
explorers or contact lists.
• Program 3: The Messagebox and File Dialog widgets are essential for interactive applications,
providing users with feedback and enabling file selection functionality.