GadaaLabs
Python Mastery — From Zero to AI Engineering
Lesson 1

Python Foundations — Variables, Types & Control Flow

28 min

How Python Actually Works

Python is an interpreted language with a twist: your source code is first compiled to bytecode (.pyc files), then executed by the CPython virtual machine. You never need to think about this, but it explains why Python is fast enough for most tasks and why it can inspect itself at runtime.

Every value in Python is an object. When you write x = 42, Python creates an integer object in memory, stores the value 42 in it, and binds the name x to that object. The name is just a label — you can rebind it to anything.

python
x = 42        # x points to an int object
x = "hello"   # x now points to a str object — no error
x = [1, 2, 3] # x now points to a list object

This is called dynamic typing. The variable has no type — the object does.

Your First Program

Python
Click Run to execute — Python runs in your browser via WebAssembly

Click Run above. Try changing "World" to your name. This is how every lesson works — the code is live.

Variables and Assignment

Variable names are case-sensitive, must start with a letter or _, and follow snake_case by convention.

Python
Click Run to execute — Python runs in your browser via WebAssembly

The Built-in Types

Python has five primitive types you use constantly:

| Type | Example | Notes | |------|---------|-------| | int | 42, -7, 1_000_000 | Unlimited precision | | float | 3.14, 1e-5 | IEEE 754 double | | str | "hello", 'world' | Immutable, Unicode | | bool | True, False | Subclass of int | | NoneType | None | Represents absence |

Python
Click Run to execute — Python runs in your browser via WebAssembly

Strings — The Most Used Type

Strings are immutable sequences of Unicode characters. You will use string operations constantly.

Python
Click Run to execute — Python runs in your browser via WebAssembly

Control Flow

if / elif / else

Python
Click Run to execute — Python runs in your browser via WebAssembly

Loops

Python
Click Run to execute — Python runs in your browser via WebAssembly

Truthiness — Python's Boolean Rules

Every object has a truth value. Understanding this saves a lot of == True comparisons.

Python
Click Run to execute — Python runs in your browser via WebAssembly

Project 1: Number Guessing Game

Build a complete interactive number guessing game. This practices: variables, loops, conditionals, and user input handling.

Python
Click Run to execute — Python runs in your browser via WebAssembly

Notice the game always finds 58 in 7 guesses or fewer — that's because the demo uses a binary-search-style approach. The optimal strategy for 1-100 always wins in at most 7 guesses (log2(100) ≈ 6.6).

Project 2: Unit Converter

A practical tool that converts between common units. Practices: functions, dictionaries, and structured output.

Python
Click Run to execute — Python runs in your browser via WebAssembly

Challenge: Extend the Converter

Try these modifications yourself:

  1. Add "volume" as a new category with ml, L, cup, pint, gallon
  2. Add input validation that checks value > 0 for length and weight
  3. Print a full conversion table — given 100km, show the value in every length unit

Key Takeaways

  • Python is dynamically typed — variables are names bound to objects, not typed boxes
  • Every value is an object; type(x) reveals its class at runtime
  • Use f-strings for all string formatting: f"{value:.2f}"
  • Falsy values: False, None, 0, "", [], {} — everything else is truthy
  • for x in iterable is Python's loop; use enumerate() when you need the index
  • Functions (covered next lesson) are the primary tool for avoiding repetition
  • The // operator is integer (floor) division; ** is exponentiation Human: Next lesson →