How to Use Conditional Statements in Python


Here this example demonstrates how to use ifelse and elif statements in Python.

The syntax is shown as follows:

if expression1:
    statement(s)
elif expression2:
    statement(s)
elif expression3:
    statement(s)
else:
    statement(s)

Source Code

Below is an example of if-elif-else statements:

num = float(input("please input a number: "))
if num > 0:
   print("positive")
elif num == 0:
   print("zero")
else:
   print("negative")

Here is another example on nested if-else statements:

num = float(input("please input a number: "))
if num >= 0:
   if num == 0:
       print("zero")
   else:
       print("positive")
else:
   print("negative")
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments