Python Tutorial 2 – Numeric Expressions

Python programming language logoPython’s interpreter can be used as a calculator quite easily. Of course, this is somewhat pointless since there are calculators built into most operating systems anyway. However, the numeric expressions that are built into Python become quite useful in general applications and are important to know.

Before you can start learning Python, you will need to install it.

The install is pretty straight forward, once you’ve got that done, you are ready to go.

Numeric Operators in Python

Python uses all of the normally expected operators, much the same as most other languages:

+ addition
– subtraction
* multiplication
/ division

Now, using these operators, we can type directly into the Python interpreter, IDLE, and it will return the result:

[sourcecode language=”python”]
# Addition:
2 + 2
# Will return 4.

# Subtraction:
4 – 2
# Will return 2.

# Multiplication:
2 * 2
# Will return 4.

# Division:
4 / 2
# Will return 2.
[/sourcecode]

Note that a # indicates a comment in Python.

If you need to take things a bit further, there are more operators commonly used in Python.

** is used to indicate to the power of, such as 8 to the power of 4, or 4 squared and so on.

Additionally, you can use brackets to override the default order that the expression will be interpreted in, the same was you would on a calculator.

[sourcecode language=”python”]
# To the power of:
8 ** 4
# Will return 4096.

4 ** 2
# Will return 16.

# Using brackets to change the order:
4 * 2 + 5
# Will return 13, whereas.
4 * ( 2 + 5 )
# Will return 28.
[/sourcecode]

Functions that you may find on a scientific calculator are available as either predefined functions, or need to be imported from a library.

[sourcecode language=”Python”]
5 + abs(-5)
# The "abs" function will return the absolute value,
# so this returns 10 rather than 0

abs(5 * -5)
# Returns 25 rather than -25.
[/sourcecode]

Importing Additional Functions

Other less common operators are not loaded into Python by default, and so they have to be imported from a library of functions. This can become time consuming when you are writing directly into IDLE. If you open up a blank Python window (File > New Window), you will be able to type as many lines of code as necessary before sending it to IDLE. If you are writing expressions such as the ones above into a separate window, you will need to add a print command before hand, otherwise Python will just evaluate them and not display them, for example:

[sourcecode language=”python”]
print 4 – 2
# Should be used so that IDLE will print the result,
# otherwise it will just interpret it without printing it.
[/sourcecode]

To load less common operators, such as the square root operator, you have to load Pythons math library. If you just a single function, such as square root, this can be done as follows:

[sourcecode language=”python”]
from math import sqrt

# The square root can then be found with this command:
sqrt(25)
# This will return 5.0.
[/sourcecode]

If you need to use multiple, less common functions it may be easier to load the entire mathematics library.

[sourcecode language=”python”]
import math
# This tells IDLE to load the mathematics library.
[/sourcecode]

Once the library has been loaded there are quite a few more functions available, similar to those used in C.

[sourcecode language=”python”]
math.ceil(2.2)
# The ceiling function will round the decimal up.

math.floor(2.2)
# The floor funtion will round the decimal down.

math.pi
# The constant, pi (3.1415926535897931).

math.pi*2
# Multiplies pi by 2.

pow(4, 2)
# The power function works similarly to the ** function.

# pow(4, 2) returns the same answer as 4 ** 2.
[/sourcecode]

There are many more available including angular, hyperbolic, trigonometric, powers and other mathematic functions. A full list is available in the Python documentation.

Fractions

So far, we have mostly seen whole numbers as a result to our answers, why do you think this is? What happens if you tell IDLE to interpret the following expressions?

[sourcecode language=”python”]
2.3 + 5.9
18.0 / 7.0
18 / 7
[/sourcecode]

The answers and explanations in the next Python tutorial, Integers and Floats.

Thanks for reading, if you have any questions or comments, feel free to let me know below.

1 thought on “Python Tutorial 2 – Numeric Expressions”

Leave a comment

en_AUEnglish (Australia)
es_ESEspañol en_AUEnglish (Australia)