Topic 02 / 15

Variables, Types & Numbers

~8 min read  //  Python Series  //  Coding India

Variables Are Labels, Not Boxes

In Python you don’t declare variables — you assign, and the name starts existing. A variable is a label pointing at an object:

students = 10500          # int
price = 199.0             # float
channel = "Coding India"  # str
is_live = True            # bool
winner = None             # None — "no value yet"

Check any value’s type with type():

type(students)   # <class 'int'>
type(channel)    # <class 'str'>

Numbers

7 + 3      # 10
7 - 3      # 4
7 * 3      # 21
7 / 3      # 2.3333... — / ALWAYS returns float
7 // 3     # 2  — floor division
7 % 3      # 1  — remainder (modulo)
7 ** 3     # 343 — exponent

Python ints have unlimited precision2 ** 1000 just works, no overflow. Floats are standard 64-bit IEEE doubles, with the usual caveat:

0.1 + 0.2 == 0.3        # False! (0.30000000000000004)
import math
math.isclose(0.1 + 0.2, 0.3)   # True — compare floats this way

For money, use decimal.Decimal, never float.

Converting Between Types

int("42")        # 42
float("3.14")    # 3.14
str(199)         # "199"
int(3.99)        # 3 — truncates, doesn't round
round(3.99)      # 4

Conversions that don’t make sense raise an error — int("ten") is a ValueError, not a silent NaN. Python fails loudly, which is a feature.

Naming Conventions

  • snake_case for variables and functions: total_price, get_user
  • UPPER_CASE for constants: MAX_RETRIES = 3
  • PascalCase for classes: VideoPlayer

These come from PEP 8, Python’s official style guide. Following it makes your code instantly readable to every Python developer.

Multiple Assignment & Swapping

x, y, z = 1, 2, 3
x, y = y, x          # swap without a temp variable
count = total = 0    # both names point at 0