02 - Control Flow - Conditional Block and Loops
I am an aspiring DevOps Engineer proficient with containers and container orchestration tools like Docker, Kubernetes along with experienced in Infrastructure as code tools and Configuration as code tools, Terraform, Ansible. Well-versed in CICD tool - Jenkins. Have hands-on experience with various AWS and Azure services. I really enjoy learning new things and connecting with people across a range of industries, so don't hesitate to reach out if you'd like to get in touch.
Conditional Statements
Conditional statements in Python are used to make decisions based on conditions.
These statements control the flow of execution depending on whether a condition evaluates to
TrueorFalse.
Types of Conditional Statements
ifStatementif-elseStatementif-elif-elseStatement
if Statement
The
ifstatement executes a block of code if the condition isTrue.if condition: # Code to execute if condition is True
if-else Statement
The
if-elsestatement provides an alternative block of code to execute if the condition isFalse.if condition: # Code to execute if condition is True else: # Code to execute if condition is False
if-elif-else Statement
The
if-elif-elsestatement allows checking multiple conditions. Once a condition evaluates toTrue, the corresponding block is executed, and the remaining conditions are skipped.if condition1: # Code to execute if condition1 is True elif condition2: # Code to execute if condition2 is True else: # Code to execute if none of the conditions are True
Short-Hand Conditional Statements
One-Line if Statement
You can write an
ifstatement in one line.x = 10 if x > 5: print("x is greater than 5")
Ternary Operator (Conditional Expression)
A concise way to write
if-elsestatements in one line.x = 10 message = "x is greater than 5" if x > 5 else "x is not greater than 5" print(message)
Loops
- Loops in Python are used to execute a block of code repeatedly as long as a condition is true or for a predefined number of iterations.
Types of Loops in Python
forLoopwhileLoop
for Loop
The
forloop is used to iterate over a sequence (e.g., list, tuple, string, or range) or any iterable object.for variable in iterable: # Code to execute in each iterationWhile LoopUsing
range(): Therange()function is commonly used withforloops to generate a sequence of numbers.# Print numbers from 0 to 4 for i in range(5): print(i)
while Loop
The
whileloop repeats a block of code as long as a condition isTrue.while condition: # Code to execute while the condition is True
Special Statements in Loops
- Python provides special statements to control the flow of loops:
break: Exits the loop prematurely.for i in range(5): if i == 3: break print(i) # Output: 0 1 2continue: Skips the current iteration and moves to the next.for i in range(5): if i == 3: continue print(i) # Output: 0 1 2 4pass: Does nothing; acts as a placeholder.for i in range(5): if i == 3: pass print(i) # Output: 0 1 2 3 4
Loops with else
- Both
forandwhileloops can have an optionalelseblock, which executes if the loop completes normally (without abreak).
Example with for:
for i in range(5):
print(i)
else:
print("Loop completed successfully!")
Example with while:
i = 0
while i < 3:
print(i)
i += 1
else:
print("Loop completed successfully!")