After we getting to know about operators and their usage in Python we want introduce the use of functions in Python.
Built-in functions are predefined, provided codes that are distributed together with Python. These functions will make your life a lot easier because you do not have to implement frequently and commonly used codes at your own. In this understanding they expand the possibilities given by the introduced operators. So let's get started!
Functions are written for a specific data type. Data types in general and in Python will be introduced in the next chapters. As a starting point it is sufficient to know that there exist different data types for varying purposes and that not every function works for every data type. With your already learned knowledge you will see that there are specialized functions that only work for numbers and others that only work for characters. Besides this there are also functions that work for all datatypes. We want to start with these.
Note: A function is used by typing the name of the function and putting the value (formally known as argument) in brackets:
Function to get the data type of a variable type()
:
a_word = "hello"
a_number = 9
# we expect String as datatype
type(a_word)
str
# as 9 is a number, we expect an according datatype (int == Integer)
type(a_number)
int
Function to display the content of a variable or more general the given argument print()
:
print(a_word)
hello
print(a_number)
9
As we want to introduce Python as a calculator we want to have a closer look at functions that work for numbers.
Note: Only very few functions are available by default in Python. Most functions are conflated in dedicated modules. To use these functions you must load the required module first. A specific module is loaded with the
import
command. You can find a list of all Python modules in the Python documentation.
To get access to commonly used mathematical function for calculations we must load the math
module at first:
import math
Note: It is recommented to
import
modules with an alias. This will allow yourself and others to identify from which module or package a function is used. This is especially important if there are two function from two different modules with the same name. That's why we want to adapt ourimport
statement a little bit:
import math as m
Function to get the absolute value of a number abs()
:
abs(9 - 12)
3
Function to calculate the square root of a number sqrt()
:
m.sqrt(64)
8.0
Function to get a specific value based on the $e$ function exp()
:
m.exp(1)
2.718281828459045
Function to get the logarithm to the base of $e$ log()
:
m.log(10)
2.302585092994046
Function to calculate the logarithm to the base of $10$ log10()
:
m.log10(1000)
3.0
Function to calculate the logarithm to a user defined base log(<value>, <base>)
:
m.log(81, 3)
4.0
Note: As you can see it is also possible to call a function with more than one argument. If you want to call a function with more than one argument you have to seperate each argument by
,
. In general:function(<argument_1>, <argument_2>, ... ,<argument_n>)
Using mathematical constants like $\pi$:
m.pi
3.141592653589793
Note: constants are a special type of variable. They differ in so far that the actual value of a constant cannot be changed after it is assigned. Because constants, like variables, are a storage for values they are used without brackets.
besides $\pi$ the following constants are provided over the math
package:
Mathematical constant | Python correspondent |
---|---|
$e$ | math.e |
$\tau$ | math.tau |
$\infty$ | math.inf |
Trigonometric functions (arguments in radians) sin()
, cos()
, tan()
:
m.sin(m.pi / 2)
1.0
m.cos(m.pi / 2)
6.123233995736766e-17
m.tan(0)
0.0
Inverse trigonometric functions asin()
, acos()
, atan()
:
m.asin(0)
0.0
m.acos(1)
0.0
m.atan(m.pi)
1.2626272556789115
Exercise: Check whether the cosine of 360° within the unit circle is 1!
### your solution
m.cos(2 * m.pi)
1.0
Function to round x to n decimal places round(<number>, <decimal_places>)
:
round(m.pi, 2)
3.14
Function to round down to the nearest integer number floor()
:
m.floor(m.pi)
3
Function to round up to the nearest integer number ceil()
:
m.ceil(m.pi)
4
Advanced Exercise: Consider Earth as a ball with a radius of 6371 km and calculate the distance between two meridians at the equator and at 23 degree. Round your answer to 1 decimal place! Bear in mind that the trigonometric functions in R expect angles in radians!
### your solution
distance_equator = round((2 * m.pi * 6371) / 360 * m.cos((m.pi / 180) * 0), 1)
print("The distance between two meridians at the equator is", distance_equator, "km.")
The distance between two meridians at the equator is 111.2 km.
distance_23_degree = round((2 * m.pi * 6371) / 360 * m.cos((m.pi / 180) * 23), 1)
print("The distance between two meridians at the 23 degree is", distance_23_degree, "km.")
The distance between two meridians at the 23 degree is 102.4 km.
A summary of all introduced functions is given below.
Python function | Mathematical correspondent |
---|---|
abs(9) |
|$9$| |
math.sqrt(64) |
$\sqrt {64}$ |
math.exp(2) |
$e^2$ |
math.log(5) |
$\ln(5)$ |
math.log10(1000) |
$\log_{10}(1000)$ |
math.log(27, 3) |
$\log_{3}(27)$ |
math.sin(math.pi) |
$\sin(\pi)$ |
math.cos(math.pi) |
$\cos(\pi)$ |
math.tan(math.pi) |
$\tan(\pi)$ |
math.asin(math.pi) |
$\arcsin(\pi)$ |
math.acos(math.pi) |
$\arccos(\pi)$ |
math.atan(math.pi) |
$\arctan(\pi)$ |
Citation
The E-Learning project SOGA-Py was developed at the Department of Earth Sciences by Annette Rudolph, Joachim Krois and Kai Hartmann. You can reach us via mail by soga[at]zedat.fu-berlin.de.
Please cite as follow: Rudolph, A., Krois, J., Hartmann, K. (2023): Statistics and Geodata Analysis using Python (SOGA-Py). Department of Earth Sciences, Freie Universitaet Berlin.