Conditional statements in Python allow you to execute blocks of code only if a certain condition is
True
.
if
Statement
The
if
statement is used to check a condition and execute a block of code if the condition is
True
.
1 2 | if condition: # code to be executed if condition is True |
You can also use the
elif
keyword to check additional conditions if the first condition is
False
.
1 2 3 4 | if condition1: # code to be executed if condition1 is True elif condition2: # code to be executed if condition1 is False and condition2 is True |
You can use as many
elif
clauses as you need. If none of the conditions are
True
, you can use the
else
clause to specify code to be executed.
1 2 3 4 5 6 | if condition1: # code to be executed if condition1 is True elif condition2: # code to be executed if condition1 is False and condition2 is True else: # code to be executed if condition1 and condition2 are False |
Here is an example of an
if
statement:
1 2 3 | x = 10 if x > 5: print("x is greater than 5") |
This will print “x is greater than 5” because the condition (x > 5) is
True
.
ternary
operator in Python
The ternary operator is a shorthand way to write an
if
statement. It has the following syntax:
1 | value_if_true if condition else value_if_false |
Here is examples of a ternary operator:
Example1
1 2 3 | x = 10 result = "x is greater than 5" if x > 5 else "x is not greater than 5" print(result) |
This will print “x is greater than 5” because the condition (x > 5) is
True
.
Example 2
1 2 3 | x = 10 result = "x is even" if x % 2 == 0 else "x is odd" print(result) |
This will print “x is even” because the condition (x % 2 == 0) is
True
.
bool()
function
The
bool()
function is used to evaluate a value as a boolean. It returns
True
if the value is considered “truthy,” and
False
if it is considered “falsy.”
Here are some examples of values that are considered “truthy”:
- True
- Non-empty strings and lists
- Non-zero numbers
Here are some examples of values that are considered “falsy”:
- False
- None
- 0
- Empty strings and lists
You can use the
bool()
function to test whether a value is “truthy” or “falsy” in a conditional statement.
1 2 3 4 | if bool(value): # code to be executed if value is truthy else: # code to be executed if value is falsy |
Here is an example of using the
bool()
function:
1 2 3 4 5 | x = 10 if bool(x): print("x is truthy") else: print("x is falsy") |
This will print “x is truthy” because the value of x (10) is considered “truthy.”
Comparison Operators In Python
We use comparison operators to compare two values and determine if they are equal, unequal, greater than, less than, etc.
Here are some common comparison operators:
-
==
: equal to -
!=
: not equal to -
>
: greater than -
<
: less than -
>=
: greater than or equal to -
<=
: less than or equal to
Here are some examples of using comparison operators in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | x = 10 y = 20 if x == y: print("x is equal to y") else: print("x is not equal to y") if x > y: print("x is greater than y") else: print("x is not greater than y") if x < y: print("x is less than y") else: print("x is not less than y") |
This will print:
1 2 3 | x is not equal to y x is not greater than y x is less than y |
Logical Operators in Python
We use logical operators to combine multiple conditions.
Here are some common logical operators:
-
and
: both conditions must beTrue
-
or
: at least one condition must beTrue
-
not
: negates a condition
Here are some examples of using logical operators in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | x = 10 y = 20 if x > 5 and y > 5: print("x is greater than 5 and y is greater than 5") else: print("x is not greater than 5 or y is not greater than 5") if x > 5 or y > 5: print("x is greater than 5 or y is greater than 5") else: print("x is not greater than 5 and y is not greater than 5") if not(x > 5): print("x is not greater than 5") else: print("x is greater than 5") |
This will print:
1 2 3 | x is greater than 5 and y is greater than 5 x is greater than 5 or y is greater than 5 x is not greater than 5 |
Here, you have learned Conditional Statements(if, elif and else) in Python.