DEV Community

loading...

Python conditions (if, elif, else statement)

oladejo abdullahi
I am web developer and data scientist
・6 min read

conditional statements in python
Quite often in programs we only want to do something provided something else is true. Python’s if statement is what we need.

Example

Let’s ask a user to input a number greater than 5 and the program tells them if they are correct. To see if the user do input the right number or not something called an if statement will be needed. let's see the code below

UsersNumber = eval(input('Enter a number greater than 5: '))
#we use this to collect the users number
if UsersNumber>5: 
  print('You input right number!')
Enter fullscreen mode Exit fullscreen mode

OUTPUT1

Enter a number greater than 5: 6
You input right number!
Enter fullscreen mode Exit fullscreen mode

OUTPUT2

Enter a number greater than 5: 3
Enter fullscreen mode Exit fullscreen mode

The syntax of the if statement is a lot like the for statement only that there is a colon at the end of the if condition and the following line or lines are indented. The lines that are indented will be executed only if the condition is true. Once the indentation is done with, the if block work is completed.

But won't it be pretty interesting If the user input the wrong and the programs tell him it is wrong input? Yeah! that will be nice. so, we can add to the if statement as follows:

UsersNumber = eval(input('Enter a number greater than 5: '))
#we use this to collect the users number
if UsersNumber>5: 
  print('You input right number!')
else:
   print('Sorry! You input a wrong number')
Enter fullscreen mode Exit fullscreen mode

We have added an else statement, which is like an “otherwise.”
with the above example we can divided the step taken while using if statement as follow.
step1: write if followed by the conditional expression ending with column(:)
step2: write the code to run if the condition in step1 true provided that all the codes get indent before them
step3: if the condition in step1 false write 'else:' with column without indent before it or parallel to if condition.
step4: write the code to run if the condition is false with each of them having indents before them.
step5: check the codes didn't you forget the indent and column? if yes then enjoy!

Example2

Try to use the above step to build a simple guessing game.
where The computer will pick random number,and the player tries to guess the number, your program tells them if they are correct.
solution
if you understand this problem well it is very easy. all we need it to get random number, ask user to input their guess, if guess equal to the random number the program say correct and that is all. here is the code:

from random import randint #to import randomint function so that it work
computerNumber = randint(1,10)#to pick number between 1 and 10
userGuess = int(input('Enter your guess: '))#for user input
if userGuess==computerNumber:#check if user-guess is same as computer-number
    print('You got it!')
Enter fullscreen mode Exit fullscreen mode

Now run the code you should have something like this
OUTPUT1

Enter your guess: 6
Enter fullscreen mode Exit fullscreen mode

OUTPUT2

Enter your guess: 3 
Enter fullscreen mode Exit fullscreen mode

OUTPUT3

Enter your guess: 5
You got it!
Enter fullscreen mode Exit fullscreen mode

But there is a little flawless in our game. what was that?Umh? yeah! we don't give any reply when the user guess is wrong. so what do we need?? exactly we need else. so we write the codes as

from random import randint
computerNumber = randint(1,10)#to pick number between 1 and 10
userGuess = int(input('Enter your guess: '))#for user input
if userGuess==computerNumber:#check if user-guess is same as computer-number
    print('You got it!')
else:
   print('you are wrong ')
   print('the number is',computerNumber)#to state the answer
Enter fullscreen mode Exit fullscreen mode

OUTPUT

Enter your guess: 6
you are wrong 
the number is 4
Enter fullscreen mode Exit fullscreen mode

wow we just build a simple game with if statement.
There are some other tools we might need while working with condition they are called conditional operators they are:
Conditional operators
The comparison operators are :
Equals (==): a == b to check if a and b equal
Not Equals(!=): a != b check if and b is not equal
Less than(<): a < b check if a less than b
Less than or equal to(<=): a <= b check if a less than or equal to b
Greater than(>): a > b check if a greater than b
Greater than or equal to(>=): a >= b check if a greater than or equal to b

Examples:

Expression Description
if x>3: if x is greater than 3
if x>=5: if x is greater than or equal to 5
if x==2: if x is 2
if x!=2: if x is not 2
There are three additional operators used to construct more complicated conditions: 'and', 'or', and 'not'.
and : this is used to combine two condition. the code below it will run if and only if the two statement joined by and are both true.

Example

compare the age of the 3 friends below:
Code:

MaxwizAge=45
Maxwell=30
Moses=45
if Maxwiz>Maxwell and Maxwiz>Moses:
    print('Maxwiz is older than both Maxwell and Moses')
Enter fullscreen mode Exit fullscreen mode

OUTPUT

Maxwiz is older than both Maxwell and Moses
Enter fullscreen mode Exit fullscreen mode

OR: this is used to test two condition statement if atlest one of them is true the code after it will be executed.

Example

MaxwizAge=45
Maxwell=30
Moses=45
if Maxwiz>Maxwell or Maxwi>Moses:
    print('Maxwiz is not the youngest')
Enter fullscreen mode Exit fullscreen mode

Not: this is used to negate the statement after it i.e it will run the code under it if the statement after it is not true
Example

if not(Maxwiz>Maxwell and Maxwiz>Moses):
    print('Maxwiz is the youngest one')
Enter fullscreen mode Exit fullscreen mode

Now Let’s try to write a program that tell the grade of a user when they input their score such that
80 and above is 'EXCELLLENT'
70 to 79 is 'VERY GOOD'
60 to 69 is 'GOOD'
50 to 59 is 'PASS'
40 to 49 is 'FAIL'
our code will be :

score=int(input('what is your score'))
if score>=80 :
    print('exellent ')
if score>=70 and score<80:
    print('verygood ')
if score>=60 and score<70:
    print('good ')
if score>=50 and score<60:
    print('pass ')
if score>=40 and score<50:
    print('fail ')
Enter fullscreen mode Exit fullscreen mode

Output1

what is your score66
good
Enter fullscreen mode Exit fullscreen mode

Output2

what is your score 56
excellent
Enter fullscreen mode Exit fullscreen mode

output3

what is your score 40
fail
Enter fullscreen mode Exit fullscreen mode

the program above work perfect? yeah! But there is a good news for you,guess what? a perfect way of doing that leading us to something called 'ELIF'
ELIF
elif is used to state another condition if the first one is not true. so elif provide another condition to number of condition you wish.
so the program above can also be as followed:

if score>=80:#check if greater than or equal to 80
print('exellent') 
elif score>=70:#if not greater than or equal 80 check if greater than or equal 70
print('verygood')
elif score>=60:#if not greater than or equal 70 check if greater than or equal 60
print('good')
elif score>=50:#if not greater or equal60 check if greater than or equal 50
print('pass')
elif score>=40:#if not greater than or equal 50 check if greater than or equal 40
print('fail')
else: #if not greater than or equal 40 then
    print('advice to withdraw')
Enter fullscreen mode Exit fullscreen mode

Output1

what is your score 63
good
Enter fullscreen mode Exit fullscreen mode

Output2

what is your score 52
excellent
Enter fullscreen mode Exit fullscreen mode

output3

what is your score 40
fail
Enter fullscreen mode Exit fullscreen mode

If condition is very interesting and can never be miss out of programming as every time we make use of it! did you enjoy this then consider follow me so that you don't miss any of my article. Alsi try to solve the following puzzle and comment below.

PUZZLE

Ask the user to enter a temperature in Celsius. The program should print a message based
on the temperature:
• If the temperature is less than -273.15, print that the temperature is invalid because it is
below absolute zero.
• If it is exactly -273.15, print that the temperature is absolute 0.
• If the temperature is between -273.15 and 0, print that the temperature is below freezing.
• If it is 0, print that the temperature is at the freezing point.
• If it is between 0 and 100, print that the temperature is in the normal range.
• If it is 100, print that the temperature is at the boiling point.
• If it is above 100, print that the temperature is above the boiling point.
enjoy coding! and merry new year in advance!

Discussion (0)

Forem Open with the Forem app