Go4Expert Tutorials

Programming And Web Development Tutorials

 
  • Home
  • Tutorials
  • Python
  • PERL
  • ASP.Net
  • C++
You are here: Home / Tutorials / Python / Python Operators and Their Precedence

Python Operators and Their Precedence

Python has many operators which can perform specific operations. These operators can manipulate numbers or check conditions.

And the best part, they are easy to understand and implement.

Types of Operator:

Python language supports the following types of operators.

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Membership Operators
  6. Bitwise Operators

1. Arithmetic Operators:

These operators are meant for number data types only. There perform the basic arithmetic operations:

  • + for addition
  • – for subtraction
  • * for multiplication
  • / for division

Python also has 3 arithmetic operators that are special. We will discuss them.

a. % (Modulus operator) :

This operator gives the remainder of the division. And if there is no remainder it returns zero.

For example: 22 % 7 = 1, 21 % 7 = 0

b. ** (Exponent operator) :

This operator performs exponential (power) calculation on numbers. The first number is raised to the power of the second number.

For example: 2**3 = 8

c. // (Floor Division) :

This operator performs division but truncates off decimal part.

For example: 44 // 7 = 6

# Let's declare variable variable1 with value 200, variable2 with value 30
variable1 = 200
variable2 = 3
print("value of variable1 is", variable1)
print("value of variable2 is", variable2)
# Performing modulus
modulusOutput = variable1 % variable2
print("Value of modulus is", modulusOutput)
# output: Value of modulus is 2
# Performing Exponent function
exponentOutput = variable1 ** variable2
print("Value of exponent is", exponentOutput)
#output: Value of exponent is 8000000
# Performing floor division
floorDivisionOutput = variable1 // variable2
print("Value of floor division is", floorDivisionOutput)
# output: Value of floor division is 66
view raw Arithmetic Operators - special operators.py hosted with ❤ by GitHub

2. Relational Operators:

These Operators used for comparing values. If the condition of comparison holds correct it gives True as output otherwise False.

There are some basic relational operators:

  • < less than
  • > greater than
  • <= less than or equal to
  • >= greater than or equal to

There are 2 special relational operators which are going to discuss.

a. == (Equal comparison):

Compares the 2 values and returns True if both values are equal.

For example: 12 == 13 will give False

b. != (Not Equal to) :

Compares the 2 values and returns True if both values are not equal.

For example: 12 != 13 will give True

# lets declare a variable, variable1 with value 100
variable1 = 100
print("Value of variable1 is", variable1)
# performing equality
EqualityOutput = variable1 == 100
print("is variable1 == 100", EqualityOutput)
# result: is variable1 == 100 True
# performing not equal to
output = variable1 !=100
print("is variable1 != 100", output)
# result: is variable1 != 100 False
view raw Relational Operators.py hosted with ❤ by GitHub

3. Logical Operators:

In Programming we have to check for multiple conditions to reach a conclusion. And for that we use Logical Operators.

A logical operator works on the conditions and gives output as True or False.

Following are the logical operators:

a. and :

If the value of both the condition is True then, “and” operator will True as output. If the value of any of the condition is False then, “and” operator will give False as output.

For Example: 12> 13 and 12<13 will give False

b. or :

If the value of any of the condition is True then, “or” operator will give True as output. If both the condition is False then, “or” operator will give False as output.

For Example: 12>13 or 12<13 will give True

c. not :

“not” operator reciprocates the output of condition. It converts True to False and vice versa.

For example: not 12> 13 will give True as output

# Declare variable1 as 100 and variable as 200
variable1 = 100
variable2 = 200
print("value of variable1", variable1)
print("value of variable2", variable2)
# performing and operation
andOutput = variable1 > 90 and variable1 < 105
print("is vairable1 greater than 90 and less than 105", andOutput)
# output: is vairable1 greater than 90 and less than 105 True
# performing or operation
orOperation = variable1 > variable2 or variable2 < 12
print("variable1 is greater than variable2 or variable2 less than 12", orOperation)
# output: variable1 is greater than variable2 or variable2 less than 12 False
# performing not operation
notOperation = not variable1 > variable2
print(" not variable1 > variable2", notOperation)
# output: not variable1 > variable2 True
view raw Logical Operators.py hosted with ❤ by GitHub

4. Assignment Operators:

These operators are used to assign the values into a variable. The assignment is done with the help of “=” (equal to ) operator.

For example: variable1 = 12

There is also a variation in the assignment called as short-hand operators. They are used to reduce the code and perform the operation while assigning.

For example: variable2 = variable2 + 12 will be written as variable2 +=12

Both the statements have the same meaning, but the second one is the short version of the first statement. Following are the short-hand operators used in python:

  1. += adds the value to the variable before assigning
  2. -= subtracts the value from variable before assigning
  3. *= multiplies the value to the variable before assigning
  4. /= divides the value to the variable before assigning
  5. %= perform modulus to the variable by value before assigning
  6. **= perform exponential to the variable by value before assigning
  7. //= performs floor division to the variable by value before assigning
# DeoutputOperationlare the variable, variable1 as 21 and variable2 as 4
# we are going to perform series of operations and the result will be stored in outputOperation variable
variable1 = 21
variable2 = 4
print("variable1 =", variable1) # output: variable1 = 21
print("variable2 =", variable2) # output: variable2 = 4
# performing addition
outputOperation = variable1 + variable2
print("outputOperation = ", outputOperation ) # output: outputOperation = 25
# performing shorthand addition
outputOperation += variable1
print("outputOperation = ", outputOperation ) # output: outputOperation = 46
# performing shorthand multiplication
outputOperation *= variable1
print("outputOperation = ", outputOperation ) # output: outputOperation = 966
# performing shorthand divison
outputOperation /= variable1
print("outputOperation = ", outputOperation ) # output: outputOperation = 46.0
# performing shorthand modulus
outputOperation %= variable1
print("outputOperation = ", outputOperation ) # output: outputOperation = 4.0
# performing shorthand exponent
outputOperation **= variable2
print("outputOperation = ", outputOperation ) # output: outputOperation = 256.0
# performing shorthand floor division
outputOperation //= variable1
print("outputOperation = ", outputOperation ) # output: outputOperation = 12.0
view raw Assignment Operators.py hosted with ❤ by GitHub

5. Membership Operators:

These operators are used to find out whether a value is part of a variable or not. These operators are not for numbers. These operators can be used for all other data types. These are

a. in :

This operator returns True if the value is part of the variable.

# Declare a string1 variable
# Find "H1" in string 1
string1 = "Hello World"
print("value of string1 is", string1)
# output: value of string1 is Hello World
inOutput = "H" in string1
print("is 'H' in string1", inOutput)
# output: is 'H' in string1 True
view raw Membership Operators - in.py hosted with ❤ by GitHub

b. not in :

This operator returns False if the value is part of the variable.

# Declare a list list1 with value [12,23,34]
list1 = [12,23,34]
print("value os list1 is", list1)
# output: value os list1 is [12, 23, 34]
# using not in operator for 25
notInOperator = 25 not in list1
print("is 25 not in list1", notInOperator)
# output: is 25 not in list1 True
view raw Membership Operators - not in.py hosted with ❤ by GitHub

Note: in and not in operators will work for Dictionary Keys and not values.

6. Bitwise Operators:

These operators are used on numbers only and they are used to perform bit-level manipulation.

These operators first convert a number into the binary form and then perform specific operations. The result is then converted back into decimal number format and given as output.

We can check the binary value of a number by using bin function. Following are the operators:

  1. & (Binary AND) – performs “and” operations on bits of both variables
  2. | (Binary OR) - performs “or” operations on bits of both variables
  3. ^ (Binary XOR) – gives output as 1 bit if one bit
  4. ~ (Binary Complement) – used for a single variable, this operator complements the binary bits of a variable
  5. << (Binary Left Shift) – variable binary value is moved to shifted to the left side and 0 is added in the end
  6. >> (Binary Right Shift) – variable binary value is moved towards the right side and the rightmost bit is discarded
# declare 2 variables, variable1 with value 78, and variable2 with value 341
# we are going to perform series of operations and the result will be stored in output variable
variable1 = 78
variable2 = 341
print("variable1 =",variable1," - binary value =",bin(variable1))
print("variable2 =",variable2," - binary value =",bin(variable2))
# output: variable1 = 78 - binary value = 0b1001110
# output: variable2 = 341 - binary value = 0b101010101
# performing binary & operation
output = variable1 & variable2
print ("output of binary & operation is ", output,' -',bin(output))
# output : output of binary & operation is 68 - 0b1000100
# performing binary or operation
output = variable1 | variable2
print ("output of binary or operation is ", output,' -',bin(output))
# output : output of binary or operation is 351 - 0b101011111
# performing binary xor operation
output = variable1 ^ variable2
print ("output of binary xor operation is ", output,' -',bin(output))
# output: output of binary xor operation is 283 - 0b100011011
# performing binary complement operation
output = ~variable1
print ("output of binary complement is ", output,' -',bin(output))
# output: output of binary complement is -79 - -0b1001111
# performing binary left shift operation
output = variable2 << 2
print ("output of binary left shift is ", output,' -',bin(output))
# output: output of binary left shift is 1364 - 0b10101010100
# performing binary right shift operation
output = variable2 >> 2
print ("output of binary right shift is ", output,' -',bin(output))
# output: output of binary right shift is 85 - 0b1010101
view raw Bitwise Operators.py hosted with ❤ by GitHub

Operator Precedence:

What will happen if in an expression there is more than one operator? In that case, operators will follow the following precedence.

  1. ** Exponentiation (raise to the power)
  2. ~ Binary Complement
  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 operators
  9. <> == != Equality operators
  10. = %= /= //= -= += *= Assignment operators
  11. in not in Membership operators
  12. not or and Logical operators

Key Takeaways:

  1. Arithmetic operators are used only by the number data types.
  2. Relational operators compare the values and give the output as True or False.
  3. Logical Operators compares the conditions and give output as True or False.
  4. Assignment operators are used to assigning values and short-hand operators are used to manipulate value before assigning.
  5. A membership variable tells whether a value is part of a variable or not.
  6. Bitwise operator operates on the binary bits of number data type only.

Programming exercises:

  1. Perform the following steps:
    • Declare a variable variable1 assign value 200 to it
    • Perform short-hand subtraction of the value 20 on variable1
    • Perform short-hand modulus with the value 27 on variable1
    • Perform left shit bitwise operator with 2 as a value on variable1
    • Compare equality operator on variable1 with 72 and assign the value in a variable output
    • Print value of output, it should be True.
  1. Perform the following steps:
    • Declare a variable, variable2 with the value 23
    • Perform short-hand multiplication with the value 456 on variable2
    • Perform short-hand floor division with the value 34 on variable2
    • Perform right shit bitwise operator with 2 as a value on variable2
    • Declare a list, list1 with the value [7, 77, 777]
    • Perform variable2 "not in" operator on list1 and assign the value in a variable output.
    • Print the value of output, it should be False.

You can download the solution files Python operators

« Python Variables And Data Types

Table of Content

  • Python Overview
  • Python Installation
  • Python Interpreter, IDLE and Exercise
  • Python Basic Syntax
  • Python Variables And Data Types
  • Python Operators and Their Precedence

© Go4Expert Tutorials 2004 - 2025. All Rights Reserved.