Part of Python for Beginners

Python Tutorial for Beginners #2 - Variables & Data Types (str, int, float, bool)

Sandy LaneSandy Lane

Video: Python Tutorial for Beginners #2 - Variables & Data Types (str, int, float, bool) by Taught by Celeste AI - AI Coding Coach

Take the quiz on the full lesson page
Test what you've read · interactive walkthrough

Python Variables & Data Types

name = "Alice". Four basic types: str, int, float, bool. Python is dynamically typed — variables can hold any type and switch types at runtime.

Variables in Python are simpler than in most languages — no type declarations, no var/let, no const. You assign; Python figures out the type.

Creating variables

name = "Alice"
age = 25
height = 5.6
is_student = True

print(name)       # Alice
print(age)        # 25
print(height)     # 5.6
print(is_student) # True

Four variables, four types — set with a single =. No declaration syntax; assignment creates the variable.

The four basic types

print(type(name))         # <class 'str'>
print(type(age))          # <class 'int'>
print(type(height))       # <class 'float'>
print(type(is_student))   # <class 'bool'>

type(value) returns the class of the value. The four foundational types:

  • str — text, like "Alice" or 'hello'.
  • int — whole numbers, no fractional part.
  • float — decimal numbers, like 5.6 or 3.14.
  • boolTrue or False. (Note the capital T/F.)

There are more types (list, dict, tuple, set, None, classes) — but these four are the foundation.

Dynamic typing

x = 10
print(type(x))     # <class 'int'>

x = "hello"
print(type(x))     # <class 'str'>

x = [1, 2, 3]
print(type(x))     # <class 'list'>

The same variable x holds an int, then a string, then a list. Python doesn't care — variables are just names that point to values.

This is dynamic typing: types are checked at runtime, not at variable declaration. Compared to static typing (C, Java, Rust):

  • Pros: less typing, more flexibility.
  • Cons: type errors only show up when the bad code runs.

For larger projects, Python supports type hints (lesson 29) to add static-style checks back.

None

result = None
print(result)            # None
print(result is None)    # True
print(type(result))      # <class 'NoneType'>

None is Python's "no value" — used as a default, a sentinel, or to signal "nothing here yet." Equivalent to null in JavaScript, nil in Lua, NULL in C.

Compare with is None, not == None. Both work, but is is the idiom — checks identity, not value.

Naming rules

Valid: - name, age, is_student - student_name, studentName, MAX_VALUE - _private, __dunder__

Invalid: - 1st_place (can't start with digit) - student-name (hyphen is subtraction) - class (reserved keyword)

Conventions (PEP 8 — lesson 41):

  • snake_case for variables and functions.
  • SCREAMING_SNAKE_CASE for constants.
  • PascalCase for classes.
  • _private prefix for "internal use" (not enforced; convention).

Multiple assignment

x, y, z = 1, 2, 3
print(x, y, z)   # 1 2 3

a = b = c = 0
print(a, b, c)   # 0 0 0

Tuple unpacking on the left lets you assign multiple variables at once. Useful for swapping:

a, b = 1, 2
a, b = b, a   # swap — no temp variable needed
print(a, b)   # 2 1

Type conversion

n = 25
s = str(n)         # "25"
back = int(s)      # 25

f = float("3.14")  # 3.14
b = bool(0)         # False; bool(1) is True

Conversion functions: str(), int(), float(), bool(), list(), tuple(), etc.

int("hello") raises ValueError. Always handle bad input:

try:
    n = int(user_input)
except ValueError:
    print("Not a number")

We cover exception handling in lesson 16.

bool from any value

bool(0)        # False
bool(1)        # True
bool("")       # False (empty string)
bool("hi")     # True
bool([])       # False (empty list)
bool([1])      # True
bool(None)     # False

Python's "truthiness" rules: zero, empty containers, and None are falsy. Everything else is truthy.

This is different from C (where 0 is falsy but "" and [] don't exist as such) and different from Lua (where 0 and "" are truthy). Each language has its own conventions.

Integers are unbounded

big = 10 ** 100
print(big)
print(type(big))   # still int

Python's int has no maximum. C's int overflows at ~2 billion; Python's just keeps going. Useful for cryptography or any "big number" math.

The cost: Python ints are slower than C ints. For performance-critical numeric work, use NumPy.

Mutable vs immutable

s = "hello"
s[0] = "H"   # ERROR — strings are immutable

lst = [1, 2, 3]
lst[0] = 99  # OK — lists are mutable

Some types can be modified after creation; some can't:

  • Immutable: str, int, float, bool, tuple, frozenset, None.
  • Mutable: list, dict, set, custom objects (usually).

Immutable types are safer (can't be changed by accident); mutable are convenient (modify in place).

For strings, "modifying" means creating a new string:

s = "hello"
s = s.upper()    # s is now a new string "HELLO"

Common stumbles

True vs true. Python uses capital True/False. Lowercase is a NameError.

int("3.14"). Errors. int parses int strings only. Use float("3.14") first or int(float("3.14")).

Dynamic typing surprise. x = 5; x = "hi"; x + 1 errors at runtime. Type hints catch this earlier.

Mutable default argument. def f(x, lst=[]): — the default list is shared across calls. Use def f(x, lst=None): and check inside.

Forgetting is None for None checks. x == None works but x is None is the convention.

What's next

Lesson 3: strings. Methods (upper, split, strip), slicing, f-strings. The most-used type in Python.

Recap

Variables: name = value — no declaration. Four basic types: str, int, float, bool. Dynamic typing: variables can hold any type. type(x) to inspect. None for "no value"; check with is None. Truthiness: 0, empty containers, None are falsy. Integers are unbounded. Strings/numbers/tuples are immutable; lists/dicts/sets are mutable. Naming: snake_case for variables and functions.

Next lesson: strings.

Ready? Take the quiz on the full lesson page →
Test what you've learned. Watch the lesson and try the interactive quiz on the same page.