Conditional Statement

Conditional Statement in Python perform different computations or actions depending on whether a specific Boolean constraint evaluates to true or false. Conditional statements are handled by if ... elif ... else statements in python.

if Statement

if statement is used for decision making. It will run the body of code only IF the statement is satisfied or true. When you want to justify one condition while the other condition is not true, then you use if statement

if expression:
    statement(s)

Above is the syntax for the if statement. Now let's see an example of an if statement in action. We would use python to determine if a number is a positive number when that number is greater than 0.

In [2]: num = input("\nEnter a number: ")
number = int(num)

if number > 0:
print(number, "is a positive number.")

Above we have created a variable num to receive a number from the user. After that we convert the input num to an integer, this is because the input() takes strings only while > works with integers. Lets run the program and see the output.

Out[2]: Enter a number: 
7 is a positive number.

After running the program, we provided the number 7 and our output is 7 is a positive number.

if...else Statement

if expression:
    statement(s)
else:
    statement(s)

Above is the syntax for the if...else statement. Now let's see an example of the if...else statement in action.

I believe we all are thinking of what if we provided a negative number, what should the output be? Well here we can get the response of if the number we provided is negative using the if….else statement.

Well here we can get the response of if the number we provided is negative using the if….else statement.

In [2]: number input("\nEnter a number: ")
number = int(num)

if number > 0:
print(number, "is a positive number.")
else:
print (number, "is a negative number.")

Above we have added our else conditional statement to inform us if the number entered by the user is a negative number when a negative number is entered. Let’s take a look at the output when we enter a negative number.

Out[2]: Enter a number: 
-5 is a negative number.

As seen above that the output we get is from the else condition since the number -5 is a negative number.

Let's take a look at another scenario, I believe we all make use of apps that ask for our username and passwords when logging in.

In [2]: username = input("\nPlease enter username? ")
password = input("\nPlease enter password ")

if username == 'tina':
print('\nHello Tina')
else:
print('\incorrect username')

if password == 'swordfish'
print('Access granted! Welcome Tina.')
else:
print('incorrect password')

Above we have created a variable for username and password to receive the input tina as the username and swordfish as the password. From what we have if the username provided is tina the output would be Hello Tina and if it’s not (else) it gives the output incorrect username. Same goes for the password. Let's take a look at the output for both an incorrect input and a correct one.

Out[2]: Please enter username?: 
Please enter password:
Hello Tina
incorrect password

As seen above we provided the correct username tina but gave a wrong password catwoman and that can be seen from the output we got.

elif Statement

elif condition tells the program to print out the third condition or possibility when the other condition goes wrong or incorrect. The elif is short for else if. It allows the program to check for multiple expressions. For example if the condition for our if is False, it checks the condition of the next elif block and so on. If all the conditions are False, body of else is then executed.

if expression:
    statement(s)
elif another expression:
    statement(s)
elif another expression:
    statement(s)
else:
    statement(s)

Let's take a look at an example

In [2]: num = input("\nEnter a number: ")
number = int(num)

if number > 0:
print(number, "is a positive number.")
elif number == 0:
print(number, "is neither a positive nor negative number.")
else:
print(number, "is a negative number.")

From what we can see above, we now have an elif condition to check and give an output if the user gives the number 0 which is neither positive nor negative. Let's take a look at the output.

Out[2]: Enter a number: 
0 is neither a positive nor negative number.

After running our program we receive an output from the elif condition because the user provided 0 as the number.

Nested if Statement

Do you know we can have a if...elif...else statement inside another if...elif...else statement?. Yes! This is called nesting in computer programming. Any number of these statements can be nested inside one another. But this can get confusing, so you must be avoided if we can. Let's look at an example.

In [2]:  num = float(input("Enter a number: ")

if num >= 0:

if num == 0:
print(number,"is neither positive nor negative number.")
else:
print(num, "is a positive number.")
else:
print (num, "is a negative number.")

Above is an example of a nested if statement. You can try and run it, yes it looks similar to our elif example but this is a different approach. You can notice that here we converted the user’s input to a float() using a different style from the int() we used previously.

results matching ""

    No results matching ""