DEV Community

Cover image for What are loops and conditional statements in Python? Understand them better!
Michael Abraham Wekesa
Michael Abraham Wekesa

Posted on • Updated on

What are loops and conditional statements in Python? Understand them better!

With the basics of python, you can now speak the language. in order to get to know and dive in further in to programming and putting some serious logic in code, there are a few things to learn and understand. Let's do it the python way!

  • Conditional statements
  • Loops

Conditional statements

Conditional statements are commands used in handling decisions, in which a decision is made if and only if the pre-stated conditions are either true or false.
For example the traffic lights, you only start driving if and only if the lights turn from red to green else you wait.

Before diving deep into conditional statements there are some logical conditions to consider, of which play a crucial part in making conditions with conditional statements, in python these basic logical conditions from math are:

  • Less than, e.g a < b
  • Equals, e.g a == b
  • Greater than, e.g a > b
  • Less than or equal to, e.g a <= b
  • Greater than or equal to, e.g a>= b
  • Not Equals, e.g a != b

These logical conditions are used with if statements and loops in manipulating the control flow in programming.

1. If statement
A program will only continue to be executed if only the test expression evaluates to true and if the test expression is false, the statements after the condition is not executed.

if 2 > 1:
   print("Greater!")
Enter fullscreen mode Exit fullscreen mode

Output:

Greater
Enter fullscreen mode Exit fullscreen mode

2. Nested if statements
One condition is executed and if true then the next condition is executed and so on till the last if condition is met. These if statements are declared inside other if statements.

n = 10
if n < 20:
   n = 20
   print(" Greater than 10")
   if n < 30:
      n = 30
      print(" Greater than 20")
Enter fullscreen mode Exit fullscreen mode

Output:

Greater than 10!
Greater than 20!
Enter fullscreen mode Exit fullscreen mode

3. Elif statements
elif is a keyword that enables a program to go through another if condition if the previous one is false e.g.

a = 43
b = 57
if a == 57:
   print("a is equal to 57!")
elif b == 57:
   print("b is equal to 57!")
else:
   print("Sorry, neither a nor b is equal to 57!")
Enter fullscreen mode Exit fullscreen mode

Output:

b is equal to 57!
Enter fullscreen mode Exit fullscreen mode

4. If not Statements
not is a keyword that returns True if the statement is not true. returns true if a value is false;

num_list = [2,4,6,8,9]
num = 5
if num not in num_list:
   print("num not in num_list")
else:
   print("Found it in num_list!")
Enter fullscreen mode Exit fullscreen mode

Output:

num not in num_list
Enter fullscreen mode Exit fullscreen mode

Python loops

A loop is basically a set of instructions or statements of a given program that is repeated a finite number of times until a certain condition is met.
There are two types of loops in python:

  • For loops
  • while loops

1. for loops
A for loop is used for iterating over a sequence of statements multiple times, e.g a list, dictionary or a tuple.

instruments = ["Guitar", "Piano", "Flute", "Drums"]
for instrument in instruments:
   print(instrument)
Enter fullscreen mode Exit fullscreen mode

Output:

Guitar
Piano
Flute
Drums
Enter fullscreen mode Exit fullscreen mode

2. while loops
While loops repeat a statement or a block of statements until a given condition is false. It checks the condition before executing the loop body, if the condition is true, it will go on until it is false, then exit the loop.

i = 1
while i != 5:
  print(i)
  i += 1
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
Enter fullscreen mode Exit fullscreen mode

Break statement
A break statement is used in stopping a loop even if the condition still passes as true.

i = 1
while i != 8:
  print(i)
  if i == 6:
    break
  i += 1
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
5
6
Enter fullscreen mode Exit fullscreen mode

Continue statement
The continue keyword can stop the current iteration and continue with the next iteration.

i = 0
while i != 8:
  i += 1
  if i == 6:
    continue
  print(i)
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
5
7
8
Enter fullscreen mode Exit fullscreen mode

With the knowledge of loops and conditional statements. There's more to do with python. As we progress we unleash its potential.
Here is a simple calculator in which we get to implement the conditional statements.

Simple calculator

print("The simple calculator")
print("===========")
num1 = input("Enter the first value: ")
num2 = input("Enter the second value: ")
print("===========")
print("\n1.Addition\t2.Subtraction\n3.Multiplication\t4.Division")
choice = input("Pick a choice (1,2,3 or 4): ")
num1 = int(num1)
num2 = int(num2)

if choice == "1":
    print( num1 + num2)
elif choice == "2":
    print(num1 - num2)
elif choice == "3":
    print(num1 * num2)
elif choice == "4":
    print(num1 / num2)
else:
    print("invalid option")
Enter fullscreen mode Exit fullscreen mode

Conclusion
With that, adios! And happy coding. Hope you found this informative.

Top comments (0)