HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Python: Print and RepeatVariablesData Types & NumbersStringsListsConditions (if / elif / else)Loops (for / while)FunctionsOperatorsDictionariesTuples & SetsError Handling (try / except)Classes (Object-Oriented)File OperationsComments & IndentationBooleans (True / False)Lambda & map / filterModules (import)InheritanceVariable Scope
REFERENCE
📖 Overview
🖥️ Output & Input
🔢 Numbers & Math
✍️ String
📋 List
🗂️ Dictionary & Set
🔁 Condition & Loop
if / elif / elseforrange()enumerate()zip()while
🧩 Function
📁 File & Error
🔄 Type & Conversion
Python›REFERENCE›Condition & Loop
🔁

Condition & Loop

Decision-making (if) and repetition (for/while) structures.

CommandDescription
if / elif / elseRuns different code based on a condition.
forIterates for each element in a sequence.
range()Generates a sequence of numbers (used with for).
enumerate()Gives both the index and the element in a loop.
zip()Iterates over two sequences in parallel.
whileLoops as long as the condition is true.

if / elif / else

Try it yourself →

Runs different code based on a condition.

Syntax

if condition:
    ...
elif condition:
    ...
else:
    ...

Description

If the if condition is true, the indented block below it runs. elif specifies other conditions, else specifies the block to run if none match. Indentation (whitespace) is required.

Example

score = 75
if score >= 85:
    print("Excellent")
elif score >= 50:
    print("Passed")
else:
    print("Failed")

Expected output

Passed

When to use?

Input validation, grade calculation, branching the flow.

Common mistakes

  • ⚠️ A colon (:) after the condition and indentation on the next line are required.
  • ⚠️ Use == for equality, not =.

for

Try it yourself →

Iterates for each element in a sequence.

Syntax

for element in arr:
    ...

Description

Goes through each element of sequences like a list, string or range, in order. Most often used with range() or a list.

Example

for letter in "abc":
    print(letter)
for i in range(3):
    print(i)

Expected output

a
b
c
0
1
2

When to use?

Processing a list, repetitive tasks, generating a table/rows.

range()

Try it yourself →

Generates a sequence of numbers (used with for).

Syntax

range(start, end, step)

Description

Generates numbers starting at 0 (or at start) up to end (excluded). With a single argument, range(n) → 0..n-1. step is the skip amount.

Example

print(list(range(5)))
print(list(range(2, 10, 2)))

Expected output

[0, 1, 2, 3, 4]
[2, 4, 6, 8]

When to use?

A set number of repetitions, looping with an index, number tables.

Common mistakes

  • ⚠️ end is not included: range(1,4) → 1,2,3.

enumerate()

Try it yourself →

Gives both the index and the element in a loop.

Syntax

for i, element in enumerate(arr):

Description

Returns the sequence number (starting from 0) along with each element. Ideal when you need 'which element it is'.

Example

for i, color in enumerate(["red", "purple"]):
    print(i, color)

Expected output

0 red
1 purple

When to use?

Printing a numbered list, processing by position.

zip()

Try it yourself →

Iterates over two sequences in parallel.

Syntax

for a, b in zip(array1, array2):

Description

Matches up multiple sequences simultaneously, giving (a, b) pairs. It stops when the shortest sequence runs out.

Example

names = ["Alex", "Linus"]
scores = [95, 88]
for name, p in zip(names, scores):
    print(name, p)

Expected output

Alex 95
Linus 88

When to use?

Processing two lists together (name+score, key+value).

while

Try it yourself →

Loops as long as the condition is true.

Syntax

while condition:
    ...

Description

Used when you don't know up front how many repetitions there'll be. Don't forget to make the condition false at some point, or it'll be an infinite loop. You exit with break.

Example

counter = 0
while counter < 3:
    print(counter)
    counter += 1

Expected output

0
1
2

When to use?

Continuing until the user says 'quit', asking until correct input is given.

Common mistakes

  • ⚠️ Forgetting the line that changes the condition causes an infinite loop.
  • ⚠️ Add advancement like counter += 1.
HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code