1. What is Python operators
Python operators are fundamental components of the Python programming language. They are symbols or special characters that enable developers to perform specific actions or operations on variables, values, or expressions. These operators act as building blocks for manipulating data and controlling program flow.
Python offers a comprehensive set of operators, each designed to serve a specific purpose. These operators can be classified into various categories, such as arithmetic operators, assignment operators, comparison operators, logical operators, and bitwise operators. Each category caters to different aspects of computation and programming logic.
2. Arithmetic operators
Arithmetic operators are used to perform basic mathematical calculations. They include addition, subtraction, multiplication, division, floor division, etc. These operators allow for straightforward numerical computations and are essential for tasks involving calculations and formula-based operations:
- Addition (+): Adds two operands.
- Subtraction (-): Subtracts the right operand from the left operand.
- Multiplication (*): Multiplies two operands.
- Division (/): Divides the left operand by the right operand (resulting in a float).
- Floor Division (//): Divides the left operand by the right operand and returns the integer quotient.
- Modulus (%): Returns the remainder of the division.
- Exponentiation (**): Raises the left operand to the power of the right operand.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
x = 10 y = 3 # Addition result = x + y # 13 # Subtraction result = x - y # 7 # Multiplication result = x * y # 30 # Division result = x / y # 3.3333333333333335 # Floor Division result = x // y # 3 # Modulus result = x % y # 1 # Exponentiation result = x ** y # 1000 |
3. Assignment Operators
Assignment operators are used to assign values to variables. The most common assignment operator is the equals sign (=), which assigns the value on the right side to the variable on the left side. Additionally, there are compound assignment operators such as +=, -=, *=, /=, %=, and **=, which combine arithmetic operations with assignment:
- Assignment (=): Assigns a value to a variable.
- Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
- Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
- Multiplication Assignment (*=): Multiplies the right operand with the left operand and assigns the result to the left operand.
- Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
- Modulus Assignment (%=): Performs modulus on the left operand with the right operand and assigns the result to the left operand.
- Exponentiation Assignment (**=): Raises the left operand to the power of the right operand and assigns the result to the left operand.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
x = 5 # Addition Assignment x += 3 # x = x + 3 => 8 # Subtraction Assignment x -= 2 # x = x - 2 => 6 # Multiplication Assignment x *= 4 # x = x * 4 => 24 # Division Assignment x /= 3 # x = x / 3 => 8.0 # Modulus Assignment x %= 5 # x = x % 5 => 3 # Exponentiation Assignment x **= 2 # x = x ** 2 => 9 |
4. Comparison Operators
Comparison operators are used to compare values and determine their relationship. They include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). These operators are useful for making decisions based on conditions and for creating conditional statements like if-else and while loops.
- Equal to (==): Checks if the values of two operands are equal.
- Not equal to (!=): Checks if the values of two operands are not equal.
- Greater than (>): Checks if the left operand is greater than the right operand.
- Less than (<): Checks if the left operand is less than the right operand.
- Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
- Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
x = 10 y = 5 # Equal to result = x == y # False # Not equal to result = x != y # True # Greater than result = x > y # True # Less than result = x < y # False # Greater than or equal to result = x >= y # True # Less than or equal to result = x <= y # False |
5. Logical Operators
Logical operators are used to combine and manipulate logical values (True and False) and Boolean expressions. The logical AND (and), logical OR (or), and logical NOT (not) operators enable developers to perform logical operations, such as checking multiple conditions or negating a condition, which are fundamental for control flow and decision-making in programming.
- Logical AND (and): Returns True if both operands are True.
- Logical OR (or): Returns True if at least one of the operands is True.
- Logical NOT (not): Returns the opposite of the operand's logical value.
Example
1 2 3 4 5 6 7 8 9 10 11 |
x = True y = False # Logical AND result = x and y # False # Logical OR result = x or y # True # Logical NOT result = not x # False |
Bitwise Operators
Bitwise operators are used to manipulate individual bits within binary representations of data. They include bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise NOT (~), left shift (<<), and right shift (>>). These operators are primarily used in scenarios that involve low-level programming, bit manipulation, or working with binary data.
- Bitwise AND (&): Performs a bitwise AND operation on the binary representations of two operands.
- Bitwise OR (|): Performs a bitwise OR operation on the binary representations of two operands.
- Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation on the binary representations of two operands.
- Bitwise NOT (~): Performs a bitwise NOT operation on the binary representation of the operand.
- Left Shift (<<): Shifts the bits of the left operand to the left by the number of positions specified by the right operand.
- Right Shift (>>): Shifts the bits of the left operand to the right by the number of positions specified by the right operand.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
x = 10 # Binary: 1010 y = 6 # Binary: 0110 # Bitwise AND result = x & y # 2 (Binary: 0010) # Bitwise OR result = x | y # 14 (Binary: 1110) # Bitwise XOR result = x ^ y # 12 (Binary: 1100) # Bitwise NOT result = ~x # -11 (Binary: -1011) # Left Shift result = x << 2 # 40 (Binary: 101000) # Right Shift result = x >> 1 # 5 (Binary: 101) |
Younes Derfoufi
CRMEF OUJDA