Skip to main content

Command Palette

Search for a command to run...

02 - Control Flow - Conditional Block and Loops

Updated
3 min read
R

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 True or False.

Types of Conditional Statements

  1. if Statement

  2. if-else Statement

  3. if-elif-else Statement

if Statement

  • The if statement executes a block of code if the condition is True.

      if condition:
          # Code to execute if condition is True
    

if-else Statement

  • The if-else statement provides an alternative block of code to execute if the condition is False.

      if condition:
          # Code to execute if condition is True
      else:
          # Code to execute if condition is False
    

if-elif-else Statement

  • The if-elif-else statement allows checking multiple conditions. Once a condition evaluates to True, 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 if statement in one line.

      x = 10
      if x > 5: print("x is greater than 5")
    

Ternary Operator (Conditional Expression)

  • A concise way to write if-else statements 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

  1. for Loop

  2. while Loop

for Loop

  • The for loop 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 Loop
    
  • Using range(): The range() function is commonly used with for loops to generate a sequence of numbers.

      # Print numbers from 0 to 4
      for i in range(5):
          print(i)
    

while Loop

  • The while loop repeats a block of code as long as a condition is True.

      while condition:
          # Code to execute while the condition is True
    

Special Statements in Loops

  • Python provides special statements to control the flow of loops:
  1. break: Exits the loop prematurely.

     for i in range(5):
         if i == 3:
             break
         print(i)
     # Output: 0 1 2
    
  2. continue: Skips the current iteration and moves to the next.

     for i in range(5):
         if i == 3:
             continue
         print(i)
     # Output: 0 1 2 4
    
  3. pass: 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 for and while loops can have an optional else block, which executes if the loop completes normally (without a break).

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!")

More from this blog

Rohit Pagote Blogs

39 posts

A DevOps enthusiast with a passion for implementing modern DevOps terminologies and tools and proficient in AWS cloud, Azure cloud, DevOps practices, IT infrastructure and Support.