Python Theory With Examples PPT
Python Theory With Examples PPT
(College Level)
Full PPT with Concepts and Code
Examples
Introduction to Python
• Python is an interpreted, high-level language.
• Easy to learn and widely used.
• Example:
• print("Welcome to Python!")
Variables and Data Types
• Variables store data. Python types include int,
float, str, and bool.
• Example:
• age = 20
• price = 99.99
• name = "Alice"
• is_student = True
Operators
• Python supports arithmetic, comparison,
logical, and assignment operators.
• Example:
• a = 10
• b=3
• print(a + b)
• print(a > b)
• a += 2
Control Statements
• if, elif, and else help control flow based on
conditions.
• Example:
• marks = 75
• if marks >= 90:
• print("Grade A")
• elif marks >= 60:
• print("Grade B")
Loops
• Loops allow repetition using 'for' and 'while'.
• Example:
• for i in range(1, 6):
• print(i)
• i=1
• while i <= 5:
• print(i)
• i += 1
Functions
• Functions are reusable blocks of code.
• Example:
• def add(x, y):
• return x + y
• print(add(3, 5))
Data Structures
• Python has built-in types: List, Tuple, Set,
Dictionary.
• Example:
• fruits = ["apple", "banana"]
• colors = ("red", "blue")
• nums = {1, 2, 3}
• info = {"name": "John"}
Strings and Methods
• Strings support many built-in methods.
• Example:
• text = "hello"
• print(text.upper())
• print(text.replace("h", "H"))
File Handling
• Open files to read or write.
• Example:
• f = open("test.txt", "w")
• f.write("Hello")
• f.close()
• f = open("test.txt", "r")
• print(f.read())
OOP in Python
• Classes define objects. Use __init__ to
initialize.
• Example:
• class Student:
• def __init__(self, name):
• self.name = name
• s = Student("Alice")
Modules and Packages
• Modules are files with functions. Use 'import'
to use them.
• Example:
• import math
• print(math.sqrt(16))
Exception Handling
• Use try-except to catch errors.
• Example:
• try:
• x=1/0
• except ZeroDivisionError:
• print("Error")
Sample Programs
• Swap:
• a, b = 5, 10
• a, b = b, a
• Even/Odd:
• num = 4
• if num % 2 == 0:
• print("Even")
• else: