base = "Hi"
print(base*4)HiHiHiHi
int, float, string, boolOperators: Similarly to mathematics, the +, -, *, / are operators used to perform arithmetic operations on numbers. In Python, these operators can also be used on other data types but with caution. Complications may arise if you combine literals of different types. For example, 2 + "4" will lead to an error and 5 + True won’t crash but isn’t necessarily correct.
To use arithmetic operators correctly, you should first be aware of the different data types and their behaviour when used with arithemtic operators.
+ operatorArithmetic operations are deisigned for integers and floats. These types can also be safely used together. But, since integers have no fractional part, they will be automatically converted to floats by Python when combined to floats:
floatYou can also explicitly convert float to int if this is intended outcome.
This will truncate the decimal part of the float number, the resulting int is 3. Therefore, you must be careful when performing those conversion because this might result in loosing precision or loosing information. We say that floats are wider range than intor int are narrower than floats.
Although extremely rare, arithmetic operations with the bool type are possible in Python.
If you perform an addition, such as: True + True, Python will convert the bool into integers 1 (or 0 for False), and still performs the operation (1+1 = 2). If it’s a more complex operation, for example False / True, the values will be converted to float, therefore the result will be 0.0 / 1.0 = 0.0. Conceptually, this makes no sense, therefore we’ll tend to avoid those situations.
In summary, when performing arithmetic operation, ensure all the variables are in the correct type, don’t mix strings with numeric variables, and avoid using booleans.
Here is an extensive table with all opertors applied to string, int and float:
| Operator | Name | string with string |
int with int |
float with float |
int with float |
|---|---|---|---|---|---|
+ |
Addition | "H" + "i" → "Hi" |
3 + 2 → 5 |
3.2 + 2.5 → 5.7 |
3 + 2.0 → 5.0 |
- |
Subtraction | Error ❌ | 3 - 2 → 1 |
3.2 - 2.5 → 0.7 |
3 - 2.0 → 1.0 |
* |
Multiplication | Error ❌ | 3 * 2 → 6 |
3.5 * 2.0 → 7.0 |
3 * 2.0 → 6.0 |
/ |
Division | Error ❌ | 3 / 2 → 1.5 |
3.0 / 2.0 → 1.5 |
3 / 2.0 → 1.5 |
// |
Floor Division | Error ❌ | 3 // 2 → 1 |
4.0 // 3.0 → 1.0 |
4 // 2.0 → 2.0 |
% |
Modulo | Error ❌ | 10 % 7 → 3 |
7.0 % 5.0 → 2.0 |
7 % 5.0 → 2.0 |
** |
Power | Error ❌ | 4 ** 2 → 16 |
4.0 ** 2.0 → 16.0 |
4 ** 2.0 → 16.0 |
%The modulus operation returns the remainder of a long division (e.g. 7 % 5 = 2). It’s most commonly used on integers and rarely used on floats. If is an easy way to verify if a number is divisible by another number (remainder is 0).
To grasp this operator, it’s essential to recall what a long division remainder is. Consider the following division: \[ 7/5 = 1.4 = 1 + 2/5 \]
int, the result will be an intIf performing this operation with two float, the result will be a float (e.g.: 7.0 % 5.0 = 2.0)
If performing this operation with an int and a float, the int is automatically converted into the wider type float and the result is a float.
This operator doesn’t apply to string literals and will cause an error if used on strings.
This operator is useful to determine if a number is odd or even. If it even, the remainder of a division by 2 would be 0. Otherwise, if the number is odd, the remainder would be 1:
//Divides the first operand by the second and rounds the result down to the nearest whole number. It’ll compute the division, but then discard the fractional part and the result is always an integer.
It’s useful when dealing with “countable” elements such as count of people, count of objects, number of pixel. These enties can be divided further, the result of the operation must be an integer.
You can also replace the use of the // by performing a normal division with /, then converting the result into an int: x // y is the same as int(x/y)
Is the
int are used in this operation, the result is and intfloat, the result will be a float and any integer will automatically be converted to the wider type float.🎯 Test Myself: Manually execute the operations of the program below and write the expected result. Check your answers by running the code.