# 1) Reverse a string using a class
class ReverseString:
def __init__(self, text):
[Link] = text
def reverse(self):
return [Link][::-1]
# Example
obj = ReverseString("python")
print([Link]()) # nohtyp
# 2) Find factors of a number
num = int(input("Enter a number: "))
print("Factors of", num, "are:")
for i in range(1, num + 1):
if num % i == 0:
print(i)
# 3) Swap two numbers (without temporary variable)
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Before swap: a =", a, "b =", b)
a, b = b, a
print("After swap: a =", a, "b =", b)
# 4) Fibonacci series using generator
def fibonacci(n):
a, b = 0, 1
count = 0
while count < n:
yield a
a, b = b, a + b
count += 1
n = int(input("How many terms? "))
for num in fibonacci(n):
print(num, end=" ")
# 5) Calculator class (add, sub, mul, div)
class Calc:
def add(self, a, b):
return a + b
def sub(self, a, b):
return a - b
def mul(self, a, b):
return a * b
def div(self, a, b):
return a / b
c = Calc()
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
print("Addition:", [Link](x, y))
print("Subtraction:", [Link](x, y))
print("Multiplication:", [Link](x, y))
print("Division:", [Link](x, y))
# 6) Recursive factorial
def fact(n):
if n == 0 or n == 1:
return 1
else:
return n * fact(n - 1)
num = int(input("Enter a number: "))
print("Factorial of", num, "is", fact(num))
# 7) Check if key exists in a dictionary
data = {"name": "Arjun", "age": 20, "city": "Pune"}
key = input("Enter key to search: ")
if key in data:
print("Key exists. Value is:", data[key])
else:
print("Key does not exist.")
# 8) Tkinter Entry widget - insert and delete example
from tkinter import *
root = Tk()
[Link]("Entry Insert/Delete Example")
e = Entry(root)
[Link](padx=10, pady=10)
def insert_text():
[Link](0, "Hello")
def delete_text():
# delete from index 0 to end
[Link](0, END)
btn_insert = Button(root, text="Insert", command=insert_text)
btn_insert.pack(pady=5)
btn_delete = Button(root, text="Delete", command=delete_text)
btn_delete.pack(pady=5)
[Link]()
# 9) Simple GUI alert using button click
from tkinter import *
from tkinter import messagebox
root = Tk()
[Link]("Alert Example")
def show_alert():
[Link]("Alert", "Button Clicked")
btn = Button(root, text="Click Me", command=show_alert)
[Link](padx=20, pady=20)
[Link]()
# 10) GUI program for cylinder surface area and volume
from tkinter import *
import math
root = Tk()
[Link]("Cylinder SA and Volume")
def calc():
r = float(entry_radius.get())
h = float(entry_height.get())
sa = 2 * [Link] * r * (r + h) # surface area
v = [Link] * r * r * h # volume
label_sa.config(text="Surface Area = " + str(round(sa, 2)))
label_vol.config(text="Volume = " + str(round(v, 2)))
Label(root, text="Radius").pack()
entry_radius = Entry(root)
entry_radius.pack()
Label(root, text="Height").pack()
entry_height = Entry(root)
entry_height.pack()
Button(root, text="Compute", command=calc).pack(pady=5)
label_sa = Label(root, text="Surface Area = ")
label_sa.pack()
label_vol = Label(root, text="Volume = ")
label_vol.pack()
[Link]()
# 11) Program to reverse a number
num = int(input("Enter a number: "))
rev = 0
while num > 0:
digit = num % 10
rev = rev * 10 + digit
num //= 10
print("Reversed number:", rev)
# 12) Program to count vowels in a string
text = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for ch in text:
if ch in vowels:
count += 1
print("Number of vowels:", count)
# 13) Program to check if a number is prime
num = int(input("Enter number: "))
if num < 2:
print("Not Prime")
else:
for i in range(2, num):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime Number")
# 14) Program to find largest of three numbers
a = int(input("Enter first: "))
b = int(input("Enter second: "))
c = int(input("Enter third: "))
if a >= b and a >= c:
print("Largest:", a)
elif b >= a and b >= c:
print("Largest:", b)
else:
print("Largest:", c)
# 15) Program to check palindrome string
text = input("Enter string: ")
if text == text[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
# 16) Program to sum elements of a list
lst = [1, 2, 3, 4, 5]
total = 0
for n in lst:
total += n
print("Sum =", total)
# 17) Program to remove duplicates from a list
lst = [1, 2, 2, 3, 4, 4, 5]
unique = []
for item in lst:
if item not in unique:
[Link](item)
print("List without duplicates:", unique)
# 18) Program to convert Celsius to Fahrenheit
c = float(input("Enter Celsius: "))
f = (c * 9/5) + 32
print("Fahrenheit:", f)
# 19) Program to print multiplication table of a number
num = int(input("Enter number: "))
for i in range(1, 11):
print(num, "x", i, "=", num * i)
# 20) Program to count words in a string
text = input("Enter a sentence: ")
words = [Link]()
print("Number of words:", len(words))