2.4 Arithmetic and Logical Operators
Python provides a wide range of operators that allow you to perform various operations on data, including arithmetic calculations and logical comparisons. Understanding how these operators work is essential for performing mathematical computations and making decisions in your code.
In this section, we will explore:
- Arithmetic Operators
- Logical Operators
- Operator Precedence
- Short-Circuiting in Logical Operators
2.4.1 Arithmetic Operators
Arithmetic operators are used to perform mathematical operations on numeric data types (integers and floats). The common arithmetic operations include addition, subtraction, multiplication, division, and more.
Here is a list of Python's arithmetic operators:
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division (float result) | 5 / 2 |
2.5 |
// |
Floor division (integer result) | 5 // 2 |
2 |
% |
Modulus (remainder) | 5 % 2 |
1 |
** |
Exponentiation (power) | 2 ** 3 |
8 |
Examples of Arithmetic Operations
- Division and Floor Division
Exponentiation
The exponentiation operator (**
) raises the first number to the power of the second number.
x = 2
y = 3
print(x ** y) # Output: 8 (2^3)
Modulus (Remainder)
The modulus operator (%
) returns the remainder when dividing two numbers.
x = 10
y = 3
print(x % y) # Output: 1
Floor Division (//
) returns the largest integer less than or equal to the result (also known as integer division).
x = 10
y = 3
print(x // y) # Output: 3
Division (/
) returns a float, even if the result is a whole number.
x = 10
y = 2
print(x / y) # Output: 5.0
Multiplication
x = 5
y = 4
print(x * y) # Output: 20
Addition and Subtraction
x = 10
y = 3
print(x + y) # Output: 13
print(x - y) # Output: 7
2.4.2 Logical Operators
Logical operators are used to perform logical operations (also called boolean operations) on Boolean values (True
or False
). These operators help control the flow of the program by allowing you to perform comparisons and combine conditions.
Here is a list of Python's logical operators:
Operator | Description | Example | Result |
---|---|---|---|
and |
Logical AND (True if both are True) | True and False |
False |
or |
Logical OR (True if at least one is True) | True or False |
True |
not |
Logical NOT (Inverts the boolean value) | not True |
False |
Examples of Logical Operations
- The
and
operator returnsTrue
only if both conditions areTrue
. Otherwise, it returnsFalse
. - The
or
operator returnsTrue
if at least one of the conditions isTrue
. It returnsFalse
only if both conditions areFalse
. - The
not
operator inverts the boolean value. If the expression isTrue
, it becomesFalse
, and vice versa.
not
Operator
x = True
print(not x) # Output: False
or
Operator
x = 5
print(x > 3 or x > 10) # Output: True (one condition is True)
print(x < 3 or x > 10) # Output: False (both conditions are False)
and
Operator
x = 5
print(x > 3 and x < 10) # Output: True (both conditions are True)
print(x > 3 and x > 10) # Output: False (one condition is False)
Combining Logical Operators
Logical operators can be combined to create complex conditions.
Example:
x = 7
print((x > 5 and x < 10) or (x == 0)) # Output: True
2.4.3 Relational (Comparison) Operators
Relational (comparison) operators are used to compare two values and return True
or False
depending on the result. These operators are commonly used in conjunction with logical operators for decision-making in your code.
Here is a list of Python's relational operators:
Operator | Description | Example | Result |
---|---|---|---|
== |
Equal to | 5 == 5 |
True |
!= |
Not equal to | 5 != 3 |
True |
> |
Greater than | 5 > 3 |
True |
< |
Less than | 5 < 3 |
False |
>= |
Greater than or equal to | 5 >= 5 |
True |
<= |
Less than or equal to | 5 <= 3 |
False |
Examples of Relational Operators
Greater than or Equal to and Less than or Equal to
x = 5
print(x >= 5) # Output: True
print(x <= 4) # Output: False
Greater than and Less than
x = 15
print(x > 10) # Output: True
print(x < 20) # Output: True
Equality and Inequality
x = 10
y = 20
print(x == y) # Output: False
print(x != y) # Output: True
2.4.4 Operator Precedence
When multiple operators are used in an expression, operator precedence determines the order in which the operations are evaluated. Python follows a specific precedence rule to resolve this.
From highest to lowest precedence:
- Parentheses:
()
- Exponentiation:
**
- Unary operators:
+
,-
,not
- Multiplication, Division, Modulus, and Floor Division:
*
,/
,%
,//
- Addition and Subtraction:
+
,-
- Relational operators:
>
,<
,>=
,<=
- Equality operators:
==
,!=
- Logical
and
- Logical
or
Examples of Operator Precedence
Example 1:
x = 5 + 2 * 3 # Multiplication has higher precedence than addition
print(x) # Output: 11
Example 2:
x = (5 + 2) * 3 # Parentheses change the order of evaluation
print(x) # Output: 21
2.4.5 Short-Circuiting in Logical Operators
Python uses short-circuit evaluation for logical operators and
and or
. This means that Python stops evaluating an expression as soon as the result is determined.
and
Short-Circuiting
In the case of and
, if the first condition is False
, Python does not evaluate the second condition because the entire expression can never be True
.
Example:
x = 5
print(x > 10 and x < 20) # Output: False (Python doesn't check the second condition)
or
Short-Circuiting
For or
, if the first condition is True
, Python does not evaluate the second condition because the entire expression will be True
regardless of the second condition.
Example:
x = 5
print(x > 3 or x > 10) #
Output: True (Python doesn't check the second condition)
2.4.6 Summary
- Arithmetic operators allow you to perform basic mathematical operations such as addition, subtraction, multiplication, division, and more.
- Logical operators help you combine multiple conditions, using
and
,or
, andnot
to control the flow of your program. - Relational operators are used to compare values and return a boolean (
True
orFalse
). - Operator precedence determines the order in which operations are performed in an expression.
- Short-circuiting allows Python to skip unnecessary evaluations in logical expressions for efficiency.
Understanding how to use these operators effectively is key to performing calculations, making decisions, and controlling the logic of your Python programs.