Lesson 8

Main Concept: Mastering Python Loops

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

1. Syntax of Loops

2. While Loops

3. Break/Continue

4. For Loops

5. Range() Statement


By the end you will be able to: Understand what a for/while loops is and the syntax for them. You will also learn what break, continue, and range statements do.

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

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

1. Syntax of Loops

Loops automate repetitive tasks, making code more concise and maintainable. They’re like assembly lines, repeating a single process for multiple items.

 

 

Python has for loops and while loops. For loops iterate through sequences, while loops continue as long as a condition is true.

 

 

The general syntax of both for loops and while loops is identical. It goes:

2. While Loops

While loops allow you to run a section of code repeatedly until a certain condition is met. Let’s think of some examples in which we can use them:

 

 

Imagine you want to keep asking for a password until the correct one is entered:

3. Break/Continue

The break statement in Python is used to exit a loop (for or while) prematurely when a certain condition is met. Instead of continuing to execute the remaining iterations, the loop is terminated, and the program proceeds with the next block of code outside the loop.

The continue statement in Python is used to skip the remaining code within a loop (for or while) for the current iteration and immediately move to the next iteration. This allows you to bypass specific parts of the loop without exiting the loop entirely.

In this example, we’ll use a for loop to iterate through numbers from 1 to 10. We’ll use the ‘continue’ statement to skip even numbers, and we’ll use the ‘break’ statement to exit the loop when the number 7 is encountered. Notice no even numbers are printed when the program runs.

4. For Loops

For loops help you repeat a certain action or set of actions for a specific number of times or for each item in a list. By learning the syntax, types of for loops, and common mistakes, you can write better code and save time.

 

The general structure of a for loop is:

Make sure you use the correct indentation (spaces at the beginning of a line) for the code inside the loop. Here’s an example that prints the numbers from 1 to 5:

Types of for loops: You can use for loops with different data structures:

5. Range() Statement

The Python range() function is a built-in function that generates a list of numbers. The range() function can take one, two, or three arguments:

  • range(stop): This form generates a sequence of numbers from 0 up to, but not including, the stop value. The stop value must be an integer.
  • range(start, stop): This form generates a sequence of numbers starting from start up to, but not including, the stop value. Both start and stop must be integers.
  • range(start, stop, step): This form generates a sequence of numbers starting from start up to, but not including, the stop value, incrementing by the step value. All of start, stop, and step must be integers.

In all cases, the range() function creates a sequence of integers, where the default starting value is 0 and the default step size is 1. Note that the stop value is never included in the range of numbers. The range goes up to but does not include the stop value!

Last Lessons Content

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.

Last Lessons Content

Next Lessons Content

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.

Main Concept: Coming soon…

Coming: Soon…