What are conditions in Python?
Conditions in Python are a fundamental programming tool that allows you to perform various actions depending on whether certain conditions are met or not. The main construct for checking conditions is the if operator. elif and else are also used to create more complex conditional constructions.
Basic constructions of conditions
The if operator
The if operator is a basic construct for checking conditions. If the boolean expression is True, the code block associated with this condition is executed.
x = 10
if x > 5:
print("x is greater than 5")
The if statement...else
The if construction...else allows you to define an alternative block of code that is executed if the condition in the if block is False.
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
The if...elif operator...else
The if...elif construction...else allows you to check multiple conditions sequentially. As soon as one of the conditions becomes true, the corresponding code block is executed, and the remaining checks are skipped.
x = 3
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is 5")
else:
print("x is less than 5")
Nested conditions
You can use conditional statements inside other conditional statements to check more complex conditions. This allows you to create a multi-level decision logic.
x = 8
if x > 5:
if x < 10:
print("x is greater than 5 and less than 10")
else:
print("x is greater than or equal to 10")
else:
print("x is less than or equal to 5")
Comparison operators in Python
Comparison operators are used to compare two values. The result of the comparison is a boolean value True or False.
Operator == (equal to)
Returns True if the values of the two operands are equal.
x = 5
y = 5
print(x == y) # Outputs: True
The operator!= (not equal to)
Returns True if the values are not equal.
x = 5
y = 10
print(x != y) # Outputs: True
Operator > (more than)
Returns True if the value of the left operand is greater than the value of the right operand.
x = 10
y = 5
print(x> y) # Outputs: True
Operator < (less than)
Returns True if the value of the left operand is less than the value of the right operand.
x = 5
y = 10
print(x< y) # Outputs: True
Operator = (greater than or equal to)
Returns True if the value of the left operand is greater than or equal to the value of the right operand.
x = 10
y = 10
print(x >= y) # Outputs: True
The operator <= (less than or equal to)
Returns True if the value of the left operand is less than or equal to the value of the right operand.
x = 5
y = 10
print(x <= y) # Outputs: True
Logical operators
To combine several conditions, the logical operators and, or and not are used.
The and operator
Returns True if both conditions are true.
x = 7
if x > 5 and x < 10:
print("x is greater than 5 and less than 10")
The or operator
Returns True if at least one of the conditions is true.
x = 3
if x < 5 or x > 10:
print("x is less than 5 or greater than 10")
The not operator
Returns True if the condition is false.
x = 5
if not x == 10:
print("x is not equal to 10")
Conditional expressions (ternary operator)
Python supports the ternary operator to fulfill simple conditions in a single line. This is a compact way to record conditions.
x = 5
result = "x is greater than 10" if x > 10 else "x is not more than 10"
print(result)
Practical examples of using conditions
Determining an even or odd number
num = 4
if num % 2 == 0:
print(f"{num} is an even number")
else:
print(f"{num} is an odd number")
Determination of positive, negative or zero
num = -5
if num > 0:
print(f"{num} is a positive number")
elif num < 0:
print(f"{num} is a negative number")
else:
print(f"{num} is zero")
Age verification
age = 25
if age >= 18 and age < 65:
print("A person of working age")
else:
print("A person of working age or a pensioner")
Temperature check
temperature = 20
status = "warm" if temperature > 15 else "cold"
print(status)
Abbreviated logical operators
When you have simple conditions, you can combine them into one line to reduce the amount of code.
x = 5
y = 10
# Instead of nested conditions
if x == 5:
if y == 10:
print("x is 5 and y is 10")
# You can use the logical operator
if x == 5 and y == 10:
print("x is 5 and y is 10")
# Can be shortened to one line
if x == 5 and y == 10: print("x is 5 and y is 10")
Features of working with conditions
Operator priority
In Python, there is a certain order of execution of operators. First, the comparison operators are executed, then the logical operators not, and, or.
Short circuit
Python uses a short-circuit mechanism for logical operators. This means that if the result of an expression can be determined from the first operand, the second operand is not evaluated.
x = 0
if x != 0 and 10 / x > 5: # The second condition will not be fulfilled
print("Condition met")
Checking availability
Python provides additional operators in and not in for application developers.
fruits = ["apple", "ban", "apelson"]
if "apple" is in fruits:
print ("Just see")
Here is a table with Python operators:
Operator controls
operator
| The | Name | Description | Example |
|---|---|---|---|
== |
Equal to | Confirms the equality of values | 5 == 5 True |
!= |
Wrong | Approves the new value | 5 != 3 True |
| I'm changing | Check if you are changing the left value | 3<5 True |
|
> |
More | Asserts that this is the left value | 5 > 3 True |
| All or nothing | Claims that this is either a completely left-hand value | 3<= 5 True |
|
>= |
More or less | Checks whether the main or completely left value | 5 >= 5 True |
Logical operators
operator NameDescriptionExample
| The | |||
|---|---|---|---|
and |
And (logical and) | It's true because it's a country | True and false False |
or |
OR (logical OR) | It's true, but it's still true | True or false True |
not |
NOT (logical NOT) | Enters a boolean value | is incorrect False |
Telecom operators
operator NameDescriptionExample
| The | |||
|---|---|---|---|
in |
The application | To check whether to stay in the position | 'a' in 'apple' The truth |
is missing from |
Provision of services | He believes, he can't resist making comments in the comments | 'z' is not in 'apple' True |
Modern operators
operator NameDescriptionExample
| The | |||
|---|---|---|---|
is |
Reality | Check if the objects are the same object | a is b |
is not |
Does not exist | Checks whether objects are objects of each other and thus an object | a is not b |
Arithmetic operators
operator NameDescriptionExample
| The | |||
|---|---|---|---|
+ |
Addendum | Stores two records | 5 + 3 → 8 |
- |
Extraction | Outputs the second value from the first | 5 - 3 → 2 |
* |
Multiplication | Multiplies two values | 5 * 3 15 |
/ |
Separation | Do the first thing on the second (restore floating) | 6/2 3.0 |
// |
Intelligent separation | Sharing and transforming privacy | 7 // 2 3 |
% |
Division status | Returns the division status | 7 % 2 1 |
** |
Getting to know each other in style | Create something new in style | 2 ** 3 → 8 |
Operators get to know each other
operator NameDescriptionExample
| The | |||
|---|---|---|---|
= |
Introduction | Assigns a value to a constant | x = 5 |
+= |
Offer with offer | x += 3 is equal to x = x + 3 |
x += 3 |
-= |
Receiving with an invitation | x -= 3 is equal to x = x - 3 |
x -= 3 |
*= |
Multiplication with assignment | x*= 3 is equal to x = x * 3 |
x *= 3 |
/= |
Getting to know the invitation | x /= 3 is equal to x = x / 3 |
x /= 3 |
//= |
Intelligent interaction with an invitation | x //= 3 is equal to x = x // 3 |
x //= 3 |
%= |
Stop with an invitation | x %= 3 is equal to x = x %3 |
x %= 3 |
**= |
Keep getting to know each other | x **= 3 is equal to x = x ** 3 |
x **= 3 |