Python Tutorials

Overview

In Python, a for loop is used for iterating over a sequence (list, tuple, dictionary, set, or string).

The for Loop

Print each car in the cars list.

cars = ["Ferrari", "Maserati", "Alfa Romeo"]

for x in cars:
    print(x) #print each car in the cars list

Output:

Ferrari
Maserati
Alfa Romeo

Print each letter in the string “Alfa Romeo”.

for x in "Alfa Romeo":
    print(x) 

Output:

A
l
f
a
 
R
o
m
e
o

The else Statement

The else statement specifies a block of code to be executed when the loop is finished.

for x in range(4):
    print(x)
else:
    print("All done!")

Output:

0
1
2
3
All done!

Note

The else block will NOT be executed if the loop is stopped by a break statement.

The break Statement

The break statement will stop the loop before it has looped through all the items.

Example 1 – the break statement comes after the print statement.

cars = ["Ferrari", "Maserati", "Alfa Romeo"]

for x in cars:
  print(x) 
  if x == "Maserati":
      break

Example 2 – the break statement comes before the print statement.

cars = ["Ferrari", "Maserati", "Alfa Romeo"]


for x in cars:
    if x == "Maserati":
        break
    print(x) 

Output 1:

Ferrari
Maserati

Output 2:

Ferrari

The continue Statement

The continue statement will stop the current iteration, and continue with the next iteration.

cars = ["Ferrari", "Maserati", "Alfa Romeo"]

for x in cars:
    if x == "Maserati":
        cars
    print(x) 

Output:

Ferrari
Alfa Romeo

The range() Function

The range() function is used to loop through a set of code a specified number of times.

The range() function starts from 0 by default, and increments by 1 (by default), and ends at a specified number.

for x in range(4):
    print(x) 

Output:

0
1
2
3

Note

Note that range(4) is not the values of 0 to 4, but the values 0 to 3.

The range() function defaults to 0 as a starting value. To start with another value, an additional parameter is added.

For example: range(1, 4), which means the values 1 to 4 (1, 2, 3).

for x in range(1, 4):
    print(x) 

Output:

1
2
3

it is also possible to specify the increment value by adding a third parameter: range(1, 15, 3)

The range (1, 15) will increment by 3s.

for x in range(1, 15, 3):
    print(x) 

Output:

1
4
7
10
13

Using Nested Loops

A nested loop is a loop inside a loop where the inner loop is executed one time for each iteration of the outer loop.

colors = ["red", "yellow", "white"]
cars = ["Ferarri", "Maserati", "Alfa Romeo"]

for x in colors:
    for y in cars:
        print(x, y)

Output:

red Ferarri
red Maserati
red Alfa Romeo
yellow Ferarri
yellow Maserati
yellow Alfa Romeo
white Ferarri
white Maserati
white Alfa Romeo

Python Notes:

  • The most recent major version of Python is Python 3; however, Python 2 is still in use and quite popular, although not being updated with anything other than security updates
  • Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
  • Python relies on indentation, using whitespace to define scope, such as the scope of loops, functions, and classes; other programming languages often use curly-brackets for this purpose
  • Python string methods return new values, and DO NOT change the original string
  • Python tuples are unchangeable after created (their items CANNOT be changed or re-ordered at a later point)
  • Python sets are unordered (may appear in random orders when called), unchangeable (the value of individual items cannot be changed after creation), unindexed (items cannot be referred to by index or key), and duplicates are NOT ALLOWED
  • As of v3.7, Python dictionaries are ordered and duplicates ARE ALLOWED; in v3.6 and earlier, dictionaries were unordered (did not have a defined order and could not be referred to using an index)

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.