In the previous posts we told you the predefined functions and the Keywords in python, this week we are going to talk about the basic operator types in Python and how to use each of them with some examples.
The operators They are symbols that tell the interpreter to perform a specific operation, such as arithmetic, comparison, logic, etc.
In this post we are going to learn about assignment, arithmetic, relational and logical operators.
We remind you that if you want to learn more about Python, we invite you to stop by our master in Advanced Programming in Python for Big Data, Hacking and Machine Learning in EIP where in just 12 months, you will become a widely qualified expert.
Assignment operators
They are the ones we use to give value to a variable. Normally we usually combine them with other operators to carry out an operation. It is a way of summarizing code.
Operator | Description | Example |
= | Assigns the value to the variable | >>> x=5; print(«Result «, x) Result 5 |
+= | Add the value to the right of the equal to the variable | >>> x=5; x+=2; print(«Result «, x) Result 7 |
-= | Subtract the value to the right of the variable X | >>> x=5; x-=2; print(«Result «, x) Result 3 |
*= | Multiply the value to the right of equal to the variable | >>> x=5; x*=2; print(«Result «, x) Result 10 |
/= | Divide the value to the right of the variable X | >>> x=5; x/=2; print(«Result «, x) Result 2.5 |
%= | Returns the remainder of the division of the value to the right of the equal with the variable | >>> x=5; x%=2; print(«Result «, x) Result 1 |
//= | Returns the quotient of the division of the value to the right of the equal with the variable | >>> x=5; x//=2; print(«Result «, x) Results 2 |
**= | Calculates the exponent to the variable X according to the value to the right of the equal | >>> x=5; x**=2; print(«Result «, x) Result 25 |
Arithmetic operators
They are the ones we use to make a arithmetic and mathematical operations.
Operator | Description | Example |
+ | Add numeric type values | >>> 5+2 7 |
– | Subtracts numeric type values | >>> 5-2 3 |
– | Assigns a negative value to a numeric data | >>> -5 -5 |
* | Multiplies numeric type values | >>> 5*2 10 |
/ | Splits numeric type values | >>> 5/2 2.5 |
% | Returns the remainder of the division between values of type numeric | >>> 5%2 1 |
// | Returns the quotient of the division between values of numeric type | >>> 5//2 2 |
** | Calculates the exponent between numerical type values | >>> 5**2 25 |
Relational operators
They are the ones we use to compare two values. Returns a boolean value (true or false) depending on the condition.
Operator | Description | Example |
== | Compare the values to see if they are the same | >>> 5==2 False |
!= | Compare the values to see if they are different | >>> 5!=2 true |
< | Compares that the value on the left is less than the one on the right | >>> 5<2 False |
> | Compares that the value on the left is greater than the one on the right | >>> 5>2 true |
<= | Compares that the value on the left is less than or equal to the one on the right | >>> 5<=2 False |
>= | Compares that the value on the left is greater than or equal to the one on the right | >>> 5>=2 true |
Logical operators
They are the ones we use to work with boolean values.
Operator | Description | Example |
and | Check that the left and right value is met | >>> True and False False |
or | Checks that the left or right value is true | >>> True or False true |
not | Returns the opposite value of the boolean value | >>> not True False |
Surely you knew most of them, in that case, you can always put them into practice to learn more.