This episode dissects Python's core syntax: built-in data types, operators, control flow, functions, and comprehensions. You'll also learn naming best practices, modularization, and how to write docstrings correctly according to convention.

After understanding Python's architecture in episode 2, now it's time to write real code. Episode 3 builds the syntax foundation you'll use throughout the series: built-in data types, operators, control flow, functions, and comprehensions.
Python provides built-in data types that cover almost every need:
You can check a data type with the type function:
angka = 42
pecahan = 3.14
teks = "Python"
daftar = [1, 2, 3]
pasangan = {"kunci": "nilai"}
print(type(angka))
print(type(teks))
print(type(daftar))
print(type(pasangan))The call type(angka) returns the type of each value.
Python supports a complete set of arithmetic operators, including a few that set it apart from other languages:
a = 17
b = 5
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)The output of a // b is floor division, a % b is the remainder, and a ** b is exponentiation. Note that / always returns a float, while // returns an integer.
Control flow governs the order of execution. Branching uses if, elif, and else:
nilai = 85
if nilai >= 90:
predikat = "A"
elif nilai >= 80:
predikat = "B"
else:
predikat = "C"
print(predikat)The block if nilai >= 90: checks conditions in sequence. Python uses indentation to define code blocks, not curly braces like C. Consistent indentation is very important.
Loops use for to iterate collections and while for conditions:
for i in range(3):
print(i)
total = 0
while total < 5:
total += 1
print(total)for i in range(3) iterates the numbers 0 through 2. range is evaluated lazily, so it's memory-efficient.
Functions wrap reusable logic. The syntax uses the def keyword:
def sapa(nama, sapaan="Halo"):
return f"{sapaan}, {nama}!"
print(sapa("Arman"))
print(sapa("Arman", sapaan="Selamat pagi"))The function sapa takes the required nama and sapaan with a default value. Calling sapa("Arman", sapaan="Selamat pagi") uses keyword arguments, which makes the code clearer at the call site.
Every public function should be documented with a docstring — a string literal placed right below the function definition:
def luas_persegi(sisi):
"""Menghitung luas persegi.
Argumen:
sisi (float): panjang sisi persegi.
Returns:
float: luas persegi.
"""
return sisi * sisi
print(luas_persegi(4))The docstring """Menghitung luas persegi...""" can be read via luas_persegi.__doc__ or tools like help(). This is part of documentation that we'll explore more deeply in episode 19.
Comprehensions are a concise way to build collections from an iterable:
angka = [1, 2, 3, 4, 5]
kuadrat = [n * n for n in angka]
genap = [n for n in angka if n % 2 == 0]
print(kuadrat)
print(genap)[n for n in angka if n % 2 == 0] produces a new list containing the even numbers. Comprehensions read like natural language and are more concise than manual loops. The same pattern applies to dict and set — {n: n * n for n in angka} builds a dict, while {n for n in range(10) if n % 2 == 0} builds a set.
Python has naming conventions governed by PEP 8:
hitung_total.PenggunaService.MAKS_RETRY.Key takeaways:
In the next episode, episode 4, we'll cover advanced functions and functional tools — positional and keyword arguments, *args and **kwargs, the mutable default trap, higher-order functions, lambda, functools, and iterator utilities. Your syntax foundation will be put straight to the test with deeper patterns!