Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Arithmetic operators: exponentiation 

A ** (double asterisk) sign is an exponentiation (power) operator. Its left argument is the base, its right, the exponent.

Classical mathematics prefers notation with superscripts, just like this: 2^3. Pure text editors don't accept that, so Python uses ** instead, e.g., 2 ** 3. 

Take a look at our examples in the editor window. 

Note: we've surrounded the double asterisks with spaces in our examples. It's not compulsory, but it improves the readability of the code. 

The examples show a very important feature of virtually all Python numerical operators. 

Run the code and look carefully at the results it produces. Can you see any regularity here? 

Remember:  It's possible to formulate the following rules based on this result: 

  • when both ** aruguments are integers, the result is an integer, too
  • when at least one ** argument is a float, the result is a float, too

This is an important distinction to remember. 

 

Arithmetic operators: multiplication

An * (asterisk) sign is a multiplication operator.

Run the code below and check if our integer vs. float rule is still working. 

 

print(2 * 3)

print(2 * 3.)

print(2. * 3)

print(2. * 3.)

Arithmetic operators: division

A / (slash) sign is a divisional operator. 

The value in front of the slash is a dividend, the value behind the slash, a divisor.

Run the code below and analyze the results. 

print(6 / 3)

print(6 / 3.)

print(6. / 3)

print(6. / 3.)

You should see that there is an exception to the rule. 

The result produced by the division operator is always a float, regardless of whether or not the result seems to be a float at first glance:  1 / 2 , or if it looks like a pure integer: 2  /  1. 

Is this a problem? Yes, it is. It happens sometimes that you really need a division that provides an integer value, not a float. 

Fortunately, Python can help you with that. 

Arithmetic operators: exponentiation

David Khieu
Module by David Khieu, updated more than 1 year ago

Description

Arithmetic operators: exponentiation
No tags specified