In episode 6 we talk about the Python Strings. Now I'm going to explain to you about Python Operators. Operators are very important when making a mathematical calculation. There are a few common operators you may already know. They are,
+ - This is used to add two numbers (x + y)
- - This is used to subtract two numbers (x - y)
/ - This is used to divide two numbers (x / y)
- - This is used to subtract two numbers (x - y)
/ - This is used to divide two numbers (x / y)
* - This is used to multiply two numbers (x * y)
= - This is used to equal one number to another (x = y). Also, you are getting a float value after dividing a number. If you don't have knowledge about Python data types you can refer to the Python Variable Post.
= - This is used to equal one number to another (x = y). Also, you are getting a float value after dividing a number. If you don't have knowledge about Python data types you can refer to the Python Variable Post.
In the above, I explained to you about basic Operators in Python. But there lot more other types, we can categories them into two different categories.
- Python Assign Operators
- Python Comparision Operators
Python Assign Operators
Python Assign operators are usually used to increment or change a value in a specific task. I'll show you how to use Python Assign Operators but before it let's see what are Python Assign Operators.
- += - This operator is using increase a value
ex: a = 1
while a == a:
a = a + 1 or a += 1
- -= - This is used to decrease a value
ex: a = 10
while a == a:
a = a - 1 or a -= 1
- *= - This is used to multiple a value. This operator commonly used in loops.
ex: a = 2
while a == a:
a = a * 2 or a *= 2
- /= - This is used to divide a value in a loop
ex: a = 100
while a == a:
a = a / 2 or a /= 2
Python Comparision Operators
Python comparison operators are used to compare two values like which is greater or which is smaller.
- == - this operator is used to check whether one value equal to another.
ex: a = 1
if a == 1:
print(1)
- != - this operator means not equal. this is the opposite of equal operator.
ex: a = 1
if a != 2:
print(a)
- > - Greater than is using to comapare two values
ex: a = 1
if a > 0:
print("a is greater than 0")
- < - smaller than is also using to compare to values
ex: a = 1
if a < 2:
print("a is smaller than 2")
- >= - This means equal or greater than
ex: a = 1
if a >= 1:
print(a)
- <= - This means equal or smaller than
ex: a = 1
if a <= 2:
print(a)
If you have any problems with this tutorial, feel free to comment below.
Tags:
Python