Lesson 6

Main Concept: Understanding loops and an introduction to libraries.

Here are a few things that can help you understand the Main Concept for todays lesson:

1. Boolean

2. Loops

3. While Loops

4/5. Uses of While Loops

6. Libraries

By the end you will be able to: Understand what a boolean value is and why it is used, and also what loops do in Python and how they help to save time writing code and make it more efficient.

Here's a 2 page PDF that covers the same content if that's easier for you

1. Boolean

A Boolean is a type of variable that can only have one of two values: True or False.

 

We can use Boolean variables to represent conditions that are either true or false. For example, we can use a boolean variable to represent whether it is raining outside or not. Or whether the lights in a room are on/off.

2. Loops

Loops are a way to repeat a block of code. We can use loops to automate repetitive tasks and make our code more efficient. There are two types of loops in Python: for loops and while loops.

 

We can use loops to iterate over lists, perform calculations, and perform other repetitive tasks.

 

Let’s imagine an example where we want to print “Hello World” to the screen 5 times. We can do either:

Both methods above will give us the exact same result, however one is much easier to type, doesn’t have any repeated code, and can be easily edited to repeat more times or print a different statement.

 

Overall, using loops here made our code more efficient and in the process, saved us time and effort.

3. While Loops

A while loop is a type of loop that repeats a block of code until a certain condition is met. The condition is checked at the beginning of each iteration of the loop. If the condition is true, the block of code is executed. If the condition is false, the loop stops.

 

Example: If we want to print the number 1-5 in order, we can use a while loop.

Here are two different examples:

4. Looping Until User Types Quit

We can use a while loop to repeatedly ask the user for input until they enter a certain value. This is a common way to build interactive programs.

 

Example: If we wanted to repeatedly ask the user for input until they enter the word “quit”, we could write:

5. Counters

Counters are variables that keep track of how many times a loop has iterated. In a while loop, we can use a counter to control how many times the loop iterates.

6. Libraries

Libraries are pre-written code collections containing functions, variables, and other code, made by developers for programmers to import into their programs. This saves time by using pre-existing code instead of writing complex code from scratch.

Last Lessons Content

Main Concept: Saving multiple elements and manipulating variables

By the end you will be able to: Store a variety of elements inside of one variable, know how to get certain elements out, and how to manipulate strings/lists.

Last Lessons Content

Next Lessons Content

Main Concept: Saving multiple elements and manipulating variables

By the end you will be able to: Store a variety of elements inside of one variable, know how to get certain elements out, and how to manipulate strings/lists.

Main Concept:

Understanding for loops and how they work with lists.

By the end you will be able to: Understand what a for loop is and why it is used. Also how for loops and lists work together to extract data from them.

If you'd like to have a qualified tutor explain this lessons content to you, download our Spartan Tutors app today!