Try a free Python lesson with Python Essentials

Try a Free Lesson

No account required. Just code.

Welcome to Python Essentials

This is a sample lesson from the full course. No sign up, no payment, no catch. Just work through the lesson, run the code in your browser and see what Python can do.

This lesson is from Module 6: Logical Operators and Loops. By the end you will have written a program that draws a Christmas tree in the terminal using nothing but lists, loops and conditional logic.

Control Flow Statements

Control flow is the order in which Python executes the lines of your code. Up to this point in the full course students 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:

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.

Run the code below and observe the error. Then add pass inside the loop to fix it and run it again.

Challenge: Christmas Tree

This challenge combines everything from this lesson. Lists, nested loops, conditional logic and control flow. Your task is to recreate a Christmas tree 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.

You will need to use the end argument in the print() function to prevent each character from printing on a new line. Try searching for how it works and figure it out yourself. That is exactly how real developers learn.

Start with this list of lists in the pybox below:

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?

That is just one lesson.

The full Python Essentials course has 57 lessons across 7 modules, taking you from absolute zero all the way through to writing real programs. Every lesson has interactive code examples you can run directly in your browser, hands on exercises in VS Code and a quiz to test your understanding.

It is completely free. No payment. No catch.

Create a free account to save your progress, track your scores across all 57 lessons and earn a certificate for each module you complete.