Here’s a simple example of an if-else
statement in Python:
# Example code to check if a number is even or odd
# User input
num = int(input("Enter a number: "))
# Check if the number is even or odd
if num % 2 == 0:
print(f"{num} is an even number.")
else:
print(f"{num} is an odd number.")
Code language: Python (python)
In this example, the user is prompted to enter a number, and then the program checks if the number is even or odd using the %
operator (which gives the remainder of the division).
If the remainder is zero (i.e., the number is divisible by 2), it means the number is even, and the program prints that it is even. Otherwise, it prints that the number is odd.
What does Elif mean in Python?
In Python, elif
is a keyword that stands for “else if”. It is used in conditional statements to provide an alternative condition to check when the initial if
condition evaluates to False
. Essentially, elif
allows you to chain multiple conditions together and handle different cases based on the evaluation of these conditions.
The syntax for using elif
is as follows:
if condition1:
# Code block to be executed if condition1 is True
elif condition2:
# Code block to be executed if condition1 is False and condition2 is True
elif condition3:
# Code block to be executed if condition1 and condition2 are False and condition3 is True
else:
# Code block to be executed if none of the above conditions are True
Code language: Python (python)
The elif
statements are optional and can appear zero or more times in an if-elif-else
block. When the if
condition is evaluated to False
, Python checks the subsequent elif
conditions one by one until it finds a True
condition or reaches the else
block (if provided). If none of the if
or elif
conditions are True
, the code inside the else
block (if present) will be executed.
Here’s an example:
# Example code to determine the grade based on the score
score = int(input("Enter your score: "))
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print(f"Your grade is: {grade}")
Code language: Python (python)
In this example, we use elif
statements to evaluate different score ranges and assign corresponding grades to the students based on their scores.’
How do you use Elif and or in Python?
You can use elif
and or
in combination to create more complex conditional statements that check multiple conditions. The or
operator is used to combine two or more conditions, and the elif
keyword allows you to specify an alternative condition to check if the previous if
or elif
conditions are not met.
Here’s the syntax for using elif
and or
together:
if condition1:
# Code block to be executed if condition1 is True
elif condition2 or condition3:
# Code block to be executed if condition1 is False and either condition2 or condition3 is True
elif condition4:
# Code block to be executed if condition1, condition2, and condition3 are all False and condition4 is True
else:
# Code block to be executed if none of the above conditions are True
Code language: Python (python)
Let’s see an example of using elif
and or
together:
# Example code to check if a number is positive, negative, or zero
num = float(input("Enter a number: "))
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
elif num == 0:
print("The number is zero.")
else:
print("Invalid input.")
Code language: Python (python)
In this example, we use elif
and or
to check whether the number entered by the user is positive, negative, or zero.
The first if
condition checks if the number is greater than 0, then the subsequent elif
condition with or
checks if the number is less than 0, and finally, the last elif
condition checks if the number is equal to 0.
The else
block is used for any other invalid input that doesn’t fall into the specified conditions.
Read More;
- How to write update query in MySQL in Python?
- Simple Example Python Programs for Practice [Beginners ]
- What is for loop in python with example [10 Examples]
- Multiprocessing Python Example For Loop
- What is a list () in Python With Example
- What is a dictionary in Python example?
- What are the types of variables in Python?
- Can you run a for loop on a dictionary Python?
- Vimrc Example for Python (Vim configuration)
- What is the e function in Python?
- How do you write an automation test script in Python?
- How do you automate daily tasks in Python?