Arithmetic Expressions? What? This is nothing more than a fancy word for “calculating”. Though, it covers a little more than only working with numbers. Arithmetic expressions can be used with number data types, booleans and with strings.
Addition and Subtraction
The most basic and most intuitive arithmetic expressions are addition (indicated with a plus “+”) and subtraction (indicated with a minus “-“). They can be used for integers and floats.
a = 5 + 3 b = 7.3 + 32.9 c = 43 - 3.6
Note: In the examples, the results of the calculations are stored in variables as you have not seen how to use the results directly, but basically you can also use the result of an arithmetic expression as an input for functions or use them otherwise.
The addition expression can also be used on strings. Obviously you can’t calculate with words and characters, but you can add them together to form a new string. This is called “concatenation”.
first_name = "Harry" last_name + "Hendrickson" full_name = first_name + last_name
Note: In this example, the resulting string would be “HarryHendrickson” as there is no white space between the words. This needs to be added separately. You could add a pair of (double) quotes containing a single white space character to separate the two words.
full_name = first_name + " " + last_name
Note: You can’t add a number and a string. Here it is important to distinguish a number (e.g. 5) from a number in form of a string (e.g. ‘5’) as they will not work together. The reason for this is that Python will not be able to know if you want to use the string as a number (e.g. to create 10 as a result) or if you want to use the number as a string (e.g. to create the string ’55’ as a result).
Multiplication and Division
Another very intuitive expression is multiplication (indicated with an asterisk “*”). Multiplication is used to multiply numbers to get the resulting product.
a = 2 * 4 b = a * 3.4 c = 15 * (-0.5)
Like with addition, you can use the multiply operator for strings as well. Here, the string will be repeated the amount of times specified by the number after the multiplication symbol.
a = "ha"*3 b = 5*'Nope! '
The examples above will result in “a = ‘hahaha‘ ” and “b = ‘Nope! Nope! Nope! Nope! Nope! ‘ “.
The division (indicated with a forward slash “/”) behaves differently for integers and floats and also different for Python 2 and Python 3. As you are learning Python 2 in this guide (as it is used in ROS), it does make a difference if you divide integers of floats. (For Python 3, there is basically no difference.)
When using the division operator with floating point numbers, the result will be exactly what you expect. The result is the result of the first number divided by the second number. The result will be a float type number regardless if the resulting number has a decimal value or not. This holds even true if only one of the numbers is a floating point number and the other is an integer type.
a = 10.5 / 2.0 b = 4 / 2.0
In the above example, “a = 5.25” and “b = 2.0”. Everything as you would expect. In Python 2, when dividing an integer by another integer, the result will always be an integer.
c = 10 / 2 d = 7 / 2
In the above example, “c = 5” and “d = 3”. Wait what?! As you can see “c” is being calculated as it should, but the variable “d” is wrong? What is happening is, that the result is being forced to be an integer value which means that everything behind the decimal point will be lost, thus “0.5” is just being lost. (In Python 3, the result would simple be a float instead.)
Okay, not it will become a little more complicated. The modulo operator is very useful to verify the property of some numbers like to see if it is an even number or not. It is only used for integer type numbers and represents the remainder of an Euclidean division which is defined by:
a = b * q + r
where “q” is the quotient and “r” the remainder. “a” is the initial number that is divided and “b” is the number that “a” is divided by. In other words, if “a” is divided by “b”, the result is “q” if “a” is dividable by “b”. If not, “q” would be a number with a decimal value. But in the formula above, “q” is always an integer which means that there is a remainder “r” that will be left out. If “r” is zero, “a” can be divided by “b”.
The modulo operator is indicated with a procent sign (“%”). To stick with the same variable names as the example formula:
r = a % b pizza_left = 8 % 3
One example where you also can use the modulo operator is to keep values within a specific range. For example, when a wheel is spinning, it will turn 360 degrees before it returns to the same position. So if it rotates 380 degrees or 20 degrees, the position is the same. Hence:
resulting_angle = 380 % 360 number_of_rotations = 380 % 360
As you can see, the integer division and the modulo operator can go quite well hand in hand.
Exponentiation is a way to calculate a number multiplied with itself a certain amount of times. This can be part of a formula such as A = r²*Pi which equals to:
area = r * r * pi
This is quickly done for a small exponent such as given above (the exponent is equal to 2). For the propper way to write exponentiation, the exponent can either be expressed with the math module (you will see about modules later) or by a simple build in notation with two asterisks.
area = radius**2 * pi
The above solution would be the prefered way to express exponential expressions.
To calculate the square root, again you could use the math module of Python or you can use a simple trick. When you think about the square root of a number, you can also express it as a number with the exponent of 0.5 which will get you the same result.
area = radius**0.5
This notation will probably be the most simple one.
This is about everything you need to know about the necessary tools for creating arithmetic expressions in Python 2.
Continue learning about inputs and outputs or go back to revisit complex data types.