An introduction to the transition from visual block coding to writing actual Python code.
What if you could move from building with pre-made LEGO sets to 3D printing any shape you can imagine? Moving from blocks to Python is exactly like that—you're about to unlock the power to build anything.
In block-based languages like Scratch, you are protected from errors because blocks only snap together if they 'fit.' In text-based programming, you type every character yourself. This gives you much more power and flexibility, but it requires more precision. Think of blocks as a guided tour and Python as an open-world adventure. In Python, we use Syntax, which is the specific set of rules that defines how a program must be written to be understood by the computer. If you forget a single quote mark or parenthesis, the computer won't know what to do!
Quick Check
What is the term for the set of rules that determines how code must be written to be understood?
Answer
Syntax
In English, we use periods and capital letters to show where sentences start and end. In Python, we use indentation (the space at the beginning of a line) to group code together. While other languages use curly braces {}, Python uses whitespace. This makes Python code very clean and easy to read. Usually, one 'level' of indentation is equal to 4 spaces or one press of the Tab key. If your alignment is off by even one space, Python will throw an IndentationError.
To show text on the screen, we use the `print()` function. 1. Type the word `print` in lowercase. 2. Add a set of parentheses `()`. 3. Inside the parentheses, put your message in quotation marks: `print("Hello, Python!")`.
Quick Check
True or False: In Python, indentation is just for making the code look pretty and doesn't affect how it runs.
Answer
False
Python is also a powerful calculator. You can print the results of mathematical expressions directly. When printing text (called a string), you must use quotes. When printing numbers or math, you do not use quotes. For example, `print(5 + 5)` will display , but `print("5 + 5")` will display the literal text "5 + 5". Python follows the standard order of operations, so will result in .
You can print multiple things at once by separating them with a comma. 1. `print("The answer is", 10 * 2)` 2. This will output: `The answer is 20`. 3. Note how Python automatically adds a space between the text and the number!
Try to predict the output of this complex print statement: 1. `print("Result:", (10 + 2) / 3)` 2. First, Python solves the parentheses: . 3. Then, it divides: . 4. The final output is: `Result: 4.0`.
Which of the following is a valid Python print statement?
What will be the output of `print(10 / 2 + 3)`?
Python requires 4 spaces of indentation to group code, and failing to do so causes an error.
Review Tomorrow
In 24 hours, try to explain to a friend why Python uses spaces (indentation) instead of curly braces like other languages.
Practice Activity
Open a Python editor and write a program that prints your name on the first line and your favorite math equation's result on the second line.