# Operators in Python

## Getting Started

As your knowledge base in Python grows, you'll gain the ability to manipulate data types and create flexible expressions. Logically, we need operators to perform such actions and sway the functioning of our Python programs as we deem necessary. Simply put, we use operators to perform operations on values, variables, and data structures, called operands, and control the flow of a program. We'll explore the different types of operators available in Python and learn how to properly use them.

## Python Arithmetic Operators

You'll likely be familiar with most of these. Just be aware of the symbol used in the rudimentary examples, as we'll be using them in more complex structures later on.

Arithmetic operators are used to performing basic mathematical operations such as addition, subtraction, multiplication, and division. They are used to perform calculations and make numerical comparisons.

``````# Addition
first_num = 5
second_num = 10
result = first_num + second_num
print(result) # Output: 15

# Subtraction
first_num = 15
second_num = 5
result = first_num - second_num
print(result) # Output: 10

# Multiplication
first_num = 10
second_num = 5
result = first_num * second_num
print(result) # Output: 50

# Division
first_num = 50
second_num = 5
result = first_num / second_num
print(result) # Output: 10.0
``````
SymbolOperation
-Subtraction
*Multiplication
/Division
%Modulus
**Exponentiation
//Floor division

## Python Assignment Operators

We've already explained the use of the equal sign (`=`) in our Basics of Variables in Python and we saw several examples of how we assign values to variables.

You'll notice several shorthand assignment operators, or augmented assignments, allowing us to perform an operation and then assign the result to a variable with a single line of code. So, let's go over them and observe how they neatly store and manipulate values in memory.

``````# Basic assignment to a variable
num = 10

# Basic assignment operator
value = 25

value += 10
print(value) # Output: 35

# Subtraction assignment operator
value -= 15
print(value) # Output: 20

# Multiplication assignment operator
value *= 2
print(value) # Output: 40

# Division assignment operator
value /= 4
print(value) # Output: 10.0

# Modulus assignment operator
value %= 3
print(value) # Output: 1.0

# Exponentiation assignment operator
value = 7
value **= 3
print(value) # Output: 343

# Floor division assignment operator
value = 100
value //= 7
print(value) # Output: 14
``````
SymbolOperation
=Assignment
-=Augmented Subtraction Assignment
*=Augmented Multiplication Assignment
/=Augmented Division Assignment
%=Augmented Remainder Assignment with the Modulus Operator
**=Augmented Exponent Assignment
//=Augmented Floor Division Assignment

## Comparison Operators

With comparison operators, we can compare values and variables in Python. They return a Boolean value of either True or False based on the comparison made. Comparison operators are oftentimes used in conditional statements to control the flow of a program.

``````# Equal to
first_num = 15
second_num = 20
print(first_num == second_num) # Output: False

# Not equal to
first_num = 15
second_num = 20
print(first_num != second_num) # Output: True

# Greater than
first_num = 15
second_num = 20
print(first_num > second_num) # Output: False

# Less than
first_num = 15
second_num = 20
print(first_num < second_num) # Output: True

# Greater than or equal to
first_num = 15
second_num = 20
print(first_num >= second_num) # Output: False

# Less than or equal to
first_num = 15
second_num = 20
print(first_num <= second_num) # Output: True
``````
SymbolOperation
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

## Bitwise Operators

Bitwise operators perform operations on binary representations of numbers in Python. They are often used for low-level operations, such as setting or checking individual bits within a number.

``````# Binary AND - "&"
first_num = 75
second_num = 35
result = first_num & second_num
print(result) # Output: 3
# The binary representation of 75 is 1001011 and 35 is 100011.
# When performing a binary AND operation on these two numbers,
# only the bits that are set to 1 in both numbers are set to 1 in the result, which is 3.

# Binary OR - "|"
first_num = 70
second_num = 30
result = first_num | second_num
print(result) # Output: 94
# The binary representation of 70 is 1000110 and 30 is 1110.
# When performing a binary OR operation on these two numbers,
# the bits that are set to 1 in either number are set to 1 in the result, which is 94.

# Binary XOR - "^"
first_num = 60
second_num = 40
result = first_num ^ second_num
print(result) # Output: 20
# The binary representation of 60 is 111100 and 40 is 101000.
# When performing a binary XOR operation on these two numbers,
# the bits that are set to 1 in only one of the two numbers are set to 1 in the result, which is 20.

# Binary Ones Complement - "~"
first_num = 50
result = ~first_num
print(result) # Output: -51
# The binary representation of 50 is 110010.
# When performing a binary ones complement operation on this number,
# all the bits are inverted, resulting in a negative number representing the complement of 50, which is -51.

# Binary Left Shift - "<<"
first_num = 50
result = first_num << 2
print(result) # Output: 200
# When performing a binary left shift operation on this number,
# the bits are shifted to the left by the specified number of positions (2 in this case),
# and zeros are filled in from the right, resulting in 200, which is 4 times the original value of 50.

# Binary Right Shift - ">>"
first_num = 50
result = first_num >> 2
print(result) # Output: 12
# When performing a binary right shift operation on this number,
# the bits are shifted to the right by the specified number of positions (2 in this case),
# and copies of the leftmost bit (0 in this case) are filled in from the left,
# resulting in 12, which is a quarter of the original value of 50.
``````
OperationExplanation
Binary ANDSets each bit to 1 if both corresponding bits in the operands are 1
Binary ORSets each bit to 1 if at least one of the corresponding bits in the operands is 1
Binary XORSets each bit to 1 if only one of the corresponding bits in the operands is 1
Binary Ones ComplementInverts all the bits of the operand
Binary Left ShiftShifts the bits of the operand to the left by the specified number of positions, filling in with zeros from the right
Binary Right ShiftShifts the bits of the operand to the right by the specified number of positions, filling in with copies of the leftmost bit from the left

## Logical Operators

We can leverage logical operators to carry out operations on Boolean values in Python. They grant us decision-making based on multiple conditions and control the flow of a program.

``````# Logical AND
first_condition = True
second_condition = False
result = first_condition and second_condition
print(result) # Output: False

# Logical OR
first_condition = True
second_condition = False
result = first_condition or second_condition
print(result) # Output: True

# Logical NOT
condition = False
result = not condition
print(result) # Output: True
``````

## Specific Python Operators

In addition to the ones we saw, Python has a few operators that are specific to the language.

### Identity Operators

Identity operators are used to comparing the memory locations of two objects in Python. They enable us to find out if two objects are the same object in memory, or if they are different objects with the same values.

``````# Equal identity
first_obj = [1, 2, 3]
second_obj = [1, 2, 3]
print(first_obj is second_obj) # Output: False

# Not equal identity
first_obj = [1, 2, 3]
second_obj = [1, 2, 3]
print(first_obj is not second_obj) # Output: True
``````

### Membership Operators

Membership operators are used to testing if a value or variable is a member of a sequence, such as a list, tuple, or string. With them, we can determine whether an object is present in a sequence.

``````# Member of
numbers = [1, 2, 3, 4, 5]
print(3 in numbers) # Output: True

# Not member of
numbers = [1, 2, 3, 4, 5]
print(6 not in numbers) # Output: True
``````

## Python Operator Precedence

Just like in mathematics, operators in Python have a specific order of precedence that determines the order in which operations are performed.

1. Exponentiation (**)

2. Complement, unary plus, and minus (e.g. ~x, +x, -x)

3. Multiplication, division, floor division, and modulus (e.g. *, /, //, %)

4. Addition and subtraction (e.g. +, -)

5. Bitwise shift operations (e.g. <<, >>)

6. Bitwise AND (e.g. &)

7. Bitwise XOR (e.g. ^)

8. Bitwise OR (e.g. |)

9. Comparison operators (e.g. ==, !=, >, <, >=, <=)

10. Membership operators (e.g. in, not in)

11. Identity operators (e.g. is, is not)

12. Logical NOT (e.g. not)

13. Logical AND (e.g. and)

14. Logical OR (e.g. or)

Lacking knowledge of the precedence order in Python can affect the outcome of a calculation or comparison. If we're unsure of the order of precedence, it's always a good idea to use parentheses to explicitly define the order of operations.

## Final Thoughts

We've delved into the various types of operators available in Python and gained enough information to start experimenting with them. They can be used in a variety of situations, from simple mathematical operations to complex decision-making processes. Once you begin creating beginner projects you'll inevitably find yourself using most of these operators in expressions. Don't hesitate to come back again when you need to refresh your memory. The point of programming isn't to resort to rote learning.