 In this tutorial we are going to learn :

• Python Conditional
• Statements
• If Statements
• If elif Statements
• If else Statements
• If elif else Statements
• Ternary Operator

### Conditional Statements

We have to make decisions in our programs in some conditions.

Python support some logical conditions from mathematics to use it when we need to
make decisions in our conditionals statements

• Equal : x == y
• Not Equal : x != y
• Greater than : x > y
• Greater than and equal to : x >= y
• Less than : x < y
• Less than and equal to : x <= y

In Python, we use if, elif, and else keywords for conditionals statements.

### If Statements

Example :

In this example we want from the user to type his/her age and if his/her age bigger or
equal to 18, the program is going to write   ” You are old enough to vote. “

``````age = int(input("How old are you ?"))

if age >= 18:
print("You are old enough to vote. ")
print("This is always gonna print. ") `````` Note :

In this program you have to be carefull about using of the if statement. After using
colon ( : ) Python automatically leaves four spaces.
If you dont write your code in that area, your code will not be included in the
statement.

### If elif Statement

When if statement doesn’t work, Python will look at elif statement. elif keyword
basically means ” When if statement ( previous ) statement were not True then try this
condition. “

Example :

In the same program if statement is not True and Python will run the elif statement.

``````age = int(input("How old are you ? "))

if age >= 18:
print("You are old enough to vote.")

else:
print("You are not old enough to vote.")

print("Done.") `````` ### If Else Statement

When the if and elif statements don’t work Python will look at other condition. The
other condition is else statement.

Example:

In the same program we used else statement. If age grater or equal to 18, program
will goint to print ” You are old enough to vote ” and if age smaller than 18, program
will run else statement.

``````age = int(input("How old are you ? "))

if age >= 18:
print("You are old enough to vote.")

elif age < 18:
print(f"Please come back in {18-age} years.")
print("Done. ") `````` ### if elif else Statements

Example :

You see our program with if elif and else conditions together.

``````number = int(input("Enter the Number : "))

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

elif number == 0:
print("Number is equal to 0")

else:
print("Number is negative. ")
print("Done. ") `````` ### Ternary Operator

We have a better and cleaner way to use conditional statements with ternary operator.

Example :

You see our program with ternary operator.

``````age = int(input("Enter your age: "))

message = "You can vote. " if age >= 18 else "You can't vote. "
print(message) ``````  0 0 vote
Article Rating
Subscribe
Notify of 0 Comments
Inline Feedbacks
View all comments