Here’s a comprehensive list of Python programs covering everything from literals to advanced OOP concepts like inheritance, overloading, overriding, and polymorphism.
๐น 1๏ธโฃ Python Literals
๐ Program: Demonstrate different types of literals.
# Numeric Literals
a = 10 # Integer
b = 3.14 # Float
c = 0b1010 # Binary
d = 0o12 # Octal
e = 0xA # Hexadecimal
# String Literals
str1 = "Hello"
str2 = 'Python'
# Boolean Literals
t = True
f = False
# Special Literal
x = None
print(a, b, c, d, e, str1, str2, t, f, x)
๐น 2๏ธโฃ Variables and Data Types
๐ Program: Declare and print different data types.
name = "Alice"
age = 25
height = 5.5
is_student = True
print(type(name), type(age), type(height), type(is_student))
๐น 3๏ธโฃ Operators and Precedence
๐ Program: Demonstrate different operators in Python.
x = 10
y = 5
# Arithmetic Operators
print(x + y, x - y, x * y, x / y)
# Comparison Operators
print(x > y, x < y, x == y, x != y)
# Logical Operators
print(x > 5 and y < 10)
print(x < 5 or y < 10)
๐น 4๏ธโฃ Control Statements
๐ Program: Use if-else conditions.
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
๐ Program: Use loops.
for i in range(5):
print(i)
i = 0
while i < 5:
print(i)
i += 1
๐น 5๏ธโฃ Functions in Python
๐ Program: Create a function.
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
๐ Program: Function with keyword and optional parameters.
def greet(name="User"):
print(f"Hello, {name}!")
greet()
greet("Alice")
๐น 6๏ธโฃ Lists, Tuples, and Sets
๐ Program: List operations.
numbers = [1, 2, 3, 4]
numbers.append(5)
numbers.remove(2)
print(numbers)
๐ Program: Tuple operations.
tpl = (10, 20, 30)
print(tpl[0])
๐ Program: Set operations.
s = {1, 2, 3}
s.add(4)
s.remove(2)
print(s)
๐ Program: Frozen set.
fs = frozenset([1, 2, 3])
# fs.add(4) # โ Will raise an error
print(fs)
๐น 7๏ธโฃ Dictionary Operations
๐ Program: Dictionary manipulation.
student = {"name": "Alice", "age": 22, "course": "CS"}
print(student["name"])
student["age"] = 23
print(student)
๐น 8๏ธโฃ List Comprehension
๐ Program: Generate squares using list comprehension.
squares = [x**2 for x in range(1, 6)]
print(squares)
๐น 9๏ธโฃ File Handling
๐ Program: Read and write files.
with open("test.txt", "w") as file:
file.write("Hello, World!")
with open("test.txt", "r") as file:
print(file.read())
๐ Program: Check if a file exists using os
module.
import os
if os.path.exists("test.txt"):
print("File exists!")
๐น ๐ Object-Oriented Programming
โค Classes and Objects
๐ Program: Define a class and create an object.
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
print(f"Car: {self.brand} {self.model}")
car1 = Car("Toyota", "Corolla")
car1.display_info()
โค Inheritance
๐ Program: Demonstrate single inheritance.
class Animal:
def speak(self):
return "Animal speaks"
class Dog(Animal):
def speak(self):
return "Woof! Woof!"
dog = Dog()
print(dog.speak())
๐ Program: Multiple inheritance.
class A:
def method_a(self):
return "Method from A"
class B:
def method_b(self):
return "Method from B"
class C(A, B):
pass
obj = C()
print(obj.method_a(), obj.method_b())
โค Method Overloading
๐ Program: Simulating method overloading.
class Math:
def add(self, a, b, c=0):
return a + b + c
math = Math()
print(math.add(2, 3))
print(math.add(2, 3, 4))
โค Method Overriding
๐ Program: Overriding a method in a child class.
class Parent:
def show(self):
print("Parent class")
class Child(Parent):
def show(self):
print("Child class")
obj = Child()
obj.show()
โค Polymorphism
๐ Program: Using polymorphism in functions.
class Cat:
def speak(self):
return "Meow"
class Dog:
def speak(self):
return "Woof"
def animal_speak(animal):
print(animal.speak())
animal_speak(Cat())
animal_speak(Dog())
๐น 1๏ธโฃ1๏ธโฃ Exception Handling
๐ Program: Handle division by zero.
try:
num = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
๐น 1๏ธโฃ2๏ธโฃ Python Modules
๐ Program: Import and use a built-in module.
import math
print(math.sqrt(25))
๐ Program: Create a user-defined module.
1๏ธโฃ Create mymodule.py
def greet(name):
return f"Hello, {name}!"
2๏ธโฃ Use in another script
import mymodule
print(mymodule.greet("Alice"))
๐น 1๏ธโฃ3๏ธโฃ Decorators
๐ Program: Create a simple decorator.
def decorator_function(func):
def wrapper():
print("Before function execution")
func()
print("After function execution")
return wrapper
@decorator_function
def say_hello():
print("Hello, World!")
say_hello()
๐ฅ Conclusion
This list covers Python topics from basics to advanced concepts.
Would you like a real-world project using these concepts? ๐
Leave a Reply