Python Exam Guide
2. Basic Data Types and Operators
Numeric Types
int: Integer numbers (e.g., 10, -5)
float: Decimal numbers (e.g., 10.5, -3.14)
boolean: True or False (subtype of int where True=1, False=0)
complex: Numbers with real and imaginary parts (e.g., 3+2j)
Type Casting and Conversion
Convert between types using int(), float(), str(), etc.
Example:
x = int(3.8) # x becomes 3
y = float("5.6") # y becomes 5.6
Operators
Arithmetic Operators
+ (addition), - (subtraction), * (multiplication), / (division)
// (floor division), % (modulus), ** (exponentiation)
Logical Operators
and (both conditions must be true)
or (at least one condition must be true)
not (negates the condition)
Comparison Operators
==, !=, >, <, >=, <=
3. Control Flow
Conditional Statements
Used to execute different blocks of code based on conditions.
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
While Loop (Repeats as long as a condition is true)
x=5
while x > 0:
print(x)
x -= 1
List Comprehension (Shorter way to create lists)
squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]
4. Functions, Modules, and Exception Handling
Defining Functions
def greet(name):
return "Hello, " + name
Function Parameters and Arguments
Positional Arguments: Must be passed in order.
Keyword Arguments: Passed with parameter names.
Default Arguments: Have default values if not provided.
Variable-Length Arguments:
def add(*args):
return sum(args)
Scope: Local vs Global Variables
Local Variable: Defined inside a function.
Global Variable: Defined outside functions and can be accessed anywhere.
global keyword can be used to modify a global variable inside a function.
Exception Handling
Prevents program crashes by handling errors.
try:
x=5/0
except ZeroDivisionError:
print("Cannot divide by zero!")
finally:
print("Execution finished")
5. Object-Oriented Programming (OOP) Basics
Classes and Objects
A class is a blueprint for creating objects.
An object is an instance of a class.
class Car:
def __init__(self, brand):
self.brand = brand
car1 = Car("Toyota")
print(car1.brand) # Toyota
Attributes and Methods
Attributes are variables that store object data.
Methods are functions defined inside a class.
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print("Woof!" )
Encapsulation
Restricting direct access to an object's data.
Public Attribute: Accessible from anywhere (self.name)
Protected Attribute: Indicated with _ (self._name)
Private Attribute: Indicated with __ (self.__name)
Inheritance
Inheritance allows a class (child class) to acquire properties and methods of another class (parent
class).
Single Inheritance (Child inherits from one parent)
class Vehicle:
def __init__(self, brand):
self.brand = brand
class Car(Vehicle):
pass
Multiple Inheritance (Child inherits from multiple parents)
class A:
pass
class B:
pass
class C(A, B):
pass
Multilevel Inheritance (Child → Parent → Grandparent)
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!