Python: For Loops, While Loops and If-Else Statements

Python: For Loops, While Loops and If-Else Statements

Overview

This article will explain what is, how to make and use for loops, while loops and if-else statements in Python.

Materials

  • Computer

  • Python

  • IDE or Terminal

For-Loops

A for-loop can be used to iterate through a range of numbers, strings, or data types that contain other variables.

# A for-loop

for i in range(0, 10, 1):
    print(i)

# A for-loop that increments by 2

for i in range(0, 10, 2):
    print(i)

  • The keyword "in" is often used to iterate through a variable

  • "range" is often used with for loops to iterate through a set of numbers. The three parameters it takes are:

    • The first number is the start of where it begins

    • The second number is the end number. It reiterates to the number before or a number less than the specified ending number depending on how the loop is being incremented or decremented.

    • The third number is the increment or decrement of how variable "i" or any declared variable is to change throughout the for loop. If a third number is not specified, then the for-loop will always try to increment by 1.

  • In many tutorials, "i" is often written as the declared variable to print out the values, though any name can be written in place of "i"

# Another for-loop with declared variable "num" instead of i
for num in range(0, 15):
    print(num)

# A for loop that decreases

for num in range(15, 0, -1):
    print(num)

# Using a for-loop to print a String

for i in "placeholder_string":
    print(i)

# Using a for-loop to print through a List

list_of_values = [1, 4, "sample_text", 7, 8]
for i in list_of_values:
    print(i)

While Statements

A while statement will always execute code within the block if the condition is satisfied. While statements are coded to execute a condition with a variable that increments or decrements to prevent it from running forever.

index = 0
while index < 3:
    print(index)
    index += 1

If-Statement

An if statement checks if a condition is True, and if it is, then the code in its block will be executed:

# An if-statement

if 3 > 2:
    print(True)

If-Else Statements

An if-else statement goes through two conditions to see if the specified condition from the if statement is true, otherwise, the else statement will be printed.

enter_a_word = input("Enter a word: ")

if enter_a_word[0] == 'a' or enter_a_word[0] == 'A':
    print("Word starts with a or A")
else:
    print("Word starts with a letter that is not a")

Using Elif

To add more conditions that can be checked within one if and else block, the elif statement can be used to check for more conditions:

enter_a_word = input("Enter a word: ")

if enter_a_word[0] == 'a' or enter_a_word[0] == 'A':
    print("Word starts with a or A")
elif enter_a_word[0] == 'b' or enter_a_word[0] == 'B':
    print("Word starts with b or B")
elif enter_a_word[0] == 'c' or enter_a_word[0] == 'C':
    print("Word starts with c or C")
else:
    print("Word starts with a different letter")

The first if or elif statement that is "True", executes the code within it and executes out of the if and else statement. For example, the user can input the word "Blueberry" for "enter_a_word". The if-else statement would execute afterward by first going through each if and elif statement to see which statement is True first. In this case, the if statement will not execute but the first elif statement would since the condition is satisfied and exit the entire if-else block, not executing the other statements that are afterward. If or any of the elif statements are not executed, then the else statement is always executed.

Other Techniques to Use

In a for-loop, while loop or if-else statement, the "break" or "pass" statements can be used

What is and How to use "break"

"break" if placed in a for-loop or while-loop will exit out of it if certain conditions are met. An if-statement is recommended to use "break" to see if the condition is met.

for i in range(0, 5):
    print(i)
    if i == 2:
        break

In this case, the numbers 3 and 4 should be printed but since the if statement checks the condition if when "i == 2" it would execute "break" to exit out of the loop so numbers 3 and 4 are not printed.

What is and How to use "pass"

"pass" is used as a placeholder. When executed, it returns no output in the Terminal. This is used when a certain condition is met but that condition or no code should be written to change anything related to the program.

for i in range(0, 5):
    print(i)
    if i == 2:
        pass