In all modern programming languages, conditional statements and loops are essential for two primary purposes:
- Making decisions based on given or computed values.
- Automating repetitive tasks to avoid rewriting the same code.
If…else
The if...else
statement allows you to change the normal flow of code execution by evaluating a specific condition.
Here is an example
x = int(input("Insert a number:"))
if x % 2 == 0:
print("Your number is even")
else:
print("Your number is odd")
This code will print a different message depending on whether the value entered by the user is an even or an odd number.
In Python, there is also a variation called elif
, which allows you to chain multiple conditions. For example:
x = int(input("\n3Insert a number greater than 0:"))
if x == 0:
print("Invalid value!")
elif x > 0:
print("You inserted a positive number!")
else:
print("You inserted a negative number!")
With this code, we can check if the entered value is not 0, and if it isn’t, determine whether it is positive or negative.
Loops in Python
Python supports two main types of loops: for
and while
. Both are used to repeat blocks of code without rewriting them, but they differ in their logical behavior:
- while: Executes the code as long as a specified condition remains true.
- for: Iterates over a collection of data (such as lists, tuples, or strings) and executes the code for each element in the collection.
Here are a couple of examples:
While Loop
print("Printing all values from 10 down to 6...")
x = 10
while x > 5:
print(x)
x = x - 1
In this example, the while
loop prints numbers starting from 10 and continues until the value of x
is greater than 5. In each iteration, the value of x
is decreased by 1.
For Loop
fruit_list = ["Apple", "Banana", "Coconut", "Raspberry", "Strawberry"]
print("Printing all elements in the list...")
for fruit in fruit_list:
print(fruit)
This example uses a for
loop to iterate over a list of fruits and prints each element in the list.
Combining Loops and If…else
We can combine loops with conditional statements (if...else
) to create more complex logic. Specifically, the continue
and break
statements allow us to control the flow of loop execution.
Example with continue
print("Printing all elements in the list except for Banana...")
for fruit in fruit_list:
if fruit == "Banana":
continue
print(fruit)
In this case, the loop prints all the elements in the fruit list except “Banana”. The continue
statement skips the current iteration and proceeds with the next one.
Example with break
print("Printing all numbers in the list. Break the loop if there is any non number element")
number_list = [1, 2, 3, "a", 4, 5]
for x in number_list:
if type(x) is int:
print(x)
else:
print(f"Found not number element: {x}")
print("For Loop will be terminated immediately")
break
Here, the loop stops execution as soon as it encounters an element that is not a number. Unlike continue
, the break
statement terminates the loop entirely and moves to the next instruction in the program.
Conclusions
Conditional statements and loops are essential tools for automating tasks and making decisions in Python programs. In future tutorials, we will delve deeper into their practical applications.
Find the link to the example codes here.