Fundamentals 7 min read

Master Python Control Flow: If, Loops, and List Comprehensions Explained

Learn Python's fundamental control flow constructs—including if, if‑else, elif‑else, for and while loops, continue, break, range, and list comprehensions—through clear explanations and interactive code examples that demonstrate condition evaluation, iteration, and common patterns for managing program logic.

Model Perspective
Model Perspective
Model Perspective
Master Python Control Flow: If, Loops, and List Comprehensions Explained

1 Conditional Statements (if)

1.1 Simple if

An if statement evaluates a condition; if the condition is true, the following block runs, otherwise it is skipped.

<code>>> n = 10
>>> if n > 5:  # because 10 > 5, condition true
...     print('n>5')
... 
>>> if n > 50:  # because 10 < 50, condition false
...     print('n>5')
</code>

1.2 if … else

<code>>> n = 10
>>> if n > 50:  # false, go to else
...     print('n>50')
... else:
...     print('n<=50')
>>> n<=50
</code>

1.3 if … elif … else

Combining if , elif , and else allows multiple mutually‑exclusive branches.

<code>>> n = 10
>>> if n > 50:
...     print('n>50')
>>> elif n > 20:
...     print('20<n<=50')
>>> elif n > 10:
...     print('10<n<=20')
>>> else:
...     print('n<=10')
>>> n<=10
</code>

2 Loop Statements

2.1 for Loop

A for loop iterates over each element of a sequence.

<code>>> for i in [1,3,5,7]:
...     print(i)
1
3
5
7
</code>
<code>>> for i in [1,3,5,7]:
...     print(i**2)  # square of i
1
9
25
49
</code>
<code>>> s = 0
>>> for i in [1,3,5,7]:
...     s = s + i
...     print(i, s)
(1, 1)
(3, 4)
(5, 9)
(7, 16)
</code>

for can be combined with if to filter items.

<code>>> for i in [1,3,5,7]:
...     if i > 4:
...         print(i)
5
7
</code>

2.2 while Loop

A while loop repeats as long as its condition remains true.

<code>>> n = 10
>>> while n > 5:
...     print('n>5')
</code>

The above would loop forever, so we decrement n inside the loop.

<code>>> n = 10
>>> while n > 5:
...     print('n>5')
...     n = n - 1  # decrease each iteration
</code>

2.3 continue

continue skips the rest of the current iteration and proceeds to the next.

<code>>> for i in [1,2,3,4]:
...     if i % 2 == 0:
...         continue
...     print(i, 'cannot be divided by 2')
1 cannot be divided by 2
3 cannot be divided by 2
</code>

2.4 break

break terminates the loop immediately.

<code>>> for i in [1,2,3,4]:
...     if i % 2 == 0:
...         break
...     print(i, 'cannot be divided by 2')
1 cannot be divided by 2
</code>

2.5 range

The range function generates a sequence of integers.

<code>>> list(range(5))
[0, 1, 2, 3, 4]
>>> list(range(1,5))
[1, 2, 3, 4]
</code>

Using range to sum numbers from 1 to 100:

<code>>> s = 0
>>> for i in range(101):
...     s = s + i
>>> s
5050
</code>

3 List Comprehensions

List comprehensions combine loops and optional conditions to build lists concisely.

<code>>> [i**3 for i in range(1,5)]
[1, 8, 27, 64]
</code>

Generating cubes of odd numbers:

<code>>> [i**3 for i in range(1,8) if i % 2 == 1]
[1, 27, 125, 343]
</code>
Pythonloopslist comprehensioncontrol flowif statement
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.