# Python Basic – How to use Operators in Python

Home /

## Python Operators

Operators in Python are used to perform various operations on variables and values. They are an essential part of the language and are used to control the flow of the program, perform calculations, and make decisions.

Python divides the operators into the following groups:

• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Bitwise operators

### Arithmetic Operator

These operators are used to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. They are essential for performing mathematical calculations and are used in a wide range of applications. Mathematical operators are used to performing any simple mathematical operation with numeric values:

Example:

```   >>> P = 38
>>> Q = 10
>>> print( P + Q )
48
>>> print( P - Q )
28
>>> print( P * Q )
380
>>> print( P / Q )
3.8
>>> print( P // Q )
3
>>> print( P % Q )
8
>>> print( P ** Q )
6278211847988224
>>>
```

### Assignment Operator

Assignment operators are used to assigning a value to a variable. The basic assignment operator is =, which assigns the value on the right side of the operator to the variable on the left side. There are also shorthand assignment operators that allow you to perform an operation and an assignment in one step. To assign values to variables, assignment operators and in-place operators are used:

Example:

```x = 5
print(x)

x += 2 # equivalent to x = x + 2
print(x)

x -= 2 # equivalent to x = x - 2
print(x)

x *= 2 # equivalent to x = x * 2
print(x)

x /= 2 # equivalent to x = x / 2
print(x)

x %= 2 # equivalent to x = x % 2
print(x)

```

Output:

``````5
7
5
10
5.0
1.0``````

### Comparison Operators

These operators are used to compare values and are essential for making decisions in control flow statements such as if-else statements. They are used to check if a value is equal to, greater than, or less than another value. Comparison operators are used to compare two values:

Example:

```x = 5
y = 3
print(x == y) # False

x = 5
y = 3
print(x != y) # True

x = 5
y = 3
print(x > y) # True

x = 5
y = 3
print(x < y) # False

x = 5
y = 3
print(x >= y) # True

x = 5
y = 3
print(x <= y) # False

x = 5
print(1<x<10) # True

```

Output:

``````False
True
True
False
True
False
True``````

It’s important to keep in mind that when comparing different types of data, such as integers and strings, Python follows a specific set of rules for type coercion. For example, when comparing a string and an integer, Python will automatically convert the string to an integer if it can, and then perform the comparison.

### Logical Operators

Logical operators are used to combining conditions and make decisions. They are used to check if multiple conditions are true or false. There are three main logical operators and, or, not. Logical operators are used to combine conditional statements:

Example:

```x = 5
y = 10
if x > 0 and y > 0:
print("x and y are positive")

x = 5
y = -10
if x > 0 or y > 0:
print("x or y is positive")

x = 5
if not x > 0:
print("x is not positive")

```

Output:

``````x and y are positive
x or y is positive``````

### Identity Operators

These operators are used to check if two variables refer to the same object in memory. Identity operators are used to comparing the objects, not if they are equal, but if they are actually the same object, with the same memory location:

Example:

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

print(x is y) # False
print(x is z) # True

x = [1, 2, 3]
y = [1, 2, 3]
z = x

print(x is not y) # True
print(x is not z) # False

```

Output:

``````False
True
True
False``````

### Bitwise Operators

These operators are used to manipulate the binary representation of integers. They are used to perform bit-level manipulation and are particularly useful when working with low-level systems or embedded systems. Bitwise operators are used to comparing (binary) numbers:

Example:

```x = 5  # binary representation: 00000101
y = 3  # binary representation: 00000011
result = x & y  # binary representation: 00000001, equivalent to 1 in decimal
print(x,y,result)

x = 5  # binary representation: 00000101
y = 3  # binary representation: 00000011
result = x | y  # binary representation: 00000111, equivalent to 7 in decimal
print(x,y,result)

x = 5  # binary representation: 00000101
y = 3  # binary representation: 00000011
result = x ^ y  # binary representation: 00000010, equivalent to 2 in decimal
print(x,y,result)

x = 5  # binary representation:  00000101
result = x << 2  # binary representation: 00010100
print(x,2,result)

x = 20  # binary representation:  00010100
result = x >> 2  # binary representation:  00000101
print(x,2,result)

```

Output:

``````5 3 1
5 3 7
5 3 6
5 2 20
20 2 5``````

These are just a few examples of the importance and use of operators in Python. They are essential for controlling the flow of the program, making decisions, performing calculations, and manipulating data, which is fundamental in any programming language.