February 7, 2021•3 min read

Getting to Know Python's Basic Syntax

You can't write Python code any way you like. There are syntax rules you have to follow, or the program errors out.

Read in Indonesian

As we covered earlier, Python code can run straight from a terminal. So for now you'll get to know Python syntax using the terminal first.

Printing Text (Output)

You already tried printing text last time. To print text you use the print() command.

TEXT
>>> print("Hello World")
Hello World
>>> print("Belajar Python di Invasikode")
Belajar Python di Invasikode

Every character inside the parentheses gets printed, exactly as shown above.

Why the quotation marks?

In Python the quotation marks mark whatever sits between them as a word or a sentence, not a variable name. We'll talk about variables in more detail later.

Writing Comments (Comments)

Next is comments. A comment is a line of code the interpreter won't read. In other words, it doesn't get executed.

A comment starts with a hash sign (#), then whatever sentence you want to write.

TEXT
>>> # Hello World
>>> # Belajar Python di Invasikode

See, nothing comes out.

So what's it for?

Right now it looks pointless, but once you're writing Python code complicated enough, comments earn their keep. They work as reminders for the programmer about what they wrote, a kind of note. They also help while you're debugging, or when you want to experiment with a new feature.

Doing Math

The Python interpreter also works as a calculator. Type the operation you want into the terminal, and the result comes out on its own.

Example:

TEXT
>>> 12+90
102
>>> 120-60
60
>>> 190*67
12730
>>> 90/9
10.0

Python follows the basic order of arithmetic rules, listed here from the highest priority down.

  1. Operations inside parentheses
  2. Exponentiation
  3. Multiplication and division
  4. Addition and subtraction

Another example:

TEXT
>>> 5*2+1 # multiplication goes first
11
>>> 5*(2+1) # parentheses go first
15
>>> 5**2 # the ** operator means power
25

What about decimal numbers?

To write a decimal number you need a dot ('.'). Remember, not a comma (','), because that already does something else.

Example:

TEXT
>>> 1.5*2
3.0
>>> 1,5*2
(2, 10)

You can see the difference. If you use a comma, Python reads it as 2 separate numbers. That's what's called a tuple. We'll cover tuples on another occasion.

A Quick Word on Tuples: A tuple is one of Python's data types, and it lets a programmer write several values into a single variable.

Declaring Variables in Python

A variable is a big deal in programming. Variables help with manipulating data and make it easier for the programmer to arrange logic.

In Python you declare a variable by writing the variable name followed directly by its value. To get the value back out, you just call the variable's name.

TEXT
>>> a = 10 # variable a holds 10
>>> print(a)
10
>>> b = "Makan Nasi"
>>> print(b)
Makan Nasi

We'll get into variables further next time.

Using Indentation in Python

Python uses indentation (spaces) to mark a program block. Other languages, C++ among them, mark a block with curly braces.

TEXT
>>> for i in range(0, 10):
...     print(i)
0
1
2
3
4
5
6
7
8
9

That program prints the numbers from 0 to 9. The print(i) command sits inside the program block belonging to the for command.

If you don't indent that code, you get an error like this.

TEXT
>>> for i in range(0,9): 
... print(i) 
  File "<stdin>", line 2 
    print(i) 
    ^ 
IndentationError: expected an indented block

Your first error, nice, congratulations...

Indentation is a serious part of Python syntax, and getting it wrong lands you with an error like that. From here on you'll use indentation a lot once you start dealing with Python files.

What's Next

Python's syntax really isn't that complicated, so there isn't much to learn about it. That's also the reason Python counts as a humane language.

That's it for this one, hope it helps. Next time you'll dig into writing and using variables in Python 3.

See ya...

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all
Struktur DataWhat a Linked List Actually Is

A linked list is a linear chain of nodes, each holding data and a pointer to the next node. Here is what that chain looks like, what it costs next to an array, and the same minimal implementation in Java, Python, and JavaScript.

For developers who learn by struggling

© 2026 Inva.dev.