Chapter 4-
Basic Operators
Arithmetic Operators
These are operators that have the ability to perform mathematical or arithmetic operations that are going to be fundamental or widely used in this programming language, and these operators are in turn subdivided into:
Sum Operator: its symbol is (+), and its function is to add the values of numerical data. Its syntax is written as follows:
> 6 + 4
® 10
Subtract Operator: its symbol is the (-), and its function is to subtract the values of numerical data types. Its syntax can be written like this:
> 4 – 3
® 1
Multiplication Operator: Its symbol is (*), and its function are to multiply the values of numerical data types.
Its syntax can be written like this:
> 3 * 2
® 6
Division Operator: Its symbol is (/); the result offered by this operator is a real number. Its syntax is written like this:
> 3.5 / 2
® 1.75
Module Operator: its symbol is (%); its function is to return the rest of the division between the two operators. In the following example, we have that division 8 is made between 5 that is equal to 1 with 3 of rest, the reason why its module will be 3.
Its syntax is written like this:
> 8 % 5
® 3
Exponent Operator: its symbol is (**), and its function is to calculate the exponent between numerical data type values. Its syntax is written like this:
> 3 ** 2
® 9
Whole Division Operator: its symbol is (//); in this case, the result it returns is only the whole part.
Its syntax is written like this:
> 3,5 // 2
® 1.0
However, if integer operators are used, the Python language will determine that it wants the result variable to be an integer as well, this way you would have the following:
> 3 / 2
> 3 // 2
If we want to obtain decimals in this particular case, one option is to make one of our numbers real. For example:
> 3.0 / 2
Comparison Operators
The comparison operators are those that will be used to compare values and return; as a result, the True or False response as the case may be, as a result of the condition applied.
Operator Equal to: its symbol is ( = = ), its function is to determine if two values are exactly the same.
For example:
> 3 = = 3
® True
> 5 = = 1
® False
Operator Different than: its symbol is ( ! = ); its function is to determine if two values are different and if so, the result will be True. For example:
> 3 ! = 4
® True
> 3 ! = 3
® False
Operator Greater than: its symbol is ( > ); its function is to determine if the value on the left is greater than the value on the right and if so, the result it yields is True. For example:
> 5 > 3
® True
> 3 > 8
® False
Operator Less than: its symbol is ( < ); its function is to determine if the left value is less than the right one, and if so, it gives True result. For example:
> 3 < 5
® True
> 8 < 3
® False
Operator ( > = ), its function is to determine that the value on the left is greater than the value on the right, if so the result returned is True. For example:
> 8 > = 1
® True
> 8 > = 8
® True
> 3 > = 8
® False
Operator ( < = ), its function is to evaluate that the value on its left is less than the one on the right, if so the result returned is True. For example:
> 8 < = 10
® True
> 8 < = 8
® True
> 10 < = 8
® False
Logical Operators
Logical operators are the and, or, not. Their main function is to check if two or more operators are true or false, and as a result, returns a True or False. It is very common that this type of operator is used in conditionals to return a Boolean by comparing several elements.
Making a parenthesis in operators, we have that the storage of true and false values in Python are of the bool type, and was named thus by the British mathematician George Boole, who created the Boolean algebra. There are only two True and False Boolean values, and it is important to capitalize them because, in lower cases, they are not Boolean but simple phrases.
The semantics or meaning of these operators is similar to their English meaning, for example, if we have the following expression:
X > 0 and x < 8, this will be true if indeed x is greater than zero and less than 8.
In the case of or, we have the following example:
> N=12
> N % 6 = = 0 or n % 8 = = 0
® True
It will be true if any of the conditions is indeed true, that is, if n is a
number divisible by 6 or by 8.
In the case of the logical operator not, what happens is that it denies a Boolean expression, so, if we have, for example:
not ( x < y ) will be true if x < y is false, that is, if x is greater than y.