Control Flow Statements
Control flow is the order in which Python executes the lines of your code. Up to this point we have used conditional logic to skip lines and loops to repeat them. Control flow statements take this one step further by giving us precise control over what happens inside a loop while it is running.
Python gives us three control flow statements for use inside loops:
breakstop the loop entirelycontinueskip the current iteration and move to the next onepassdo nothing, act as a placeholder
break
break exits the loop immediately the moment Python
reaches it. It does not matter how many iterations are left or
whether the loop condition is still True. Python stops
the loop and moves on to the next line of code after it.
break works exactly the same way in both
for and while loops.
break in a for loop: stop as soon as a specific item is found.
break in a while loop: stop as soon as a condition is met.
continue
continue does not stop the loop. Instead it
skips the rest of the current iteration and jumps
straight to the next one. Think of it as saying "never mind this
one, move on."
This is useful when you want to filter out specific items without stopping the loop entirely.
continue in a for loop: skip specific items.
continue in a while loop: skip an iteration based on a condition.
pass
pass is unique. It does absolutely nothing. It is a
placeholder that tells Python "there is nothing here
yet but do not throw an error." Python requires that code blocks are
never empty. If you write an if statement, a loop, or
a function with nothing inside it Python will throw a
SyntaxError. pass satisfies that requirement
without actually doing anything.
You will most commonly see pass used when you are
planning out the structure of your code and want to come back to
fill in the details later. It is a way of saying "I know this needs
to do something, I just have not written it yet."
Run the code below and observe the error. Then add pass
inside the loop to fix it and run it again.
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named control_flow.py in your part49 folder.
- Complete the tasks and use # comments to explain your answers.
Challenge: Find the Duplicates
This challenge will test your understanding of loops, conditional logic and list methods. Your task is to find every letter that appears more than once in a list and print them without repeating any duplicates in your output.
Start with this list and build your solution in VS Code:
some_list = ['a', 'b', 'c', 'd', 'b', 'v', 'p', 'p']- Create an empty list to store your duplicates.
-
Loop through
some_listand check how many times each letter appears. - If a letter appears more than once and has not already been added to your duplicates list, add it.
- Print the final list of duplicates.
Think carefully about why you need two conditions inside your loop and what would happen if you only used one. Once you have it working try modifying the list to include more duplicates and see if your solution still holds up.
Challenge: Christmas Tree
This challenge combines everything you have learned so far. Lists, nested loops, conditional logic and control flow. Your task is to recreate a Christmas tree in the terminal using a list of lists.
Each inner list represents a row of the tree. A
1 represents a part of the tree and a 0
represents an empty space. Your job is to loop through each row and
each item and print the correct character depending on the value.
- If the value is
1print a tree character such as0or* - If the value is
0print an empty space
You will need to research how to use the end argument
in the print() function to prevent each character from
printing on a new line. This is part of the challenge, figure it out!
Start with this list of lists and build your solution in VS Code:
pic = [ [0,0,0,1,0,0,0], [0,0,1,1,1,0,0], [0,1,1,1,1,1,0], [1,1,1,1,1,1,1], [0,0,0,1,0,0,0], [0,0,0,1,0,0,0]]Once you have the tree working try changing the characters and the shape of the tree by modifying the list of lists. Can you make it bigger? Can you add a star at the top?
Don't Forget to commit and Push!