Python Operators Simplified: A Beginner's Guide to Mastery - w9school

Python operators are symbols and keywords used to perform operations on variables and values, making it a versatile language for computations.

Python Operators Simplified: A Beginner's Guide to Mastery - w9school

Python Operators

Operators are used to perform operations on variables and values.

In Python, operators are special symbols or characters that perform operations on operands (values or variables). Python provides a variety of operators to carry out different types of operations, such as arithmetic, comparison, logical, assignment, bitwise, and more. Let's explore the different categories of operators in Python:

1. Python Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

   - Addition: `+`
   - Subtraction: `-`
   - Multiplication: `*`
   - Division: `/`
   - Floor Division: `//` (returns the quotient without the remainder)
   - Modulo: `%` (returns the remainder of division)
   - Exponentiation: `**` (raises a number to the power of another)

Operator Name Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor division x // y

2.Python Comparison Operators

Comparison operators are used to compare two values:

   - Equal to: `==`
   - Not equal to: `!=`
   - Greater than: `>`
   - Less than: `<`
   - Greater than or equal to: `>=`
   - Less than or equal to: `<=`

Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

3. Python Logical Operators

Logical operators are used to combine conditional statements:

   - Logical AND: `and` (returns True if both operands are True)
   - Logical OR: `or` (returns True if either operand is True)
   - Logical NOT: `not` (returns the opposite Boolean value of the operand)

Operator Description Example
and  Returns True if both statements are true x < 5 and  x < 10
or Returns True if one of the statements is true x < 5 or x < 4
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)

4.Python Assignment Operators

Assignment operators are used to assign values to variables:

   - 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: `-=`
   - Multiplication assignment: `*=`
   - Division assignment: `/=`
   - Modulo assignment: `%=`
   - Floor division assignment: `//=`
   - Exponentiation assignment: `**=`

Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3

5. Python Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

   - Bitwise AND: `&` (performs a bitwise AND operation)
   - Bitwise OR: `|` (performs a bitwise OR operation)
   - Bitwise XOR: `^` (performs a bitwise XOR operation)
   - Bitwise NOT: `~` (performs a bitwise NOT operation)
   - 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)

Operator Name Description Example
AND Sets each bit to 1 if both bits are 1 x & y
| OR Sets each bit to 1 if one of two bits is 1 x | y
^ XOR Sets each bit to 1 if only one of two bits is 1 x ^ y
~ NOT Inverts all the bits ~x
<< Zero fill left shift Shift left by pushing zeros in from the right and let the leftmost bits fall off x << 2
>> Signed right shift Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off x >> 2

6. Python Membership operators

In Python, membership operators are used to test whether a value is a member of a sequence or collection, such as a string, list, tuple, or set. These operators return a Boolean value of either True or False. Python provides two membership operators:

1. `in` operator: It checks if a value exists in a given sequence or collection. If the value is found, the operator returns True; otherwise, it returns False.

Example:

```python
fruits = ['apple', 'banana', 'orange']
print('apple' in fruits)  # Output: True
print('grape' in fruits)  # Output: False
```

2. `not in` operator: It checks if a value does not exist in a given sequence or collection. If the value is not found, the operator returns True; otherwise, it returns False.

Example:

```python
fruits = ['apple', 'banana', 'orange']
print('apple' not in fruits)  # Output: False
print('grape' not in fruits)  # Output: True
```​

These membership operators can be used with other data structures as well, such as strings, sets, and dictionaries, to check for the presence or absence of specific elements. They are particularly useful when you need to test for the existence of a value within a collection before performing further operations or making decisions in your code.

7. Python Identity Operators

In Python, identity operators are used to compare the memory addresses (identities) of two objects. These operators determine whether two objects refer to the same memory location or not. Python provides two identity operators:

1. `is` operator: It checks if two objects refer to the same memory location. If the objects are the same, it returns True; otherwise, it returns False.

Example:

```python
x = [1, 2, 3]
y = x  # y refers to the same memory location as x

print(x is y)  # Output: True

z = [1, 2, 3]
print(x is z)  # Output: False
```​

2. `is not` operator: It checks if two objects do not refer to the same memory location. If the objects are different, it returns True; otherwise, it returns False.

Example:

```python
x = [1, 2, 3]
y = [1, 2, 3]

print(x is not y)  # Output: True

z = x
print(x is not z)  # Output: False
```​

It's important to note that identity operators compare the objects' memory addresses, not their values. Even if two objects have the same values, they might still have different memory addresses and, therefore, not be considered identical. These operators are useful when you want to determine whether two variables are referring to the exact same object in memory.These are the basic operators in Python. They allow you to perform a wide range of operations on different types of data, such as numbers, strings, and Boolean values.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow