 # Python Operators

## Python Operators

Python Operators in general are used to perform operations on values and variables. These are standard symbols used for the purpose of logical and arithmetic operations. In this article, we will look into different types of Python operators.

## Arithmetic Operators

Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, and division.

### Example: Arithmetic operators in Python

 `# Examples of Arithmetic Operator` `a ``=` `9` `b ``=` `4` `# Addition of numbers` `add ``=` `a ``+` `b` `# Subtraction of numbers` `sub ``=` `a ``-` `b` `# Multiplication of number` `mul ``=` `a ``*` `b` `# Division(float) of number` `div1 ``=` `a ``/` `b` `# Division(floor) of number` `div2 ``=` `a ``/``/` `b` `# Modulo of both number` `mod ``=` `a ``%` `b` `# Power` `p ``=` `a ``*``*` `b` `# print results` `print``(add)` `print``(sub)` `print``(mul)` `print``(div1)` `print``(div2)` `print``(mod)` `print``(p)`

Output

```13
5
36
2.25
2
1
6561```

## Comparison Operators

Comparison of Relational operators compares the values. It either returns True or False according to the condition.

### Example: Comparison Operators in Python

 `# Examples of Relational Operators` `a ``=` `13` `b ``=` `33` `# a > b is False` `print``(a > b)` `# a < b is True` `print``(a < b)` `# a == b is False` `print``(a ``=``=` `b)` `# a != b is True` `print``(a !``=` `b)` `# a >= b is False` `print``(a >``=` `b)` `# a <= b is True` `print``(a <``=` `b)`

Output

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

## Logical Operators

Logical operators perform Logical ANDLogical OR, and Logical NOT operations. It is used to combine conditional statements.

### Example: Logical Operators in Python

 `# Examples of Logical Operator` `a ``=` `True` `b ``=` `False` `# Print a and b is False` `print``(a ``and` `b)` `# Print a or b is True` `print``(a ``or` `b)` `# Print not a is False` `print``(``not` `a)`

Output

```False
True
False```

## Bitwise Operators

Bitwise operators act on bits and perform the bit-by-bit operations. These are used to operate on binary numbers.

### Example: Bitwise Operators in Python

 `# Examples of Bitwise operators` `a ``=` `10` `b ``=` `4` `# Print bitwise AND operation` `print``(a & b)` `# Print bitwise OR operation` `print``(a | b)` `# Print bitwise NOT operation` `print``(~a)` `# print bitwise XOR operation` `print``(a ^ b)` `# print bitwise right shift operation` `print``(a >> ``2``)` `# print bitwise left shift operation` `print``(a << ``2``)`

Output

```0
14
-11
14
2
40```

## Assignment Operators

Assignment operators are used to assigning values to the variables.

### Example: Assignment Operators in Python

 `# Examples of Assignment Operators` `a ``=` `10` `# Assign value` `b ``=` `a` `print``(b)` `# Add and assign value` `b ``+``=` `a` `print``(b)` `# Subtract and assign value` `b ``-``=` `a` `print``(b)` `# multiply and assign` `b ``*``=` `a` `print``(b)` `# bitwise lishift operator` `b <<``=` `a` `print``(b)`

Output

```10
20
10
100
102400```

## Identity Operators

is and is not are the identity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal do not imply that they are identical.

```is          True if the operands are identical
is not      True if the operands are not identical```

### Example: Identity Operator

 `a ``=` `10` `b ``=` `20` `c ``=` `a` `print``(a ``is` `not` `b)` `print``(a ``is` `c)`

Output

```True
True```

## Membership Operators

in and not in are the membership operators; used to test whether a value or variable is in a sequence.

```in            True if value is found in the sequence

### Example: Membership Operator

 `# Python program to illustrate` `# not 'in' operator` `x ``=` `24` `y ``=` `20` `list` `=` `[``10``, ``20``, ``30``, ``40``, ``50``]` `if` `(x ``not` `in` `list``):` `    ``print``(``"x is NOT present in given list"``)` `else``:` `    ``print``(``"x is present in given list"``)` `if` `(y ``in` `list``):` `    ``print``(``"y is present in given list"``)` `else``:` `    ``print``(``"y is NOT present in given list"``)`

Output

```x is NOT present in given list
y is present in given list```

## Precedence and Associativity of Operators

Precedence and Associativity of Operators: Operator precedence and associativity determine the priorities of the operator.

### Operator Precedence

This is used in an expression with more than one operator with different precedence to determine which operation to perform first.

### Example: Operator Precedence

 `# Examples of Operator Precedence` `# Precedence of '+' & '*'` `expr ``=` `10` `+` `20` `*` `30` `print``(expr)` `# Precedence of 'or' & 'and'` `name ``=` `"Alex"` `age ``=` `0` `if` `name ``=``=` `"Alex"` `or` `name ``=``=` `"John"` `and` `age >``=` `2``:` `    ``print``(``"Hello! Welcome."``)` `else``:` `    ``print``(``"Good Bye!!"``)`

Output

```610
Hello! Welcome.```

### Operator Associativity

If an expression contains two or more operators with the same precedence then Operator Associativity is used to determine. It can either be Left to Right or from Right to Left.

### Example: Operator Associativity

 `# Examples of Operator Associativity` `# Left-right associativity` `# 100 / 10 * 10 is calculated as` `# (100 / 10) * 10 and not` `# as 100 / (10 * 10)` `print``(``100` `/` `10` `*` `10``)` `# Left-right associativity` `# 5 - 2 + 3 is calculated as` `# (5 - 2) + 3 and not` `# as 5 - (2 + 3)` `print``(``5` `-` `2` `+` `3``)` `# left-right associativity` `print``(``5` `-` `(``2` `+` `3``))` `# right-left associativity` `# 2 ** 3 ** 2 is calculated as` `# 2 ** (3 ** 2) and not` `# as (2 ** 3) ** 2` `print``(``2` `*``*` `3` `*``*` `2``)`

Output

```100.0
6
0
512```

Last Updated on October 24, 2021 by admin

## Check if element exists in list in PythonCheck if element exists in list in Python

Check if element exists in list in Python List is an important container in python as if stores elements of all the datatypes as a collection. Knowledge of certain list operations is necessary for day-day programming. This article discusses one

## Statement, Indentation and Comment in PythonStatement, Indentation and Comment in Python

Statement, Indentation and Comment in Python Instructions written in the source code for execution are called statements. There are different types of statements in the Python programming language like Assignment statements, Conditional statements, Looping statements, etc. These all help the

## Python | datetime.timedelta() functionPython | datetime.timedelta() function

Python | datetime.timedelta() function Python timedelta() function is present under datetime library which is generally used for calculating differences in dates and also can be used for date manipulations in Python. It is one of the easiest ways to perform

## Python – Extract specific keys from dictionaryPython – Extract specific keys from dictionary

Python | Extract specific keys from dictionary We have a lot of variations and applications of dictionary container in Python and sometimes, we wish to perform a filter of keys in dictionary, i.e extracting just the keys which are present

## Python – Accessing Key-value in DictionaryPython – Accessing Key-value in Dictionary

Python | Accessing Key-value in Dictionary Dictionary is quite a useful data structure in programming that is usually used to hash a particular key with value, so that they can be retrieved efficiently. Let’s discuss various ways of accessing all the

## Reading an excel file using Python openpyxl moduleReading an excel file using Python openpyxl module

Reading an excel file using Python openpyxl module Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files. For example, users might have to go

## Python TuplesPython Tuples

Python Tuples Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers. Values of a tuple are syntactically separated by ‘commas’.

## Get a specific row in a given Pandas DataFrameGet a specific row in a given Pandas DataFrame

Get a specific row in a given Pandas DataFrame In the Pandas DataFrame  we can find the specified row value with the using function iloc(). In this function we pass the row number as parameter. pandas.DataFrame.iloc[] Syntax : pandas.DataFrame.iloc[] Parameters