Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Python: The All-Purpose Kitchen

Welcome to the Python language guide! If programming languages were cooking styles, Python would be the versatile, all-purpose kitchen that can handle everything from a quick weekday meal to an elaborate multi-course feast.

What is Python?

Python is a high-level, general-purpose programming language known for its simple, readable syntax. Its design philosophy emphasizes code readability, which is why its syntax looks clean and is often compared to plain English. This makes it one of the most recommended languages for beginners.

What is it Used For?

Python is incredibly versatile and is a top choice in several major fields:

  • Data Science & AI: Analyzing data, training machine learning models, and building neural networks.
  • Web Development: Creating the backend logic for websites and web applications.
  • Automation & Scripting: Writing small programs to automate repetitive tasks, like organizing files or scraping data from websites.
  • Software Development: Building desktop applications and developer tools.

Why You Might Like Python

  • Beginner-Friendly: The simple syntax means you can focus on learning programming concepts rather than getting stuck on complex rules.
  • Huge "Pantry" of Libraries: Python has a massive ecosystem of pre-built code (libraries) that you can import, saving you from having to build everything from scratch.
  • Massive Community: A large and active community means that if you ever get stuck, an answer is likely just a quick search away.
  • Highly in Demand: It is one of the most popular and widely used programming languages in the world.

Keep in Mind

Because Python is dynamically typed, some errors might not be caught until your program is running, which requires diligent testing.

A Taste of Python Syntax

Here’s a taste of what Python code looks like. As you can see, it's clean and straight to the point.

# === Python: A Day at The Coder's Cafe ===

# --- Module 1: Greeting the Customer (The Basics) ---
print("Welcome to The Coder's Cafe!")
# Taking an order is like getting user input.
customer_name = "Ada"  # In a real app: input("May I have your name? ")
# This is a note for the chef (a comment)

# --- Module 2: Prepping the Ingredients (Data) ---
dish_name = "Pixel Perfect Pizza"    # String
quantity = 2                         # Integer
price_per_dish = 15.50               # Float
is_order_ready = False               # Boolean
order_summary = f"{quantity}x {dish_name}" # String Formatting

# --- Module 3: In the Kitchen (Logic & Flow) ---
if "Pizza" in dish_name:
    print(f"Cooking {order_summary} in the brick oven.")
else:
    print(f"Cooking {order_summary} on the stove.")

# --- Module 4: Handling the Full Order (Collections & Loops) ---
# A customer's complete order (List)
customer_order_list = ["Pixel Perfect Pizza", "Data-driven Drink"]
print("Processing full order:")
for item in customer_order_list:
    print(f"- Adding {item} to the ticket.")

# A process that repeats until a condition is met (While Loop)
soup_temp = 80
while soup_temp < 100:
    print(f"Heating soup... now at {soup_temp}°C")
    soup_temp += 10 # Increase temperature by 10
print("Soup is ready!")

# --- Module 5: The Final Bill & A Special Offer (Functions & Imports) ---
import random # For our special promotion

# A standard procedure (Function)
def calculate_bill(customer, items, total_price):
    print(f"\n--- Bill for {customer} ---")
    for item in items:
        print(f"  - {item}")

    # Let's add a random promotional discount!
    discount = random.randint(5, 20) # 5% to 20% off
    print(f"Applying a special {discount}% discount!")
    final_price = total_price * (1 - discount / 100)
    return final_price # Return the calculated value

# A bill represented as a Dictionary (Key-Value pairs)
order_bill = {
    "customer": customer_name,
    "items": customer_order_list,
    "total": price_per_dish * quantity
}

# Call the function to get the final result
final_amount = calculate_bill(order_bill["customer"], order_bill["items"], order_bill["total"])

print(f"Your final bill is ${final_amount:.2f}.")
print(f"Thank you for dining with us, {customer_name}!")

Start Coding in Python

Here are the simplest ways to start, from the easiest method to the most common one.

1. In Your Browser (The Easiest Start)

This method requires no installation. You just write and run your code on a website.

  • What to use: Google Colab
  • How it works: It’s a free "notebook" that runs in the cloud. You can write Python code, add notes, and run it all from your browser.
  • Best for: Learning Python, data analysis, and AI projects.
  • Modern alternative: molab if you want to use marimo

2. On Your Computer (The Standard Way)

This is how most developers work. You must install Python on your computer first.

The Python Shell (For Quick Tests)

REPL (Read-Eval-Print Loop)

  • How to use:
    1. Open your computer's "Terminal" or "Command Prompt".
    2. Type python (or python3) and press Enter.
  • Best for: Testing a single line of code or doing quick math.

Running Script Files (The Main Way)

This is the most common way to build a program.

  • How to use:
    1. Write your code in a text editor (like VS Code, Sublime Text, or even Notepad).
    2. Save the file with a .py ending (e.g., my_script.py).
    3. In your terminal, navigate to the file's folder and type python my_script.py to run it.
  • Best for: Building any kind of app, script, or project.

Using an IDE for a Better Workflow

An "IDE" (Integrated Development Environment) is a powerful editor with extra features that help you write better code, faster.

  • What to use: VS Code (with the Python extension), Zed or PyCharm.
  • How it works: These tools check your code for errors, auto-complete what you type, and help you manage all the files in a large project.
  • Essential Tooling (Linters & Formatters): To keep your code clean and consistent, developers use tools like Ruff and Black. These tools automatically format your code and check for common errors. Most IDEs can integrate them to format your code every time you save.

Managing Project Dependencies with uv

This is a crucial step for building projects that rely on external code.

  • What is a dependency? Most Python projects use "libraries" or "packages," which are collections of pre-written code that solve common problems. You can find these on the Python Package Index (PyPI), a giant online repository of Python software.
  • What tool to use: uv
  • How it works: uv is a very fast tool that creates isolated "virtual environments" for each project. This means Project A can use one version of a library, and Project B can use a different version without them conflicting. You use uv to install, remove, and manage these libraries from PyPI.
  • Examples of Key Libraries you can install: