30-Days-Of-Python/10_Day/10_loop.md
2020-04-30 14:49:24 +03:00

9.1 KiB

30 Days Of Python: Day 10 - Loops

Twitter Follow

Author: Asabeneh Yetayeh
First Edition: Nov 22 - Dec 22, 2019

<< Day 9 | Day 11 >>

30DaysOfPython

📘 Day 10

Loops

Life is full of routines. In programming also we do lots of repetitive tasks. In order to handle repetitive task programming languages provide loops. Python programming language also provides the following types of two loops to handle looping.

  1. while loop
  2. for loop

While Loop

We use the reserved word while to make a while loop. While loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop will be executed.

  # syntax
  while condition:
      code goes here

Example:

  count = 0
  while count < 5:
      print(count)
      count = count + 1

In the above while loop, the condition become false when count is 5, then the loop stops. If we are interested to run block of code once the condition is no longer true, we can use else.

  # syntax
  while condition:
      code goes here
  else:
      code goes here

Example:

  count = 0
  while count < 5:
        print(count)
        count = count + 1
  else:
      print(count)

The above loop condition will be false when count is 5 and the loop stops, and execution starts the else statement and 5 prints in the else statement.

Break and continue

  • Break: We use break when we like to get out or stop the loop.
  # syntax
  while condition:
      code goes here
      if another_condition:
          break

Example:

  count = 0
  while count < 5:
      print(count)
      count = count + 1
      if count == 3:
          break

The above while loop only prints 0, 1, 2, but when it reaches 3 it stops.

  • Continue: With the continue statement we can stop the current iteration, and continue with the next:
  # syntax
  while condition:
      code goes here
      if another_condition:
          continue

Example:

count = 0
while count < 5:
    if count == 3:
        continue
    print(count)
    count = count + 1

The above while loop only prints 0, 1, 2,4 but skips 3.

For Loop

A for key word used to make a for loop like in other programming language but with some syntax difference. Loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

  • For loop with list
  # syntax
  for iterator in lst:
    code goes here

Example:

  numbers = [0, 1, 2, 3, 4, 5]
  for number in numbers:
      print(number)
  • For loop with string
  # syntax
  for iterator in string:
    code goes here

Example:

  language = 'Python'
  for letter in language:
    print(letter)
  • For loop with tuple
  # syntax
  for iterator in tpl:
    code goes here

Example:

  numbers = (0, 1,2,3,4,5)
  for number in numbers:
    print(number)
  • For loop with dictionary Looping through a dictionary gives you the key of the dictionary.
  # syntax
  for iterator in dct:
    code goes here

Example:

  person = {
      'first_name':'Asabeneh',
      'last_name':'Yetayeh',
      'age':250,
      'country':'Finland',
      'is_marred':True,
      'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
      'address':{
          'street':'Space street',
          'zipcode':'02210'
      }
      }
  for key in person:
    print(key)

  for key, value in person.items():
    print(key, value) #
  • Loops in set
  # syntax
  for iterator in st:
    code goes here

Example:

  it_companies = {'Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'}
  for company in it_companies:
    print(company)

Break and Continue

Break: We use break when we like to stop our loop before the loop is completed.

  # syntax
  for iterator in sequence:
    code goes here
    if condition:
      break

Example:

  numbers = (0, 1,2,3,4,5)
  for number in numbers:
    print(number)
    if number == 3:
      break

In the above example, the loop stops when it reaches 3. Continue: We use continue when we like to skip some of the step in the iteration of the loop.

  # syntax
  for iterator in sequence:
    code goes here
    if condition:
      continue
**Example:**
  numbers = (0, 1,2,3,4,5)
  for number in numbers:
    print(number)
    if number == 3:
      continue

In the above example, if number is 3 the skip step and continues to the next.

The range function

The range() function uses to loop through a set of code a certain number of times. The range(start,end, step) takes three parameters:starting, ending and increment.By default it starts from 0 and the increment is 1. The range sequence doesn't include the end. Creating sequence using range

  lst = list(rang(11))
  print(lst) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  st = set(range(11))
  print(st) # {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

  lst = list(rang(0,11,2))
  print(lst) # [0, 2, 4, 6, 8, 10]
  st = set(range(0,11,2))
  print(st) #  {0, 2, 4, 6, 8, 10}
  # syntax
  for iterator in range(start, end, increment):

Example:

  for number in range(11):
    print(number)   # prints 0 to 10, not including 11
  fruits = ['banana', 'orange', 'mango', 'lemon']
  for fruit in fruits:
    print(fruit)

Nested for loop

We can write loop inside another loop.

  # syntax
  for x in y:
    for t in s:
      print(t)

Example:

  person = {
      'first_name': 'Asabeneh',
      'last_name': 'Yetayeh',
      'age': 250,
      'country': 'Finland',
      'is_marred': True,
      'skills': ['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
      'address': {
          'street': 'Space street',
          'zipcode': '02210'
      }
  }
  for key in person:
      if key == 'skills':
        for skill in person['skills']:
          print(skill)

For Else

If we want to execute some message when the loop ends, we use else.

  # syntax
  for iterator in range(start, end, increment):
    do something
  else:
    print('The loop is ended')

Example:

  for number in range(11):
    print(number)   # prints 0 to 10, not including 11
  else:
    print('The loop stops at', number)

Pass

In python after semicolon, it requires some code to run but we don't like to execute any code after if or for loop we can write the word pass to avoid error.

💻 Exercises: Day 10

  1. Iterate 0 to 10 using for loop, do the same using while and do while loop.
  2. Iterate 10 to 0 using for loop, do the same using while and do while loop.
  3. Write a loop that makes seven calls to print() output the following triangle:
      #
      ##
      ###
      ####
      #####
      ######
      #######
    
  4. Use nested loops to create the following:
    # # # # # # # #
    # # # # # # # #
    # # # # # # # #
    # # # # # # # #
    # # # # # # # #
    # # # # # # # #
    # # # # # # # #
    # # # # # # # #
    
  5. Print the following pattern:
    0 x 0 = 0
    1 x 1 = 1
    2 x 2 = 4
    3 x 3 = 9
    4 x 4 = 16
    5 x 5 = 25
    6 x 6 = 36
    7 x 7 = 49
    8 x 8 = 64
    9 x 9 = 81
    10 x 10 = 100
    
  6. Iterate through the list, ['Python', 'Numpy','Pandas','Django', 'Flask'] using a for loop and print out the items.
  7. Use for loop to iterate from 0 to 100 and print only even numbers
  8. Use for loop to iterate from 0 to 100 and print only odd numbers
  9. Use for loop to iterate from 0 to 100 and print the sum of all numbers.
    The sum of all numbers is 5050.
    
  10. Use for loop to iterate from 0 to 100 and print the sum of all evens and the sum of all odds.
      The sum of all evens is 2550. And the sum of all odds is 2500.
    
  11. Go to the data folder and use the countries.py file. Loop through the countries and extract all the countries containing the word land.
  12. This is a fruit list, ['banana', 'orange', 'mango', 'lemon'] reverse the order using loop.

🎉 CONGRATULATIONS ! 🎉

<< Day 9 | Day 11 >>