Python Exam Notes
Python Exam Notes
Numeric Types
Example:
x = int(3.8) # x becomes 3
Operators
Arithmetic Operators
Logical Operators
Comparison Operators
3. Control Flow
Conditional Statements
x = 10
if x > 5:
print("Greater")
elif x == 5:
print("Equal")
else:
print("Smaller")
Loops
For Loop (Used to iterate over sequences like lists, tuples, etc.)
for i in range(3):
print(i) # Outputs: 0, 1, 2
x=5
while x > 0:
print(x)
x -= 1
Defining Functions
def greet(name):
Variable-Length Arguments:
def add(*args):
return sum(args)
Exception Handling
Prevents program crashes by handling errors.
try:
x=5/0
except ZeroDivisionError:
finally:
print("Execution finished")
class Car:
self.brand = brand
car1 = Car("Toyota")
print(car1.brand) # Toyota
class Dog:
self.name = name
def bark(self):
print("Woof!" )
Encapsulation
Inheritance allows a class (child class) to acquire properties and methods of another class (parent
class).
class Vehicle:
self.brand = brand
class Car(Vehicle):
pass
class A:
pass
class B:
pass
pass
class Grandparent:
pass
class Parent(Grandparent):
pass
class Child(Parent):
pass
Conclusion
This document covers Python fundamentals up to Inheritance. Understanding these topics will
help you prepare effectively for your exam. Let me know if you need further explanations or
additional practice questions!