Operators are symbols used to perform operations on variables and values.
`=` is an assignment operator used to assign a value to a variable. For instance, `x = 10` means `x` now holds the value `10`. `==` is a comparison operator used to check if two values are equal.
Arithmetic operators perform basic mathematical operations. Examples include `+` for addition (e.g., `4 + 3` results in `7`), `-` for subtraction (e.g., `10 - 5` results in `5`), `*` for multiplication (e.g., `2 * 6` results in `12`), and `/` for division (e.g., `8 / 4` results in `2.0`). They are like the basic operations you perform with a calculator.
In Python 3, `5 / 2` performs floating-point division, giving `2.5`. This means it divides `5` by `2` and includes the decimal part. For integer division (without decimals), use `//`, so `5 // 2` results in `2`. This is useful when you need a whole number, like dividing a pizza among people without fractional slices.
Comparison operators compare values and return `True` or `False`. Examples include `==` for equality (e.g., `3 == 3` results in `True`), `!=` for inequality (e.g., `3 != 4` results in `True`), `>` for greater than (e.g., `5 > 2` results in `True`), and `<` for less than (e.g., `3 < 4` results in `True`). They help determine relationships between values, like checking if a student's score is higher than a passing grade.
The `and` operator checks if both conditions are `True`.
The `or` operator returns `True` if at least one condition is `True`.
The `not` operator negates a condition.
Bitwise operators perform operations on binary representations of integers. Examples include `&` (AND, e.g., `5 & 3` results in `1`), `|` (OR, e.g., `5 | 3` results in `7`), `^` (XOR, e.g., `5 ^ 3` results in `6`), `~` (NOT, e.g., `~5` results in `-6`), `<<` (left shift, e.g., `2 << 2` results in `8`), and `>>` (right shift, e.g., `16 >> 2` results in `4`). They are used in low-level programming tasks, like manipulating binary data.
The `<<` operator shifts bits to the left.
The `**` operator performs exponentiation, which raises a number to the power of another.
The `%` operator returns the remainder of a division.
The `in` operator checks if a value exists within a container, like a list or a string.
The `is` operator checks if two references point to the same object in memory.
The `+=` operator adds a value to a variable and assigns the result back to the variable.
Logical operators include `and`, `or`, and `not`, used to combine or negate conditions.
The `//` operator performs integer (floor) division, which returns the largest integer less than or equal to the result.
The `&` operator performs a bitwise AND operation.
The `|` operator performs a bitwise OR operation.
The `^` operator performs a bitwise XOR operation.
The `~` operator performs a bitwise NOT operation, inverting all bits.
The `<<` operator performs a left bitwise shift.
The `>>` operator performs a right bitwise shift.
The `**=` operator raises a variable to the power of a given number and assigns the result back.
The `<<=` operator performs a left shift operation and assigns the result.
The `>>=` operator performs a right shift operation and assigns the result.
The `:=` operator, known as the walrus operator, assigns a value to a variable as part of an expression.
The `del` operator removes an object or item.
The `@` operator is used for decorators, which modify functions or methods.
The `*` operator unpacks a sequence into individual arguments.
The `**` operator unpacks a dictionary into keyword arguments.
In list comprehensions, the `or` operator can be used to provide a default value if an expression is false.
In list comprehensions, the `and` operator can be used to combine conditions.
The `in` operator checks if a key exists in a dictionary.
The `not in` operator checks if a value is not present in a sequence.
The `**` operator unpacks a dictionary into keyword arguments in a function call.
The `:` operator is used for slicing sequences.
In function definitions, `*args` collects extra positional arguments into a tuple, while `**kwargs` collects extra keyword arguments into a dictionary.
The `+=` operator updates the value of a variable by adding to it, while `+` creates a new value.