CrackKit
0% complete
Python: Zero to Hero

Variables & the core types

A variable is a name pointing at a value. Python figures out the type from the value itself — you never declare int x. This lesson covers the handful of built-in types that everything else is built from.

Assignment: a name, not a box

python
score = 42          # name `score` now points at the integer 42
name = "Ada"        # `name` points at a string
score = score + 8   # rebind `score` to a new value, 50

Think of = as "make this name refer to this value," not "put this value in this box." Names are labels you can freely re-stick onto new values. A name can be re-pointed at a different type at any time (Python is dynamically typed) — legal, but usually a smell.

The core built-in types

int42, -7, 1000000 float3.14, -0.5, 2.0 str"hello", 'a' boolTrue, False NoneTypeNone check any value's type with: type(x) → <class 'int'> None = "no value here" — Python's null, used everywhere
  • int — whole numbers, unlimited size (Python won't overflow: 2 ** 1000 just works).
  • float — decimals. Beware: 0.1 + 0.2 gives 0.30000000000000004 (binary floating point — a universal gotcha, not a Python bug).
  • str — text in single or double quotes. Immutable (can't change in place).
  • boolTrue / False. Capitalized. Secretly ints: True == 1.
  • None — the "nothing" value. A function with no return gives back None.

Checking and converting types

python
type(42)          # <class 'int'>
type(3.14)        # <class 'float'>

int("100")        # 100   — string to int
str(100)          # "100" — int to string
float("3.5")      # 3.5
int(3.9)          # 3     — truncates toward zero, does NOT round
bool("")          # False — empty string is "falsy" (more in section 2)

Converting is explicit — Python won't silently turn "5" into 5 for you. "5" + 3 raises a TypeError, not 8. This strictness catches real bugs.

Naming rules and conventions

  • Must start with a letter or _, then letters/digits/underscores. Case-sensitive.
  • Convention: snake_case for variables and functions, UPPER_CASE for constants.
  • Names should say what the value means: user_count, not n or x2.

A TypeError means you mixed incompatible types — the single most common beginner error. Read it as "you tried an operation the types don't support," then check what each side actually is with type().