Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In Python, the if
, elif
, and else
keywords are used to create conditional statements. Conditional statements allow your program to make decisions and execute different code blocks based on whether a certain condition is true or false.
Here's a basic tutorial on using if
, elif
, and else
in Python:
if
statement:The if
statement is used to test a condition, and if the condition is true, it executes the code block following the if
statement. The basic syntax is:
if condition: # code block to execute if the condition is true
Example:
age = 18 if age >= 18: print("You are eligible to vote.")
In this example, if the age
variable is greater than or equal to 18, the message "You are eligible to vote." will be printed.
if
-else
statement:The else
statement is used to execute a code block if the condition in the if
statement is false. The basic syntax is:
if condition: # code block to execute if the condition is true else: # code block to execute if the condition is false
Example:
age = 16 if age >= 18: print("You are eligible to vote.") else: print("You are not eligible to vote.")
In this example, since the age
variable is less than 18, the message "You are not eligible to vote." will be printed.
if
-elif
-else
statement:The elif
(short for "else if") statement is used to test multiple conditions. It is used when you need to test more than two conditions. The basic syntax is:
if condition1: # code block to execute if condition1 is true elif condition2: # code block to execute if condition1 is false and condition2 is true else: # code block to execute if all conditions are false
Example:
score = 75 if score >= 90: print("Grade: A") elif score >= 80: print("Grade: B") elif score >= 70: print("Grade: C") elif score >= 60: print("Grade: D") else: print("Grade: F")
In this example, the program checks the value of the score
variable and prints the corresponding grade. Since the score
variable is 75, the message "Grade: C" will be printed.
You can add more elif
statements as needed to test additional conditions. The else
statement is optional, and it's used to handle cases where none of the conditions are true.
How to use if-else in Python:
x = 10 if x > 5: print("x is greater than 5") else: print("x is less than or equal to 5")
Conditional statements in Python with if and else:
age = 20 if age >= 18: print("You are an adult.") else: print("You are a minor.")
Python if-else examples for beginners:
number = 7 if number % 2 == 0: print("The number is even.") else: print("The number is odd.")
Using elif in Python if-else statements:
grade = 75 if grade >= 90: print("A") elif grade >= 80: print("B") elif grade >= 70: print("C") else: print("F")
Nested if-else statements in Python:
x = 5 if x > 0: if x % 2 == 0: print("Positive and even") else: print("Positive and odd") else: print("Non-positive")
Python ternary operator for if-else:
age = 22 result = "Adult" if age >= 18 else "Minor" print(result)
Comparisons and logical operators in Python if-else:
x = 12 if x > 0 and x % 2 == 0: print("Positive and even") elif x > 0 and x % 2 != 0: print("Positive and odd") else: print("Non-positive")
Example code for Python if-else conditional statements:
temperature = 25 if temperature > 30: print("It's hot outside.") else: print("It's not too hot.")