Flow Of Control - Python

Conditional


  • If Else
        if (condition):
            # statement
        elif (condition):
            # statement
        else:
            # statement
    
  • Shorthand If Else
        print("value1") if condition else print("value2")
        print("value1") if condition1 else print("value2") if condition2 else print("value3")
    

Loop


  • For
    • Unpack key-value pair of dictionary into 2 varNames
          for key, value in varD.items():
              # statement
      
    • Unpacks sequence pair into two varNames
          for value1, value2 in sequence:
              # statement
      
    • value runs in Sequence
          for value in sequence:
              # statement
      
    • Loop With else
          for value in sequence:
              # statement
          else:
              # statement
      
  • Range
        for varName in range(N): # From 0 to N-1
            print(varName)
        for varName in range(N1:N2):  # From N1 to N2-1
            print(varName)
        for varName in range(N1:N2:N3):  # From N1 to N2-1 adding N3
            print(varName)
    
  • While
        while (condition):
            # statement runs till condition is true
        else:
            # Else runs when condition becomes false
    
  • break => Leaves the Loop
  • continue => Leaves the iteration
  • Enumerate Function
        for index, element in enumerate(sequence):
            # statement
        for index, element in enumerate(sequence, start=N):
            # Start the index with "N"
    

Match-Case


    match varName:
        case value1: # Pattern
            # Statement
        case value2 if condition: # Pattern with Condition
            # Statement
        case _ if condition: # Default case with Condition
            # Statement
        case _: # Default Case
            # Statement
Share: