PY Python

Python: Variables & Data Types

What you will learn

How to store data in variables, the main built-in types, and how type conversion works. Python is dynamically typed — you never declare a type, and a variable can hold a string now and an integer later.

Variables

name = "Alice"       # string
age = 25             # integer
height = 5.6         # float
is_student = True    # boolean

Variable names: letters, digits (not first char), underscores. Case-sensitive. Convention: snake_case.

Common built-in types

Type Example Mutable? Use
int 42, -3 No Whole numbers
float 3.14 No Decimals
str "hello" No Text
bool True, False No Conditions
list [1, 2, 3] Yes Ordered collection
tuple (1, 2, 3) No Immutable sequence
dict {"key": "val"} Yes Key-value pairs
set {1, 2, 3} Yes Unique items

Type conversion

int("25")         # 25 (string to integer)
str(3.14)         # "3.14" (float to string)
float("2.5")      # 2.5
bool(0)           # False (zero is falsy)
bool("hello")     # True (non-empty is truthy)

Common mistakes

  • Forgetting that dividing two ints with / gives a float: 5/2 returns 2.5, not 2 (use // for integer division).
  • Confusing = (assignment) with == (equality check).

Quick check below!