Python Basic Operations - Python Operators | W3schools

Python is an object-oriented open-source language that was invented n 1990s by Guido Van Rossum. Since then it is extensively being used in various applications in colleges and industries. It is a very easy and simple language and can be learned within no time. Moreover, Python has the capability to interact with multiple databases and systems which makes it lucrative for industrial use. Companies like NASA, Google, Facebook etc. have already started using Python language in various of their product applications. This dynamic language with various libraries is currently the best skill-set that you can have to grow your career. People having a good knowledge of the Python language have excelled in their respective work fields. In this tutorial, we will learn about some of the basic operators that Python language has. 

Also Read: Python Variable Types

Python Operators

The operators are symbols that carry out arithmetic or logical computation in Python. The operand is the value that the operator executes on. Operators are used to changing the value of the operands. For example:

>>> 2+3

5

In the above example, ‘+’ is the operator, 2 and 3 are operands. An Addition is performed by the operator and the output of the operation is 5. The operators can also work on variables if they have a value defined. New variables can also be created by using the Python operators directly.

[post_middile_section_ad]

Types of Operators

There are many types of operators that the Python language supports. Below is the list of different operator types in Python:

1.      Arithmetic Operators

2.      Comparison Operators

3.      Assignment Operators

4.      Logical Operators

5.      Bitwise Operators

6.      Membership Operators

7.      Identity Operators

Let’s learn more details of each operator one by one.

Python Arithmetic Operators

The arithmetic Operators perform multiple arithmetic calculations like subtraction, division, multiplication, addition, exponent, %modulus etc. These calculations can be performed using various methods like using eval function or calling separate functions etc. Below is the list of arithmetic operators that Python supports:

1.      ‘+’, addition: This operator adds the value on either side of the operator. For example:

>>> a = 2

>>> b = 5

>>> print (a+b)

7

2.      ‘-‘, subtraction: This operator subtracts the right hand operand from the left hand operand. For example:

>>> a = 7

>>> b = 2

>>> print (a-b)

5

3.      ‘*’, multiplication: This operator multiplies values on either side of the operator. For example:

>>> a = 7

>>> b = 2

>>> print (a*b)

14

4.      ‘/’, division: This operator divides the left hand operand by the right hand operand. For example:

>>> a = 8

>>> b = 2

>>> print (a/b)

4

[post_middile_section_ad]

5.      ‘%’, modulus: This operator divides the left hand operand by the right hand operand to return the remainder. For example:

>>> a = 8

>>> b = 2

>>> print (a%b)

0

6.      ‘**’, exponent: This operator performs the exponential calculation on operators. For example:

>>> a = 8

>>> b = 2

>>> print (a**b)

64

7.      ‘//’, floor division: This operator performs the division but the digits after decimals are removed from the quotient. If either of the operands is negative then the result is floored i.e. the output is rounded away from zero. For example:

>>> a = 9

>>> b = 2

>>> print (a//b)

>>> c = -11.0

>>> d = 3

>>> print (c//d)

4

-4.0

 [post_middile_section_ad]

Python Comparison Operators

The comparison operators in Python are used to compare the operands on either side of the operator and a relation is decided between them. These are also known as relational operators. Many comparisons like greater than, less than, equal to, greater than or equal to, less than or equal to etc. are executed by these operators. Below is the list of the comparison operators that Python supports:

1.      ‘==’: If both the operands are equal then this condition becomes true. For example:

>>> a = 2

>>> b = 5

>>> print (a==b)

False

2.      ‘!=‘: If both the operands are not equal then this condition becomes true. For example:

>>> a = 7

>>> b = 2

>>> print (a!=b)

True

3.      ‘<>’: If both the operands are not equal then this condition becomes true. This is similar to ‘!=’ operator. For example:

>>> a = 7

>>> b = 2

>>> print (a<>b)

True

4.      ‘>’: If the left operand’s value is greater than the value of the right operand, then condition becomes true. For example:

>>> a = 8

>>> b = 2

>>> print (a>b)

True

5.      ‘<’: If the left operand’s value is less than the value of the right operand, then condition becomes true. For example:

>>> a = 8

>>> b = 2

>>> print (a<b)

False

6.      ‘>=’: If the left operand’s value is greater than or equal to the value of the right operand, then condition becomes true. For example:

>>> a = 8

>>> b = 2

>>> print (a>=b)

True

7.      ‘<=’: If the left operand’s value is less than or equal to the value of the right operand, then condition becomes true. For example:

>>> a = 9

>>> b = 2

>>> print (a<=b)

False

Python Assignment Operators

The assignment operators are used to store values in a particular variable from numbers or other variables. These operators are helpful in the looping functions like for loop, if-else loop etc. Below is the list of the assignment operators that Python supports:

1.      ‘=’: This operator assigns values from right side operand to the left side operand. c = a + b assigns value of a + b into. For example:

>>> a = 2

>>> b = 5

>>> c = a + b

>>> print (c)

7

2.      ‘+=‘: This operand adds the left and the right operands and then assigns the value to the left operand. b += a is same as b = b + a. For example: 

>>> a = 7

>>> b = 2

>>> b+=a

>>> print (b)

9

3.      ‘-=’: This operand subtracts the right operand’s value from the left operand and then assigns the value to the left operand. b -= a is same as b = b – a. For example:

>>> a = 7

>>> b = 2

>>> b-=a

>>> print (b)

-5 

4.      ‘*=’: This operand multiplies the left and the right operands and then assigns the result to the left operand. b *= a is same as b = b * a. For example:

>>> a = 8

>>> b = 2

>>> b*=a

>>> print (b)

16

5.      ‘/=’: This operand divides the left operand with the right operands and then assigns the value to the left operand. b /= a is same as b = b / a. For example:

>>> a = 2

>>> b = 8

>>> b/=a

>>> print (b)

4

6.      ‘%=’: This operand calculates the modulus using the left operand and the right operand and then assigns the value to the left operand. b %= a is same as b = b % a. For example:

>>> a = 2

>>> b = 8

>>> b%=a

>>> print (b)

0

7.      ‘**=’: This operand calculates the exponent using the left operand and the right operand and then assigns the value to the left operand. b **= a is same as b = b ** a. For example:

>>> a = 2

>>> b = 7

>>> b**=a

>>> print (b)

49

8.      ‘//=’: This operand calculates the floor division using the left operand and the right operand and then assigns the value to the left operand. b //= a is same as b = b // a. For example:

>>> a = 2

>>> b = 7

>>> b//=a

>>> print (b)

3

 [post_middile_section_ad]

Python Bitwise Operators

The Bitwise operators operate on numbers but treat them as a string of bits instead of treating them as a single number. These numbers are treated as a twos-complement binary which is same as the standard binary representation for integers. This representation differs for negative integers though. Negative integers are represented by performing the twos-complement operation on their absolute value. Let us understand this by taking an example:

a = 60; b = 13

Their binary representation will be:

a = 0011 1100

b = 0000 1101

The bitwise operation will give outputs:

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011

Below is the explanation of the bitwise operators that Python supports:

1.      & Binary AND: This operator copies a bit to the result if it exists in both the operands. If the corresponding bit of ‘a AND of b’ is 1, then the output is 1, else 0. 

2.      | Binary OR: This operator copies a bit to the result if it exists in either of the operands. If the corresponding bit of ‘a AND of b’ is 0, then the output is 0, else 1.

3.      ^ Binary XOR: This operator copies the bit if it is set in one of the operands but not both. The output of each bit is same as the corresponding bit in ‘a’ if that bit in ‘b’ is 0, and the output is the complement of the bit in ‘a’ if that bit in ‘b’ is 1.

4.      ~ Binary Ones Complement:   This operator returns the complement of each bit that is present in ‘a’. The output would be 1 if the corresponding bit in ‘a’ is 0 and vice versa.

5.      << Binary Left Shift: The value of the left operand is moved left by the number of bits specified by the right operand.

6.      >> Binary Right Shift:  The value of the left operands is moved right by the number of bits specified by the right operand.

Python Logical Operators

The Logical operators in Python are used to combine single or multiple conditions and then perform logical operations using Logical OR, Logical AND, and Logical NOT. Below is the list of the logical operators that Python supports:

1.      AND or Logical AND: This operator will return True when both the conditions are correct. For example:

>>> a = 2

>>> b = 5

>>> print (a>0 and b>0)

True

2.      OR or Logical OR: This operator will return True when either of the conditions is correct. For example: 

>>> a = -1

>>> b = 2

>>> print (a>0 and b>0)

True

3.      NOT or Logical NOT: If the condition is True then this operator makes it False. For example:

>>> a = 7

>>> b = 2

>>> not(a and b)

False

Truth Tables for Logical Operators

Below is the truth table for Logical AND and OR operators. This would be useful for future references. You can interpret your output based on the below tables.

LOGICAL AND Truth Table:

Condition 1

Condition 2

Condition 1 AND condition 2

True

True

True

True

False

False

False

True

False

False

False

False

 

LOGICAL OR Truth Table:

Condition 1

Condition 2

Condition 1 AND condition 2

True

True

True

True

False

True

False

True

True

False

False

False

 

Python Membership Operators

The membership operators in Python are used to validate the membership in a sequence like a string, a list or a tuple. Below is the list of the membership operators that Python supports:

1.      in operator: This operator is used to see if a value exists in a sequence or not. If the value exists then the operator returns a True else it returns a false. For example: 

Ex-1

>>> lst1 = [‘Ajay’, ‘Bobby’,’Ashok’, ‘Vijay’, ‘Anil’, ‘Rahul’,’Alex’, ‘Christopher’]

>>> if ‘Ajay’ in lst1: print(‘Name Ajay exists in lst1’)

Name Ajay exists in lst1

Ex-2

>>> if ‘j’ in ‘Ajay’: print(“letter ‘j’ exists in name ‘Ajay'”)

>>> else: print (“letter ‘j’ does not exists in name ‘Ajay'”)

letter ‘j’ exists in name ‘Ajay’

2.      not in operator: This operator works just the opposite to the ‘in operator’. If a value does not exist then the operator returns a True else it returns a false. For example: 

Ex-1

>>> lst1 = [‘Ajay’, ‘Bobby’,’Ashok’, ‘Vijay’, ‘Anil’, ‘Rahul’,’Alex’, ‘Christopher’]

>>> if ‘Raghav’ not in lst1: print (‘Name Raghav does not exists in lst1’)

Name Raghav does not exists in lst1

Ex-2

>>> if ‘j’ not in ‘Ajay’: print (“letter ‘j’ does not exists in name ‘Ajay'”)

>>> else: print (“letter ‘j’ exists in name ‘Ajay'”)

letter ‘j’ exists in name ‘Ajay’

Python Identity Operators

The identity operators in Python are used to compare the memory location of two objects. Below is the list of the identity operators that Python supports:

1.      is operator: This operator returns True if both the operands point to the same memory location. For example: 

>>> a = ‘London’

>>> b = ‘London’

>>> c = ‘Paris’

>>> if a is b: print (‘a is b’)

>>> else: print (‘a is not b’)

>>> if a is c: print(‘a is c’)

>>> else: print (‘a is not c’)

>>> if b is c: print(‘b is c’)

>>> else: print (‘b is not c’)

a is b

a is not c

b is not c

Since the variables having same value point to the same memory location the statement (a is b) returns a value of True.

2.      is not operator: This operator works just the opposite to the ‘is operator’. If both the operands point to different value then this operator returns True. For example: 

>>> a = ‘London’

>>> b = ‘London’

>>> c = ‘Paris’

>>> if a is not b: print (‘a is not b’)

>>> else: print (‘a not b’)

>>> if a is not c: print (‘a is not c’)

>>> else: print (‘a is c’)

>>> if b is not c: print (‘b is not c’)

>>> else: print (‘b is c’)

a not b

a is not c

b is not c

Since the variables having same value point to the same memory location the statement (a is not b) returns a value of False.

[post_middile_section_ad]

Python Operators Precedence

The following table shows the precedence status of all the operators. The topmost operator is having the highest precedence. Operator in the same box evaluates from left to right.

Operator

Description

()

Parentheses (grouping)

f(args…)

Function call

x[index:index]

Slicing

x[index]

Subscription

x.attribute

Attribute reference

**

Exponentiation

~x

Bitwise not

+x, -x

Positive, negative

*, /, %

Multiplication, division, remainder

+, –

Addition, subtraction

<<, >>

Bitwise shifts

&

Bitwise AND

^

Bitwise XOR

|

Bitwise OR

in, not in, is, is not, <, <=,  >,  >=,
<>, !=, ==

Comparisons, membership, identity

not x

Boolean NOT

and

Boolean AND

or

Boolean OR

lambda

Lambda expression

 

Conclusion

The above tutorial will help you understand the different variable types in Python. Learning these variable types is easy and their usage is straightforward. After going through this tutorial, you can start using different variable types in your code and take advantage of this dynamic nature of the Python language.