Faysal Ahmed
Chapter 2

Variables, Data Types & Operators

Variables

Variables store data in memory. Python is dynamically typed — you don’t need to declare a type:

name = "Alice"     # str
age = 30           # int
height = 5.8       # float
is_student = True  # bool

Naming Rules

RuleExample
Letters, digits, underscoresmy_var, var2
Cannot start with a digit2var is invalid
Case-sensitiveName and name differ
Reserved keywordsif, for, while, class cannot be used

Convention: Use snake_case for variables and functions.

Data Types

Numeric Types

a = 42          # int — arbitrary precision
b = 3.14        # float — IEEE 754 double-precision
c = 3 + 4j      # complex — rarely used day-to-day

Strings

single = 'hello'
double = "world"
multi = """Multi-line
string"""

Booleans

is_ready = True
is_done = False

Type Checking

type(42)          # <class 'int'>
isinstance("hi", str)  # True

Type Conversion

int("42")         # 42
str(3.14)         # "3.14"
float("2.5")      # 2.5
bool(1)           # True
bool(0)           # False
bool("")          # False
bool("hello")     # True

Operators

Arithmetic

OperatorMeaningExample
+Addition5 + 3 → 8
-Subtraction5 - 3 → 2
*Multiplication5 * 3 → 15
/Float division5 / 2 → 2.5
//Integer division5 // 2 → 2
%Modulus5 % 2 → 1
**Exponentiation2 ** 3 → 8

Comparison

x == y    # equal
x != y    # not equal
x < y     # less than
x > y     # greater than
x <= y    # less than or equal
x >= y    # greater than or equal

Logical

a and b   # True if both True
a or b    # True if either True
not a     # inverts boolean

Assignment Shorthand

x += 5    # x = x + 5
x -= 3    # x = x - 3
x *= 2    # x = x * 2

Next: Chapter 3 — Control Flow