Theory of Intelligence
PYTHON PROGRAMMING•Control Flow And Functions•Control Flow•1.1 Conditionals

1.1.1 if, elif & else

Subtopics: if, elif & else and Truthiness in Conditions.

1.1.1 if, elif & else

In programming, we often need to control the flow of our program based on different conditions or repeat certain actions multiple times. There are two main types of control structures, Decision Making and Looping:

Decision Making

Decision making in programming allows the program to choose different actions based on conditions. Python provides a variety of conditional statements to implement decision making.

If Statement

The if statement allows us to make decisions based on conditions. If the condition is true, it runs a block of code.

PYTHON
# Syntax of if statement
if condition:
    # code to run if the condition is true

Example: If the temperature is above 3030 degrees, we print a message.

PYTHON
temperature = 35
if temperature > 30:
    print("It is a hot day")

If-else Statement

The if-else statement allows us to execute one block of code if a condition is true and another block if the condition is false.

PYTHON
# Syntax of if-else statement
if condition:
    # code to run if the condition is true
else:
    # code to run if the condition is false

Example:

PYTHON
temperature = 15
if temperature > 30:
    print("It's a hot day")
else:
    print("It's not a hot day")

Short Hand if-else Statement

Python also allows a short-hand if-else statement that can be written in a single line.

PYTHON
# Syntax of short hand if-else statement
action_if_true if condition else action_if_false

temperature = 15
m = "It's a hot day" if (temperature > 30) else "It's not a hot day"
print(m)

Class Activity

Write an if-else statement and a short-hand if-else statement to check if a number is even or odd and print the appropriate message.

if-elif-else Statement

The if-elif-else statement allows us to check multiple conditions and execute different blocks of code for each condition.

PYTHON
# Syntax of if-elif-else statement
if condition1:
    # code to run if condition1 is true
elif condition2:
    # code to run if condition2 is true
else:
    # code to run if none of the conditions are true

Example:

PYTHON
weather = "cloudy"  # The output depends on the value stored in the variable weather
if weather == "sunny":
    print("Wear sunglasses")
elif weather == "rainy":
    print("Take an umbrella")
else:
    print("Enjoy your day!")

Class Activity

Write an if-elif-else statement to check if a number is positive, negative, or zero.

Looping Constructs

Loops help us repeat actions, making our code more efficient and easier to read. There are two main types of loops in Python: while loops and for loops.

while Loop

A while loop runs as long as a condition is true. It checks the condition before each iteration and stops running when the condition is no longer true.

PYTHON
# Syntax of while loop
while condition:
    # code to run while the condition is true

Example: Add 11 to a number until it reaches 1010.

PYTHON
number = 1
while number < 10:
    print(number)
    number += 1

Class Activity

Write a Python program that prints even and counts the odd numbers from 11 to 2020 using a while loop.

for Loop

A for loop repeats a block of code a specific number of times. It is commonly used to iterate over a sequence (like a list, tuple, or string).

PYTHON
# Syntax of for loop
for variable in sequence:
    # code to run for each element in the sequence

Example 1: Say "Hello" to each friend in a list of friends.

PYTHON
friends = ["Ahmad", "Ali", "Hassan"]
for friend in friends:
    print("Hello", friend)

Explanation: In this example, the code goes through each friend in the list and prints a greeting message for each one.

Class Activity

  1. Write a for loop using range() to print the even numbers from 22 to 1010.
  2. Write a Python program that prints the first 1010 multiples of 33 using a for loop and the range() function.

Discuss theoretical concepts, mathematical formulas, and architectural foundations.

Supports inline code with `backticks`
No comments yet. Be the first to start the discussion!