Python For Loops

python-for-loop-ipcisco.com-1

In this lesson, we will focus on Python For Loops and we will learn how to use Python For Loops for various reasons. Python For Loop is basically used to iterate over a sequence. In other words, it repeats a job for a given sequence. This sequence can be a python list, a python tuple, a string, a dictionary or a set.

 

The usage of Python For Loop is like below:

 

for value in sequence

body (any action)

 

Here, the key line is the first line. In the first line, we define the for loop and in the body part, we define the action that the code will do everytime. Let’s do some examples to learn Python For Loop better.

 

In the below python code, we will print the square of eaxh list member everytime.

 

numbers = [1, 2, 3]
for x in numbers:
  print(x*x)

 

1
4
9

 

how-to-use-for-loop-in-python-ipcisco

 

In another example, let’s add the members of a tuple to a list with for loops.

 

numbers1 = (1, 2, 3, 4, 5)
numbers2 = []
for x in numbers1:
  numbers2.append(x)
print(numbers2)

 

The output of this code will be:

 

[1, 2, 3, 4, 5]

 

python-for-loop-ipcisco.com-1

 

We can also us efor loops with strings. In the below example, we will print the letters of the string “Aragorn”.

 

for x in "Aragorn":
  print(x)

 

A
r
a
g
o
r
n

 

Using If Else Statements With Python For Loop

 

We use python if else statements with Python For Loops too much. With these statements we check any condition in the body of the for loop. To understand the usage of if else statements with for loops, let’s do an example.

 

In this example, we will control each number if it is even and then we will print the square of these even numbers.

 

numbers = [1, 2, 3, 4, 5, 6, 7, 8]
for x in numbers:
  if (x % 2 == 0):
   print(x*x)

 

4
16
36
64

 

python-for-loop-ipcisco-3

 

Now, let’s use if else statement with for loops. Here, we will select even and odd numbers in a given list and we will put them in different lists.

 

numbers = [1, 2, 3, 4, 5, 6, 7, 8]
even = []
odd = []
for x in numbers:
  if (x % 2 == 0):
               even.append(x)
  else:
               odd.append(x)
print("Even numbers:", even)
print("Odd numbers:", odd)

 

The output of this code will be:

 

Even numbers: [2, 4, 6, 8]
Odd numbers: [1, 3, 5, 7]

 

python-for-loop-ipcisco.com-2

Python For Loop : Break Statement

 

We use break statement to stop the loop before it loops through all the items and ends. We force it to stop. For example if we control the value of a variable in the loop, we can use break statement to end the loop if this variable reaches a defined  value.

 

Let’s give example to understant break statement in Python For Loop.

 

numbers = [1, 2, 3, 4, 5, 6, 7, 8]
for x in numbers:
  if (x == 5):
    break
  print(x*x)

 

In the output of this code, the square of the numbers up to 5 will be printed. Because, we control the value in Python For Loop and if it is equal to 5, we will not continue to the for loop action.

 

1
4
9
16

 

for-loop-usage,in-python-2

 

In the below, we will control the square of the numbers. If we see a number whose square is higher than 40, the code will end the loop. Until the end, the code will print the square of the numbers.

 

numbers = [1, 2, 3, 4, 5, 6, 7, 8]
for x in numbers:
  if (x*x > 40):
    break
  print(x*x)

 

The output of this code will be:

 

1
4
9
16
25
36

 

Python For Loop : Continue Statement

 

We use continue statement to stop the iteration of the loop fort he current step and then continue the loop with the next step. For example we can control the value of an item in the loop, and if find it then we can use continue statement to jump over it and do the for action fort he next item.

 

In the below example, we will control the item in the for loop and if our item is equal to a specified value, then we will not do the action in for loop for it. Here, we will control that if lour item is equal to 5 or 7. If it is equal one of them, then we will not print the square of it.

 

numbers = [1, 2, 3, 4, 5, 6, 7, 8]
for x in numbers:
  if (x == 5):
    continue
  if (x == 7):
    continue
  print(x*x)

 

As you can see below, the square of 5 and 7 is not printed.

 

1
4
9
16
36
64

 

Let’s do another example to understand continue statement in Python For Loop better. In the below code, we will use continue statement top rint only the square of the odd numbers. We will control the numbers with if statement, if they are even then we will use continue. So, their square will not be printed.

 

numbers = [1, 2, 3, 4, 5, 6, 7, 8]
for x in numbers:
  if (x % 2 == 0):
    continue
  print(x*x)

 

1
9
25
49

Python Nested For Loops

 

A Nested For Loop is the for loop inside a for loop. So, in Python nested for loops, you can do iteration under another iteration. Let’s give example ans see how to use nested for loops in python.

 

numbers = ["first", "second", "third"]
characters = ["King", "Queen", "Princes"]
for x in numbers:
  for y in characters:
    print(x, y)

 

The output of this code will be:

 

first King
first Queen
first Princes
second King
second Queen
second Princes
third King
third Queen
third Princes

 

for-loop-usage,in-python-4

 

Now, let’s show another example with Standard 52-card deck. Here, we will print some of the cards in Standard 52-card deck.

 

suits = ["Clubs", "Diamonds", "Hearts", "Spades"]
ranks = ["Ace", "Jack", "Queen", "King"]
for x in suits:
  for y in ranks:
    print(x, y)

 

Clubs Ace
Clubs Jack
Clubs Queen
Clubs King
Diamonds Ace
Diamonds Jack
Diamonds Queen
Diamonds King
Hearts Ace
Hearts Jack
Hearts Queen
Hearts King
Spades Ace
Spades Jack
Spades Queen
Spades King

 

In this Python For Loop lesson, we have explained for loops used in python with different examples. To learn python for loops you should do more and different example. If you think about different example that you can do with for loops and if you try to do them, this will increase your experinece on python for loops.

 

 

Back to: Python Programming Course > Python Loops

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k

Python Programming Course

Collapse
Expand