Python Operators
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
Operators are used to perform some operations on say variables, functions or in general python objects.
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Arithmetic Operators
a = 10
b = 20# Addition operator
a + b30# Substraction operator
a - b-10# Multiplication operator
a * b200# Division operator
a / b0.5# Modulus operator / remainder operator
3 % 2112 % 4011 % 43a = 10
b = 20
print(a % b)10
# Exponent operator / power operator
2 ** 3 # 2 to the power 3 is 882*2*2810 ** 2 # 10 squared100# Floor division operator
# Returns the integer part of the division without the decimal point
10 // 3 3a // b0Assignment Operators
The = operator
# The = operator assigns some data to a variable
c = 12
print(c)12
Let's increase the value of a variable to understand the next assignment operators
# Counter intuitive to how we use = sign in our mathematics class
# = sign in python just assigns the right hand side RHS of = sign
# to the left hand side LHS of = sign
d = 5
# we will do d+1 first
# and then assign the increased value to the same variable d
d = d + 1
print(d)6
The += operator
To perform the operation d=d+1 in a pythonic way, we use += operator. Nothing fancy, even I don't use it that often, but other people do use it. So, it becomes important to understand such operators.
# Initialize the value 9 as variable e
e = 9
# Increase the value of variable e by one
e += 1
print(e)10
f = 10
f += 15 # Again, the same thing as f=f+15
print(f)25
The -= operator
g = 7
g -= 2 # Pythonic way of: g=g-2
print(g)5
The *= operator
h = 5
h *= 2 # Pythonic way of: h=h*2
print(h)10
The /= operator
i = 6
i /= 2 # Pythonic way of: i=i/2
print(i)3.0
The %= operator
j = 10
j %= 3 # Pythonic way of: j=j%3, remainder
print(j)1
The //= operator
k = 8
k //= 3 # Pythonic way of: k=k//3, floor division
print(k)2
The **= operator
l = 10
l **= 3 # Pythonic way of: l=l**3
print(l)1000
So on, we have different assignment operators like: bitwise ones which we will look at later. To be honest, bitwise operators are kind of outside the scope of these basic tutorials, but I'll cover them later on as the reader gets comfortable with the basics first.
Bitwise operators will require us to learn about the binary operations, which might intimidate beginners. But I believe, after building some comfort level bitwise will become really easy to understand.
Comparison Operators
The == operator
Remember the data type boolean, this is where boolean becomes extremely useful.
The == operator compares the RHS right hand side of the == sign to the LHS left hand side of the == sign.
The output is hence returned in True or False depending on if the condition was true or false.
# Let's compare the integer 4 to the integer 5
# Here we will ask python if the value 4 equals the value 5
# Of course it isn't, that's why python will say False
4 == 5False4 == 4TrueThe != operator
This is the not equal operator. It will do something opposite of what == does. It asks python if the LHS is not equal to the RHS.
4 != 5TrueYes, 4 does not equal 5, that's why python says yes the condition is true and python returns True.
The > operator
It will check if the LHS is greater than the RHS.
4 > 1TruePython says yes LHS is in fact greater than the RHS, so in the above statement python returned True (I agree).
4 > 8FalseLHS is not greater than the RHS, python does not agree, so, python says False.
The < operator
Should be pretty intuitive by now. This < sign will ask python if the LHS is lesser than the RHS.
4 < 5TruePython is happy, python agrees, python says True
4 < 2FalsePython is not happy, python disagrees, python says False. Python shall not let this mistake pass.
The >= operator
This operator checks if the LHS is greater than or equal to the RHS
4 >= 4TrueWow, we finally managed to calm python down. Now, python agrees with the humans, so python says True.
I agree with your conditions, humans!
4 >= 5FalseThe <= operator
This operator checks if the LHS is less than or equal to the RHS
4 <= 5True"haha Tickles I'm impressed"
6 <= 6True"Tickles that was a close call, but I shall allow you for now"
6 <= 5False"Oh boy, you've did it again" with furrows between his eyebrows said Python
Python Logical Operators
The and operator
It compares the LHS of "and" operator to the RHS of the "and" operator and returns True only if both the sides are True, otherwise Python throws a temper tantrum.
True and TrueTrueTrue and FalseFalseFalse and TrueFalseFalse and FalseFalseThe or operator
The "or" operator checks if any of the LHS or RHS are True.
True or FalseTrueFalse or TrueTrueTrue or TrueTrueFalse or FalseFalseThe not operator
The "not" operator just reverse the output. If the input is a True, python will lie and say "False". If the input is False, python will smirk and tell the user "True"
not TrueFalsenot FalseTrueIdentity Operators
The is operator
Unlike the == operator which checks if two objects of python have same value, the "is" operator checks if two objects are one and the same.
Note: Please pay close attention now
x = 4
y = 4
x is yTrueAt this point you might be willing to think that of course x is same as y as both have the value 4. Lets make a more complicated example.
x = "random word"
y = "random word"
x is yFalseWhen our data type is very simple like an integer, python don't create a new object in memory (RAM) for the different objects having same value.
So, when we create the data integer 4 and assign it to variable x, python creates the integer 4 and names it x
x = 4Now, if we create another integer 4 and call it y, python will use the integer value 4 that is already stored in python and would give it another name y.
y = 4Here, instead of creating a new integer 4, python will re-use the integer 4 from memory and give it another name y. So now, that integer 4 has two names, x and y.
x is yTruePython says, x and y are two same objects in memory. Let's look at their memory locations.
id() function
id() function returns the memory location of objects inside python. In simple words, it confirms for us if different variable names are given to just one single object of python.
id(x)1982647658896id(y)1982647658896Things get tricky when our data type is mute-able (We'll talk more about it later)
Let's say we have a sequence data type like a string.
Now, if we create two strings of same value, but use different variable names to reference them, python will give it two different memory locations, so these two string variables now become two different objects in memory.
string1 = "a "
string2 = "a "
string1 is string2
FalseThese two sequence data type might have same value but they are stored in different memory locations inside python. So, in this case == operator will say they have same value. The is operator will say they have different memory locations, so they are not the same object.
string1 == string2TrueThe "is not" operator
Just the reverse of "is" keyword as "is not" is associated with the lie ability of python "not" keyword.
x = 4
y = 4x is yTruex is not yFalsestring1 = "a "
string2 = "a "
string1 is string2
Falsestring1 = "a "
string2 = "a "
string1 is not string2
TrueFinished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →