Python Assignment 2
Python Assignment 2
Ans:
Tkinter provides several geometry management methods to organize widgets within a
window or frame. The three main geometry managers are:
1. pack()
2. grid()
3. place()
Each has its own strengths and use cases. Let's explore each with examples.
1. pack() Method
The pack() method organizes widgets in blocks before placing them in the parent
widget.
Example:
import tkinter as tk
root = tk.Tk()
root.title("Pack Example")
# Create widgets
label1 = tk.Label(root, text="Top", bg="red", fg="white")
label2 = tk.Label(root, text="Bottom", bg="green", fg="white")
label3 = tk.Label(root, text="Left", bg="blue", fg="white")
label4 = tk.Label(root, text="Right", bg="yellow", fg="black")
# Pack widgets
label1.pack(side="top", fill="x", padx=10, pady=5)
label2.pack(side="bottom", fill="x", padx=10, pady=5)
label3.pack(side="left", fill="y", padx=5, pady=10)
label4.pack(side="right", fill="y", padx=5, pady=10)
root.mainloop()
The grid() method organizes widgets in a table-like structure with rows and columns.
Example:
import tkinter as tk
root = tk.Tk()
root.title("Grid Example")
# Create widgets
label1 = tk.Label(root, text="Row 0, Column 0", bg="red", fg="white")
label2 = tk.Label(root, text="Row 0, Column 1", bg="green", fg="white")
label3 = tk.Label(root, text="Row 1, Column 0", bg="blue", fg="white")
label4 = tk.Label(root, text="Row 1, Column 1", bg="yellow", fg="black")
root.mainloop()
Example:
import tkinter as tk
root = tk.Tk()
root.title("Place Example")
root.geometry("300x200")
# Absolute positioning
label1 = tk.Label(root, text="Absolute: 50, 50", bg="red", fg="white")
label1.place(x=50, y=50)
# Relative positioning
label2 = tk.Label(root, text="Relative: 0.5, 0.5", bg="green", fg="white")
label2.place(relx=0.5, rely=0.5, anchor="center")
root.mainloop()
● x, y: Absolute coordinates
● relx, rely: Relative coordinates (0.0 to 1.0)
● anchor: Position reference point ("n", "s", "e", "w", "center", etc.)
● width, height: Absolute size
● relwidth, relheight: Relative size (0.0 to 1.0)
class Blog(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
def blog_list(request):
blogs = Blog.objects.all()
return render(request, 'blog/list.html', {'blogs': blogs})
Scenario:
Imagine a restaurant (web server) where:
● Each customer (client) places an order (HTTP request).
● The chef (CPU) prepares meals (processes requests).
Without Multithreading (Single-Threaded):
● The chef takes one order at a time → Other customers wait idle → Slow service.
With Multithreading:
● The chef delegates tasks to multiple assistants (threads).
● While one assistant cooks, another can take new orders → Faster service.
4. How we can get current thread name and how to change default
thread name in multithreading.
Ans :
def task():
print(f"Current thread name: {threading.current_thread().name}")
thread = threading.Thread(target=task)
thread.start()
thread.join()
Output :
Current thread name: Thread-1 # Default naming (Thread-1, Thread-2, etc.)
def task():
print(f"Thread name: {threading.current_thread().name}")
# Assign a custom name
thread = threading.Thread(target=task, name="Worker-Thread")
thread.start()
thread.join()
Output :
Thread name: Worker-Thread # Custom name instead of Thread-1
def task():
threading.current_thread().name = "Renamed-Thread"
print(f"Updated thread name: {threading.current_thread().name}")
thread = threading.Thread(target=task)
thread.start()
thread.join()
Output:
Updated thread name: Renamed-Thread
1. Creating Arrays
import numpy as np
# From a list
arr1 = np.array([1, 2, 3]) # 1D array
arr2 = np.array([[1, 2], [3, 4]]) # 2D array
# Special arrays
zeros = np.zeros((2, 3)) # 2x3 array of 0s
ones = np.ones((3, 2)) # 3x2 array of 1s
range_arr = np.arange(0, 10, 2) # Array [0, 2, 4, 6, 8]
3. Reshaping Arrays
arr = np.arange(6) # [0, 1, 2, 3, 4, 5]
reshaped = arr.reshape((2, 3)) # Converts to 2x3 matrix
5. Statistical Operations
data = np.array([1, 2, 3, 4, 5])
6. Matrix Multiplication
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(series)
Output:
a 10
b 20
c 30
d 40
dtype: int64
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Tokyo']
}
df = pd.DataFrame(data)
print(df)
Output :
Name Age City
0 Alice 25 New York
1 Bob 30 London
2 Charlie 35 Tokyo