Python 23
Python 23
1. Syntax Simplicity:
○ Python has simpler and more readable syntax compared to Java.
Example:
python
Copy code
# Python
print("Hello, World!")
// Java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
○
2. Dynamic Typing:
○ Python uses dynamic typing, meaning variables can change types, reducing
verbosity.
Example:
python
Copy code
x = 10
x = "Hello"
○
3. Faster Development:
○ Python allows for rapid prototyping and development due to its concise syntax
and extensive libraries.
4. Interpreted Language:
○ Python is interpreted, allowing for immediate feedback and easier debugging.
5. Extensive Libraries:
○ Python has a rich set of libraries and frameworks for various applications (e.g.,
data science, web development).
6. Memory Management:
○ Python handles memory management automatically with garbage collection.
2. Why Python is called a dynamic language? Discuss the chr(), int(), oct(),
complex(), and float() type conversion functions with examples. 5+5=10
[CO1]
Dynamic Language:
# Concatenating dictionaries
dict4 = {**dict1, **dict2, **dict3}
print("Concatenated Dictionary:", dict4)
Multithreading:
python
Copy code
import threading
def print_numbers():
for i in range(5):
print(i)
def print_letters():
for char in ['a', 'b', 'c', 'd', 'e']:
print(char)
# Creating threads
thread1 = threading.Thread(target=print_numbers)
thread2 = threading.Thread(target=print_letters)
# Starting threads
thread1.start()
thread2.start()
# Joining threads
thread1.join()
thread2.join()
5. Create a GUI application in Python that provides an Entry field where the
user can provide the name of a text file. Open the file and read it, displaying
its contents in a Label. You can also replace the Entry widget with a menu
that has a File Open option that pops up a window to allow the user to
specify the file to read. Also add an Exit or Quit option to the menu rather
than having a QUIT button. 10 [CO3]
python
Copy code
import tkinter as tk
from tkinter import filedialog, messagebox
def open_file():
file_path = filedialog.askopenfilename()
if file_path:
with open(file_path, 'r') as file:
content = file.read()
text_area.config(state=tk.NORMAL)
text_area.delete(1.0, tk.END)
text_area.insert(tk.END, content)
text_area.config(state=tk.DISABLED)
def quit_app():
root.quit()
root = tk.Tk()
root.title("File Reader")
menu_bar = tk.Menu(root)
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=quit_app)
menu_bar.add_cascade(label="File", menu=file_menu)
root.config(menu=menu_bar)
root.mainloop()
List Functions:
●
sum(): Returns the sum of elements in a list.
python
Copy code
my_list = [1, 2, 3]
print(sum(my_list)) # Output: 6
python
Copy code
def process_string(s):
words = s.split()
unique_words = list(set(words))
unique_words.sort()
return ' '.join(unique_words)
Generalization vs Encapsulation:
Generalization: The process of extracting shared characteristics from two or more classes and
combining them into a generalized superclass.
python
Copy code
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "Woof"
class Cat(Animal):
def speak(self):
return "Meow"
Encapsulation: The bundling of data and methods that operate on that data within a single unit
or class, and restricting access to some of the object's components.
python
Copy code
class Person:
def __init__(self, name, age):
self.__name = name # Private attribute
self.__age = age # Private attribute
def get_name(self):
return self.__name
def get_age(self):
return self.__age
●
Monkey Patching:
def patched_method():
print("Patched Method")
A.method = patched_method
obj = A()
obj.method() # Output: "Patched Method"
Stack Implementation:
python
Copy code
class Stack:
def __init__(self, size):
self.stack = []
self.size = size
def pop(self):
if not self.stack:
raise IndexError("Stack is empty")
return self.stack.pop()
# Usage
s = Stack(3)
s.push(1)
s.push(2)
s.push(3)
try:
s.push(4)
except IndexError as e:
print(e) # Output: Stack is full
print(s.pop()) # Output: 3
try:
s.pop()
s.pop()
s.pop()
except IndexError as e:
print(e) # Output: Stack is empty