Python 22
Python 22
Python:
Java:
● Static Typing: Java uses static typing, which can catch type errors at compile time.
● Performance: Generally faster execution due to Just-In-Time (JIT) compiler.
● Robustness: Strong type checking, which helps in building large applications.
● Multithreading: Built-in support for multithreaded programming.
● Platform Independence: Java bytecode can run on any platform with a JVM.
Dynamic Typing:
● Variables in Python are not bound to a specific data type and can change types during
execution.
Example:
python
Copy code
x = 10
x = "Hello"
Strongly Typed:
● Even though Python is dynamically typed, it enforces type constraints during operations.
Example:
python
Copy code
x = "10"
y = 5
print(x + y) # Raises a TypeError
●
3. Search for palindrome and unique words in a text using class method
and string method. 5+5=10 [CO4]
python
Copy code
class TextProcessor:
def __init__(self, text):
self.text = text
@classmethod
def is_palindrome(cls, word):
return word == word[::-1]
def find_palindromes(self):
words = self.text.split()
return [word for word in words if self.is_palindrome(word)]
def find_unique_words(self):
words = self.text.split()
return list(set(words))
4. 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()
python
Copy code
import socket
def tcp_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', 12345))
server_socket.listen(5)
print("TCP server up and listening")
while True:
client_socket, client_address = server_socket.accept()
print(f"Connection from {client_address}")
message = client_socket.recv(1024).decode()
print(f"Message from client: {message}")
client_socket.send("Hello, TCP client!".encode())
client_socket.close()
tcp_server()
List Functions:
python
Copy code
def fibonacci_gen():
a, b = 0, 1
for _ in range(7):
yield a
a, b = b, a + b
gen = fibonacci_gen()
for num in gen:
print(num)
7. Create a list of Tuples. Each Tuple should contain an item and its price in
float. Write a program to sort the tuples in descending order by price. Use
operator.itemgetter(). Write a program that proves that the dictionary
returned by globals() can be used to manipulate values of variables in it.
5+5=10 [CO2]
python
Copy code
from operator import itemgetter
python
Copy code
x = 10
y = 20
def manipulate_globals():
globals()['x'] = 15
globals()['y'] = 25
manipulate_globals()
print(x, y) # Output: 15 25
Stack Implementation:
python
Copy code
class Stack:
def __init__(self, size):
self.size = size
self.stack = []
def pop(self):
if not self.stack:
raise IndexError("Stack is empty")
return self.stack.pop()
# Example 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
These answers cover the provided questions comprehensively, addressing each with code
examples and brief explanations.