Decision making in Python

Python tutorial if elif else statement

Decision-making or if statement in Python

Decision-making or if statement is a control structure that allows you to run codes depend on the condition that you set.

If a condition is true, a code will work. If your condition is not true, another code will work or the program will continue to run other program codes.

What is the structure of an if statement in Python?

We have if, elif, else, the condition, and code place.

#____ProgrammerCodes.com____
a = 5
b = 2
#____ProgrammerCodes.com____

a and b are the number types of variables. If you don’t know what they are, check out this variables tutorial in Python.

#____ProgrammerCodes.com____
if (a > b):
    print("A greater than B");
elif (A < B):
    print("A less than B");
else:
    print("A equal to B");
#____ProgrammerCodes.com____
  • the sign (>) is an operator that means (greater than).
  • the sign (<) is an operator that means (less than).
  • We use these operators such as (<, >, and ==’) to compare between numbers, strings, or other variables.

If we want to output a result or display it on the program, we use print(). We write our variables inside it.

(a > b) is the first condition.

The first statement If (a > b) means that our program will check this condition a > b and if it is true, it will run the code that is inside this first statement.

If our first statement is not true, our program will go to see if we have (else if) or ‘else’ in order to check another condition.
In the case of our program. the second statement (elif(A < B)) will work the same as the first one but this time the condition is another one.
The program will check the condition then it will run the code if the condition is true.
If the condition of the second statement is not true, the program will go to (else) and this last statement means that if no condition of the previous statements is true, a code will run if it is available.

You can write the first statement (if) or combine (if) with (else).

Example 1

#____ProgrammerCodes.com____
a = 5
b = 2
if (a > b):
    print("A greater than B");
elif (A < B):
    print("A less than B");
else:
    print("A equal to B");
#____ProgrammerCodes.com____

Result 1

A greater than B
if statement in python

You can go deeper and write a nested decision-making structure where there is an if statement inside another if statement.

Sometimes you will need to check a lot of things within the same condition so then the nested if statement is the good solution. You can do multiple levels of nested if statement but when things get complicated or confusing, you should define functions or create classes.

If there are a lot of levels of nested if statement (a lot of if inside if) things will be so confused to you and to other programmers who will read your code.

Example 2

#____ProgrammerCodes.com____
a = 5
b = 3
if (a > b):
    if (a == 5):
        print("Top 5 Websites");
    elif (a == 10):
        print("Top 10 websites")
else:
   print("Try Again")
#____ProgrammerCodes.com____

Result 2

Top 5 Websites
nested if statement in python

Tutorial’s Question

What is the correct syntax of “else” that exist in the decision making structure?

  • else{}:
  • else():
  • else:

Simple Examples

If you have learned the basics of Python and you want to see some simple examples about these concepts such as numbers, for loop, function, and so on, I suggest you visit these articles below:

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *