← Back to list

Data Science Day 6 Control Flow

Python Control Flow

Tharun B S · 2026-04-14 11:01 · 0 claps · 3.5 min read
#python #data-science #python-control-flow #loops-in-python #conditions-in-python
Open on Medium ↗
Wiki topics: ML · Machine Learning 🔬 · Science · General

Data Science Day 6 Control Flow

Python Control Flow

Photo by Simon Hurry on Unsplash

Photo by Simon Hurry on Unsplash

Control flow in Python dictates the order in which a program’s instructions are executed. It allows for dynamic program behavior based on conditions and the ability to repeat actions. Python primarily utilizes two types of control flow structures:

Conditional statements (if, elif, else)

The if-elif-else statement in Python is used for conditional execution, allowing different blocks of code to run based on whether specific conditions are true or false.

if condition1:
# Code to execute if condition1 is True
elif condition2:
# Code to execute if condition1 is False and condition2 is True
elif condition3:
# Code to execute if condition1 and condition2 are False, and condition3 is True
else:
# Code to execute if all preceding conditions are False

Explanation: if condition1:

The conditional block starts with the if statement.

A boolean expression called condition1 evaluates to either True or False.

The indented code block that comes right after condition 1 will be run if it is True.

Python checks the following elif or else block if condition1 is False.

(Optional, may be multiple) elif condition2:

“else if” is shortened to “elif.” If the previous if or elif conditions were False, it enables you to look for more conditions.

Another boolean expression is condition2.

The indented code block that comes after elif condition2 will run if condition1 was False and condition2 is True.

Multiple elif blocks can be used to sequentially check for different conditions.

else: (Optional): If none of the if or elif conditions that came before it were true, the else statement offers a default block of code to run.

There is no need for a condition.Only when all of the preceding conditions are evaluated to False will the indented code block that follows else be executed.

Key Points:

Indentation is crucial:

Python uses indentation to define code blocks within if, elif, and else statements.

Conditions are evaluated sequentially:

Python checks conditions from top to bottom. As soon as a True condition is found, its corresponding code block is executed, and the rest of the elif and else blocks are skipped.

else is optional:

You can have an if statement with or without elif and else. An if statement can also be combined with just an else statement (without elif).

Loops (for loop, while loop)

Python provides two primary looping constructs: for loops and while loops. These are used to execute a block of code repeatedly.

1. For loops: for loops are used to iterate over an iterable object or sequence (such as a list, tuple,

string, or range). They are usually employed in situations where the number of iterations is known or can be inferred from the sequence’s length.

for item in sequence:
# code to be executed for each item
  fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
  print(fruit)
# Output:
# apple
# banana
# cherry

while loops: while loops are used to repeatedly execute a block of code as long as a certain condition remains true. They are suitable when the number of iterations is unknown beforehand and depends on a dynamic condition.

while condition:
# code to be executed as long as the condition is True
  count = 0
  while count < 5:
    print(count)
count += 1
# Output:
# 0
# 1
# 2
# 3
# 4

Important Distinctions:

Iteration Control: While while loops continue in response to a condition, for loops iterate over a predetermined sequence.

Known vs. Unknown Iterations: While loops are used when the number of iterations is unknown and contingent on a condition, for loops are typically used when the number of iterations is known.

Initialization and Increment: For loops automatically manage iteration and element access. To guarantee that the condition eventually turns false and the loop ends, it is frequently necessary to manually initialize a counter variable in while loops and increment/decrement it throughout the loop.

break and continue statements.

Python loops (for and while) can have their normal execution changed by using the control flow statements break and continue.

break Statement: The break statement ends the loop it is a part of right away. The program jumps to the statement that comes right after the loop when a break occurs.

for i in range(5):
  if i == 3:
    break
  print(i)
print("Loop finished.")
Output:
0
1
2
Loop finished.

In this example, when i becomes 3, the break statement is executed,

terminating the for loop. The print(i) statement for i=3 and i=4 is skipped, and “Loop finished.” is printed.

Continue

The continue statement moves on to the following iteration of the loop by omitting the remainder of the current one. It doesn’t completely break the loop.

for i in range(5):
  if i == 2:
    continue
  print(i)
print("Loop finished.")
Output:
0
1
3
4
Loop finished.

Here, when i is 2, the continue statement is executed. This skips the print(i) statement for that specific iteration, and the loop proceeds to i=3. The print(i) statements for i=0, 1, 3, 4 are executed.

Key Differences:

· break: Exits the entire loop.

· continue: Skips the current iteration and proceeds to the next iteration of the same loop.

· pass: A null operation; it does nothing. It can be used as a placeholder where a statement is syntactically required but no action is desired.


메타데이터
post_id
d70d1167ca8f
slug
data-science-day-6-control-flow-d70d1167ca8f
url
https://medium.com/@tharunbs352/data-science-day-6-control-flow-d70d1167ca8f
canonical_url
https://medium.com/@tharunbs352/data-science-day-6-control-flow-d70d1167ca8f
author_url
https://medium.com/@tharunbs352
status
ok
fetched_at
2026-08-03 23:05:12