Programming Essentials Python - Basic Programming Constructs - All about for loops

Let us understand how we can loop through using for in Python.

  • Go through range between 2 integers (5 and 10) and print them. We can use the range function to get a range between two integers.
    range(5, 11)
    list(range(5, 11))
    list(range(5, 11, 2))
    for i in range(5, 11): print(i)
  • Create a list of 7 elements and print alternate numbers starting from 1.

[1, 6, 8, 3, 7, 2, 9]

  • In this example, we are using a list. It will be covered extensively at a later point in time. list is one of the core Python Data Structures.
l = [1, 6, 8, 3, 7, 2, 9]
type(l)
cnt = 0

for i in l:

    if cnt % 2 == 0: # checking if cnt is even or not

        print(i)

    cnt += 1 # incrementing cnt by 1
  • Go through the range between 2 integers (5 and 15) and print even numbers.
for i in range(5, 16): 

    if i % 2 == 0:

        print(i)
  • Iterate through a list of months and print them.
months = ['January', 'February', 'March']

for month in months:

    print(month)
import calendar

list(calendar.month_name)
for month in list(calendar.month_name)[1:]:

    print(month)

Task 1

Take the list of ages and determine the category of the baby.

  • Print New Born or Infant till 6 months.
  • Print Toddler from 7 months to 18 months.
  • Print Grown up from 19 months to 144 months.
  • Print Youth from 145 months to 216 months
ages = [7, 3, 15, 145, 217, 18]
for age in ages:

    if age <= 6:

        print(f'Kid with {age} months age is "New Born or Infant"')

    elif age > 6 and age <= 18:

        print(f'Kid with {age} months age is "Toddler"')

    elif age > 18 and age <= 144:

        print(f'Kid with {age} months age is "Grown up"')

    elif age > 144 and age <= 216:

        print(f'Kid with {age} months age is "Youth"')

    else:

        print(f'Kid with {age} months age is "Adult"')

Task 2

Check if each number in the list of integers is even or divisible by 3.

ns = [1, 6, 2, 7, 9, 11, 3, 4]
for n in ns:

    if n % 2 == 0 or n % 3 == 0:

        print(f'Number {n} is even or divisible by 3')

Task 3

Check if the number in the list of integers is even and divisible by 3

ns = [1, 6, 2, 7, 9, 11, 3, 4]
for n in ns:

    if n % 2 == 0 and n % 3 == 0:

        print(f'Number {n} is even or divisible by 3')

    elif n % 3 == 0:

        print(f'Number {n} is divisible by 3 but not even')

    elif n % 2 == 0:

        print(f'Number {n} is even but not divisible by 3')

Task 4

Check if each salary in the list of salaries is valid. If the salary is negative or 0, then print Salary {salary_amount} is invalid. If the salary is None, then print Salary is Not Available.

sals = [100.0, 1000.0, 0.0, -100, 1200.0, None, -500]
for sal in sals:

    if sal == None:

        print('Salary is Not Available')

    elif sal <= 0:

        print(f'Salary {sal} is invalid')
We can also use a list of tuples with an employee ID and salary. You will see examples like this as part of manipulating collections later

[Watch the video tutorial here](https://www.youtube.com/watch?v=jyhkj3VQJaI)