python-backend
/

Python Control Flow – Decisions & Loops

Last Sync: Today

On this page

6
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

python-backend

Python Control Flow – Decisions & Loops

Logic and Decision Making

Control flow is the order in which individual statements, instructions, or function calls are executed. Python uses standard conditional statements and loops, but with a focus on clean, readable syntax driven by indentation.

Conditional Statements (if, elif, else)

Python uses if, elif (short for else if), and else to perform different actions based on different conditions. Unlike Dart or JavaScript, parentheses around conditions are optional, and there are no curly braces.

PythonRead-only
1
score = 85

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
else:
    print("Grade: C")

Structural Pattern Matching (match)

Introduced in Python 3.10, the match statement is similar to switch in other languages but much more powerful. It can match values, types, and even the internal structure of objects.

PythonRead-only
1
status_code = 404

match status_code:
    case 200:
        print("Success")
    case 404:
        print("Not Found")
    case 500 | 501:
        print("Server Error")
    case _:
        print("Unknown Status")

Loops: for and while

Python's for loop is primarily an 'iterator'—it loops over a sequence (like a list, string, or range). The while loop continues as long as a condition remains true.

PythonRead-only
1
# Iterating over a list
langs = ["Python", "Dart", "Swift"]
for lang in langs:
    print(f"Learning {lang}")

# Using range(start, stop, step)
for i in range(0, 5):
    print(f"Iteration {i}") # 0, 1, 2, 3, 4

Loop Control: break, continue, and pass

  • break: Terminates the loop immediately.
  • continue: Skips the rest of the current iteration and moves to the next one.
  • pass: A null operation; used as a placeholder when a statement is syntactically required but you want no action taken.

Comparison Operators

OperatorDescriptionExample
==Equal to5 == 5 (True)
!=Not equal to5 != 3 (True)
and / orLogical gatesTrue and False (False)
inMembership'a' in 'apple' (True)
isIdentityx is y (True if same object)

Try it yourself

# Try modifying the range or the condition!
print("Counting even numbers:")
for num in range(1, 11):
    if num % 2 == 0:
        print(f"{num} is even")
    else:
        continue

# Simple while loop
count = 3
while count > 0:
    print(f"Countdown: {count}")
    count -= 1
print("Blast off!")

Test Your Knowledge

Q1
of 3

Which keyword is Python's version of 'else if'?

A
elseif
B
else if
C
elif
D
case
Q2
of 3

What will 'range(5)' produce?

A
0, 1, 2, 3, 4, 5
B
1, 2, 3, 4, 5
C
0, 1, 2, 3, 4
D
1, 2, 3, 4
Q3
of 3

Which statement is used to skip the current iteration of a loop?

A
break
B
pass
C
continue
D
skip

Frequently Asked Questions

Does Python have a ternary operator?

Yes, but the syntax is unique: 'value_if_true if condition else value_if_false'. Example: 'result = "Pass" if score > 50 else "Fail"'.

What is the difference between '==' and 'is'?

'==' checks for equality (are the values the same?), while 'is' checks for identity (are they the exact same object in memory?).

How do I loop with an index?

Use the enumerate() function: 'for index, value in enumerate(my_list):'.

Previous

python variables

Next

python functions

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.