0% found this document useful (0 votes)
2 views

Q2_Solutions

The document explains various concepts in Python, including the Frame widget in Tkinter, function arguments, built-in dictionary functions, displaying the current date, and anonymous functions (lambda functions). It provides examples for each concept to illustrate their usage and functionality. The information is structured into sections, making it easy to understand the different aspects of Python programming.

Uploaded by

sagarthube
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)
2 views

Q2_Solutions

The document explains various concepts in Python, including the Frame widget in Tkinter, function arguments, built-in dictionary functions, displaying the current date, and anonymous functions (lambda functions). It provides examples for each concept to illustrate their usage and functionality. The information is structured into sections, making it easy to understand the different aspects of Python programming.

Uploaded by

sagarthube
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
You are on page 1/ 5

Q2) Attempt any four of the following:

a) Explain Frame widget in Tkinter with an example:

A Frame in Tkinter is a container widget used for grouping and organizing other widgets like buttons,

labels, or entry boxes.

It acts as a rectangular area that can include child widgets and is commonly used to divide a GUI

into logical sections.

Frames also support custom styling, such as background color, border width, and dimensions.

Example:

from tkinter import Tk, Frame, Button

root = Tk()

frame = Frame(root, bg="lightblue", width=300, height=150, borderwidth=5, relief="ridge")

frame.pack(padx=10, pady=10)

Button(frame, text="Button 1").pack(side="left", padx=5)

Button(frame, text="Button 2").pack(side="right", padx=5)

root.mainloop()

In this example, the Frame is styled with a blue background, border, and specific dimensions.

Two buttons are added inside the frame for illustration.

---
b) Explain function arguments in detail:

Functions in Python accept inputs known as arguments, which allow customization and flexibility.

Python provides four types of function arguments:

1. Positional Arguments: Values are passed in the same order as parameters.

Example: def add(a, b): return a + b -> add(3, 5) outputs 8.

2. Default Arguments: Provide a default value if no argument is passed.

Example: def greet(name, msg="Hello"): return f"{msg}, {name}".

3. Keyword Arguments: Arguments are identified by parameter names, making them

order-independent.

Example: greet(name="Alice", msg="Hi").

4. Variable-length Arguments: Use *args for multiple positional arguments and **kwargs for multiple

keyword arguments.

Example:

def func(*args, **kwargs):

print(args, kwargs)

func(1, 2, a=3, b=4) # Output: (1, 2) {'a': 3, 'b': 4}

These features make Python functions highly versatile and adaptable.

---

c) Explain any three built-in dictionary functions:

Python dictionaries have various built-in functions to handle key-value pairs effectively. Three

commonly used ones are:


1. keys(): Returns a view object of all keys in the dictionary, allowing iteration or conversion to a list.

Example:

d = {'a': 1, 'b': 2}

print(d.keys()) # Output: dict_keys(['a', 'b']).

2. values(): Returns a view object of all values in the dictionary.

Example:

print(d.values()) # Output: dict_values([1, 2]).

3. items(): Returns key-value pairs as tuples inside a view object. Useful for looping.

Example:

print(d.items()) # Output: dict_items([('a', 1), ('b', 2)]).

These functions simplify dictionary operations like data retrieval and iteration.

---

d) Write a Python program to display the current date:

To display the current date, the datetime module in Python is used. It provides classes for

manipulating dates and times.

Program:

from datetime import date

# Get current date

current_date = date.today()
print("Current Date:", current_date)

Explanation:

- date.today() fetches the current system date.

- The output is displayed in the YYYY-MM-DD format.

To customize the format, the strftime method can be used:

from datetime import datetime

print(datetime.now().strftime("%d-%m-%Y")) # Output: DD-MM-YYYY

This makes date handling versatile for applications like calendars or logs.

---

e) Write an anonymous function to find area:

An anonymous function, also called a lambda function, is a one-line, inline function in Python. It is

particularly useful for simple operations.

Example:

To find the area of a rectangle using a lambda function:

# Lambda function for area of a rectangle

area = lambda length, breadth: length * breadth

# Example usage

print("Area of rectangle:", area(5, 3)) # Output: 15.

Similarly, you can calculate the area of a circle:


# Lambda function for area of a circle

circle_area = lambda radius: 3.14 * radius ** 2

print("Area of circle:", circle_area(7)) # Output: 153.86.

These compact functions simplify repetitive or small computations.

You might also like