Python is one of the most popular programming languages in the world. It's known for its clean syntax, readability, and versatility. Whether you want to build websites, analyze data, automate tasks, or create AI - Python can do it all.
Python uses indentation (spaces or tabs) to define code blocks, unlike other languages that use braces {}. This forces clean, readable code!
The tradition in programming is to start with "Hello, World!" - let's see how simple it is in Python:
That's it! One line of code. In other languages, this might take 5-10 lines. Python's philosophy is: "Simple is better than complex."
Write your first Python program! Use the print() function to display a message.
Write code to print "Hello, Hexworth!" to the console.
You've written your first Python program. Ready for variables?
A variable is like a labeled box where you can store information. You give the box a name, and you can put data inside it, retrieve it later, or change what's inside.
In Python, you create a variable by giving it a name and assigning a value with the = operator:
Must: Start with a letter or underscore, contain only letters, numbers, and underscores.
Cannot: Start with a number, contain spaces, or use Python keywords (like if, for, while).
Best Practice: Use descriptive names like user_age instead of x.
You can change what's stored in a variable at any time:
Practice creating and using variables. Store your information in variables and print them out!
Create three variables: my_name (your name as a string), my_age (your age as a number), and my_hobby (one of your hobbies). Then print all three.
You know how to create and use variables. Time to learn about data types!
Python can work with many different types of data. Each type has its own characteristics and operations. The main built-in types are:
| Type | Description | Example |
|---|---|---|
int |
Whole numbers (integers) | 42, -7, 0 |
float |
Decimal numbers | 3.14, -0.5, 2.0 |
str |
Text (strings) | "Hello", 'Python' |
bool |
True or False | True, False |
list |
Ordered collection | [1, 2, 3], ["a", "b"] |
dict |
Key-value pairs | {"name": "Alice"} |
Use the type() function to check what type a value is:
You can convert between types using functions like int(), float(), str():
int("42") → 42
str(100) → "100"
float(5) → 5.0
Use the type() function to explore different data types. Then practice type conversion!
1. Create a variable num_str with the value "100" (as a string)
2. Convert it to an integer using int() and store in num_int
3. Print both variables and their types
You know the core data types. Let's learn about input and output!
Programs need to communicate with users - displaying information and receiving input. Python makes this easy with print() and input().
You've already used print()! Let's explore its features:
f-strings are Python's most powerful way to format output. Put f before the quotes, then use {variable} to insert values. You can even do math: f"2+2={2+2}" prints "2+2=4"
Get input from the user. Note: input() always returns a string!
Practice using f-strings to create formatted output. Since we can't use real input() in the browser, we'll simulate it!
Create a greeting message using f-strings. Given name = "Wizard" and level = 7, print: "Welcome, Wizard! You are level 7."
You can now communicate with users. One more topic - comments!
Comments are notes in your code that Python ignores. They're for you and other programmers to understand what the code does. Good comments = good code!
Do: Explain complex logic, document assumptions, note TODOs.
Don't: State the obvious, write novels, let comments get outdated.
Remember: Code tells you HOW, comments tell you WHY.
Comments are also useful for temporarily disabling code while debugging:
Practice adding meaningful comments to code. Remember: explain WHY, not WHAT!
Add a comment explaining what this code does and WHY the number 3 is used:
You know how to document your code. Time for the summary!
Congratulations! You've completed Chapter 1 and learned the fundamentals of Python programming. Let's recap what you've mastered:
Use descriptive names. Think of them as labeled boxes.
Know your types. Use type() and conversion functions.
f"text {variable}" is your best friend for output.
Explain WHY, not WHAT. Future you will thank you.
You've mastered the fundamentals. Ready to prove your skills?