Python Programs

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

Your email address will not be published. Required fields are marked *