Python – Basic Operators

Python
Python - Basic Operators

Basic Operators, Operators are the constructs which can manipulate the cost of operands.Consider the expression 4 + five = nine. Here, four and 5 are called operands and + is called operator.

Types of Operator

Python language supports the subsequent sorts.

  • Arithmetic
  • Comparison (Relational)
  • Assignment
  • Logical
  • Bitwise
  • Membership
  • Identity

Let us have a glance on all separately.

Basic Operators, Python Arithmetic

Assume variable a holds 10 and variable b holds 20, then −

Python - Basic Operators

Basic Operators, Python Comparison

These operators compare the values on either sides of them and decide the relation among them. They are also called Relational.

Assume variable a holds 10 and variable b holds 20, then −

Python - Basic Operators

Python Assignment

Assume variable a holds 10 and variable b holds 20, then −

Python - Basic Operators

Python Bitwise

Bitwise operator works on bits and performs bit by bit operation. Assume if a = 60; and b = 13; Now in binary format they will be as follows −

a = 0011 1100

b = 0000 1101


a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011

There are following Bitwise supported by Python language

Python - Basic Operators

Python Logical

There are following logical supported via Python language. Assume variable a holds 10 and variable b holds 20 then

Python Membership

Python’s membership operators take a look at for membership in a chain, along with strings, lists, or tuples. There are two club operators as explained below −

Python Identity

Identity operators examine the reminiscence locations of two items. There are Identity operators explained below −

Python Operators Precedence

The following table lists all from highest precedence to lowest.

Sr.No.Operator & Description
1**Exponentiation (raise to the power)
2~ + –Complement, unary plus and minus (method names for the last two are [email protected] and [email protected])
3* / % //Multiply, divide, modulo and floor division
4+ –Addition and subtraction
5>> <<Right and left bitwise shift
6&Bitwise ‘AND’
7^ |Bitwise exclusive `OR’ and regular `OR’
8<= < > >=Comparison
9<> == !=Equality
10= %= /= //= -= += *= **=Assignment
11is is notIdentity
12in not inMembership
13not or andLogical