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.
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.
>> n = 10
>>> if n > 5: # because 10 > 5, condition true
... print('n>5')
...
>>> if n > 50: # because 10 < 50, condition false
... print('n>5')1.2 if … else
>> n = 10
>>> if n > 50: # false, go to else
... print('n>50')
... else:
... print('n<=50')
>>> n<=501.3 if … elif … else
Combining if, elif, and else allows multiple mutually‑exclusive branches.
>> 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<=102 Loop Statements
2.1 for Loop
A for loop iterates over each element of a sequence.
>> for i in [1,3,5,7]:
... print(i)
1
3
5
7 >> for i in [1,3,5,7]:
... print(i**2) # square of i
1
9
25
49 >> s = 0
>>> for i in [1,3,5,7]:
... s = s + i
... print(i, s)
(1, 1)
(3, 4)
(5, 9)
(7, 16) forcan be combined with if to filter items.
>> for i in [1,3,5,7]:
... if i > 4:
... print(i)
5
72.2 while Loop
A while loop repeats as long as its condition remains true.
>> n = 10
>>> while n > 5:
... print('n>5')The above would loop forever, so we decrement n inside the loop.
>> n = 10
>>> while n > 5:
... print('n>5')
... n = n - 1 # decrease each iteration2.3 continue
continueskips the rest of the current iteration and proceeds to the next.
>> 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 22.4 break
breakterminates the loop immediately.
>> for i in [1,2,3,4]:
... if i % 2 == 0:
... break
... print(i, 'cannot be divided by 2')
1 cannot be divided by 22.5 range
The range function generates a sequence of integers.
>> list(range(5))
[0, 1, 2, 3, 4]
>>> list(range(1,5))
[1, 2, 3, 4]Using range to sum numbers from 1 to 100:
>> s = 0
>>> for i in range(101):
... s = s + i
>>> s
50503 List Comprehensions
List comprehensions combine loops and optional conditions to build lists concisely.
>> [i**3 for i in range(1,5)]
[1, 8, 27, 64]Generating cubes of odd numbers:
>> [i**3 for i in range(1,8) if i % 2 == 1]
[1, 27, 125, 343]Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
