chevron_left
105 points
10 5 2

Python operators

posted Aug 3 3 min read

Python like any other programming language has a wide range of operators.

Arithmetic operators → They are used to perform mathematical operations on the numerical values.

Addition  → + – Adds two or more numbers together for example 2 +3

Subtraction  → – Gets the difference between two or more number for example 2-3

Multiplication → * – Gets the product between two or more number for example 2*3

Division  →  / – Divides the first number with second. For example 3 /2 . In python when a number is divided by 0 an exception is raised called zeroDivisionError

Modulus → % – Get the reminder after division of two numbers. For example 3 % 2 = 1

Exponentiation → ** – Raises one number (first number) by the second number. For example 2 ** 8.

Floor division → // → Divides two integers the applies the floor function

Assignment operators → 

= → assigns a value to a variable For example x =5

-= → subtracts the value of the right hand side operand to the variable on the left hand side operand

for example x=5 x-=3 => 2

+= → compound operator → Adds adds the right hand side operand to the existing value of the left hand side operand. For example x=4 x += 5  => 9

*= → Does the product of the right hand side operand to the left hand side operand and stores or print. For example x=4 x *= 3 => 12

/= →  Does the division of the right hand side from the left hand side and outputs. For example x=9 x/=2 => 4.5

%= → Does the  modulus(getting reminder) after dividing right hand side from left hand side

For example x = 5 x%=2 => 1

//= →

**= → The value from the right hand side is used to raise the value on the left hand side. For example x=3 x**=2 => 9

^=

&= Used to call the intersection of set type. For example a = set(‘klm’)

a &= set(‘m0l’) => m l

|= → Used to do union in sets list dictionary tuple. It will join the value of the right hand side operand to the left hand side operand while OR | alone merges. For example x = {‘1’,’a’,’q’} x |= {‘2’,’s’,’f’}

>>=

<<= → Simply does a product with exponentiation being on <sup>the right side For example x * 2 ^y x being value of left hand side and y being value of the right side c=4 c<<1 == 4*2^1 => 8.

Comparison operators

== → Equals → Checks if two numeric values are equals and return either true  or false

!= → Not equals → Checks if data given is equal or not and returns boolean value

> → Greater than →  Checks if a given value greater than the other.

< → Less than → Checks if a given value less than the other.

>= → Greater than or equals to → Checks if a given value greater than or equal to the other.

<= → Less than or equals to → Checks if a given value less than  or equal to the other.</p>

Logical operators

and → checks both condition supplied to then automates to true.

or → checks if one of the condition is true returns true

not → reverse the return provided initially

Membership operators

in → checks if a value is in an object such as a list

in not → checks if a value is not in an object such as a list

Identity operators

is → returns true if two objects are same

is not → returns true if two object are not same

########################################
# Membership operators
########################################
a=5 lst=[12,3,4,5,6,7,8]
print( a in lst )
print( a not in lst )

########################################
# Identity operators
########################################
a=4 b=5 aa='Hi' bb="hi"
print( a is b )
print( aa is not bb )

########################################
# logical comparison
########################################
x=50
print(" And : ",  x < 51 and x > 2)
print(" Or : ",  x < 51 or x > 2)
print(" not and : ",  not(x < 51 and x > 2))
print(" not or : ",  not(x < 51 or x > 2))
########################################
# Comparison operators
########################################
a=3 b=8
print( " Equals :",           a == b )
print( " Not Equals :",    a != b )
print( " Greater than :", a > b )
print( " Less than :",       a < b)
print( " Less than or Equals to :", a <= b )
print( " Greater than or Equals to :", a >= b)

########################################
# Arithmetic operators
########################################
a=4 b=6
print(" Add :", a+b )
print(" Subtract :", a-b )
print(" Multiply :", a*b )
print(" Divide :", a/b)
print(" Exponentiation :", a**b)
print(" Modulus :", a%b)
print(" Floor division :", a//b)

 

More Posts

how to install python pip on windows ? videepsinghal - 1 day
If else in one line python sam_dev - 3 days
invalid syntax python for no reason sam_dev - 3 days
Python ecommerce | build a django ecommerce web application. Tania - Sep 14
Number of occurrences of a character in a string in python with example. Tania - Sep 13
Develop a shopping cart website with django 2 and python 3. Tania - Sep 13
Introduction to programming using python exercise solutions. Tania - Sep 12
Beginner Friendly Project: Build a Simple Student Grade Calculator With Python george.isiguzo - Sep 8
How To Get the Last Element Of List In Python itsmycode - Sep 8
How to check if a variable is an integer Python? Hafsa_Rao - Sep 6