Python Intro


Written by PIRATE KING

Execution Order

In general, Python executes code sequentially from top to bottom. With functions, which you will learn in a later chapter, the flow can jump back and forth, but overall, the code still runs in top-to-bottom order. For example, let's add print("This is Python") on the next line and run the program.

As you see, the instructions execute in order from top to bottom.

Comments

Let’s go over comments in Python. Comments are parts of code that the Python interpreter ignores. They are used to add notes or explanations to help understand the program. In Python, we use hash signs # for comments.

Python supports several comment types. Let's look at some of them.

Single-Line, In-line, Multi-line

Single-line comments start with # and continue to the end of the line. Notice how the code is greened out by the code editor to indicate it is ignored. Inline comments are single-line comments placed after code on the same line; the code before # runs, while everything after is ignored. For longer explanations, multi-line comments are used by placing multiple single-line comments in a row. Let's execute this.

See? We have the same result as before; the comments have no effect on the program's execution.

Pro Tip: Use Cmd + / to quickly comment or uncomment a line, or select multiple lines and press Cmd + / to comment or uncomment them all at once. I often use Cmd + A to select the entire file content, then Cmd + / to comment everything out.

Syntax

Next, let's look at syntax errors. First, what is syntax? In programming, syntax is the set of rules that defines the correct structure and format for writing code. Each language has its own syntax, which must be followed to avoid errors and ensure the program runs correctly. For example, in Python, we put text inside quotes " " and use parentheses ( ) after the print function. These are part of Python's syntax rules.

Syntax Error

So, what is a syntax error? It happens when the code breaks these rules. For example, in this Hello World! program, the print function is missing a closing parenthesis ). What happens if we run it with this mistake? Let's try.

We get a SyntaxError because the program's syntax is incorrect. The missing closing parenthesis ) causes Python to raise an error. Adding the closing parenthesis ) fixes the issue. Now, let's try removing the closing quote " instead. What happens this time?

We get another SyntaxError but with a different message: unterminated string literal, meaning the string's closing quote " is missing. Adding the quote " fixes it again. Now, the code runs without errors.

Quiz

1. Which of the following lines is a valid Python comment?

2. What error occurs in the following code?

print("Pirate Kingdom)

3. Write a program with three print() statements to produce this output exactly:

Welcome to
the world of
Python!

4. Without deleting any lines, use comments to modify the program so it prints:

Welcome to
Python!

5. Fix the syntax error in the following code so it outputs Pirate Kingdom

© PIRATE KING 2025
GOLDSTONE STUDIO LLC
Privacy Policy