Operators
1. Arithmetic Operators
An arithmetic operation in Python involves applying mathematical operators to values (operands). Here's the anatomy of an operation:
Arithmetic Operator Symbols and Their Operations
| Operator | Operation |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| // | Floor Division |
| % | Modulus (Remainder) |
| ** | Exponentiation |
These operators are binary operators, meaning they operate on two operands. Let's explore each operator:
Explanation of Floor Division, Modulus, and Exponentiation
//Floor Division: Divides two numbers and returns the integer quotient, discarding any remainder.
%Modulus: Returns the remainder of a division.
- **
**Exponentiation**: Raises one number to the power of another.
Division vs Floor Division
The / operator performs division and returns the exact result (as a float), while // returns only the integer part (quotient).
Unary Operators: + and -
Unlike the binary operators mentioned above, the + and - operators can also act as unary operators (operating on one operand). They indicate positive or negative numbers.
Variable Usage in Arithmetic Operations
While we've used literals so far, arithmetic operations can also be applied to variables:
2. Relational Operators
Relational operators are used to compare two values. The result is a boolean (True or False).
Relational Operator Symbols and Their Operations
| Operator | Operation |
|---|---|
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| == | Equal to |
| != | Not equal to |
These operators are also binary operators. Examples:
Relational operators are also known as comparison operators, as they compare two operands and return a boolean value. You can assign the result of a comparison to a variable:
Note: The == operator checks for equality and should not be confused with = which is used for assignment.
3. Logical Operators
Logical operators are used to combine multiple boolean expressions. They operate on boolean values and return a boolean result.
Logical Operator Symbols and Their Operations
| Operator | Operation |
|---|---|
not |
Logical negation |
and |
Logical conjunction |
or |
Logical disjunction |
andandorare binary operators.notis a unary operator.
Examples
The not operator inverts the boolean value (True becomes False, and vice versa). Parentheses can be used but are optional:
4. Code Convention
Consider the following lines of code:
Both lines produce the same output. In this course, we will follow the first convention—always adding spaces around operators for readability:
Both are syntactically correct, but adding spaces makes the code more readable.
5. Expressions
An expression in Python is a combination of literals, variables, and operators that evaluates to a value. Here are a few examples:
Each expression evaluates to a value, which has a specific type. For example:
- The first two expressions result in a
float. - The next two result in a
bool.
Arithmetic Expressions
An arithmetic expression evaluates to a numeric value (int or float). Here's an example:
If one operand is a float, the result will also be a float:
Boolean Expressions
A boolean expression results in a bool value (True or False), typically through the use of relational or logical operators:
Similarly, logical operators produce boolean results:
Truth Tables for Logical Operators
A truth table lists all possible outcomes for a logical expression. For example, the truth table for or is:
| X | Y | X or Y |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
By understanding these operators and how they work in expressions, you'll have a strong foundation to write more complex Python programs.