If you would like to do some calculations with the number π (pi), how would you write it?
3.14159265 ?
Python has a lot of built-in features. You don’t have to reinvent the wheel, you just have to know where to look.
We can access π by importing the math
module.
from math import pi
print(pi)
As you can see, π is a bit hidden. Compared to print
or if
, which everyone needs,
not everyone needs math
. Let's stick with this module for a bit longer.
In mathematics we have a lot of different operations that are implemented as symbols, like + or -. The same symbols are used in Python.
It's a bit difficult with multiplying and dividing because you can't write the usual mathematical expression on your keyboard:
Mathematicians were inventing more and more complex symbols that cannot be just as easily replicated by programmers:
There are even programming languages that need a special keyboard. But their programs can't be easily written and they aren't readable.
For example this is program written in language APL:
⍎’⎕’,∈Nρ⊂S←’←⎕←(3=T)∨M∧2=T←⊃+/(V⌽”⊂M),(V⊖”⊂M),(V,⌽V)⌽”(V,V←1¯1)⊖”⊂M’
There are relatively few operators in Python. And we already know almost half of them! Some operators are words. Here are all of them:
==
!=
<
>
<=
>=
|
^
&
<<
>>
+
-
*
@
/
//
%
~
**
[ ]
( )
{ }
.
lambda
if else
or
and
not
in
not in
is
is not
It is clear now that some operations that we want to do in a program cannot be expressed by these operators.
How to deal with this?
One way which we have already mentioned is to define the operation in words.
And we can write that on our keyboards! We just have to add parentheses (some editors will do that for us) to make it clear to what the operation applies:
x = sin(a)
But first of all you have to import sin
,
in the same way as you already imported pi
.
So the whole program will look like this:
from math import sin
x = sin(1) # (in radian)
print(x)
Import and files names
When we want to import modules, we have to pay extra attention
how we name our own program files.
If you import the module math
into your program, your file can't
have name math.py
itself.
Why? Because if you are importing a module, Python will look first
into the folder from which you are running the program.
It will find the file math.py
and will try to import the sin
function from there.
And of course it won't find it.
We call the function by its name.
The name looks like a variable -– actually, it is a variable, the only difference is that instead of a number or a string, we have a function stored inside.
After the name of the function, we have parentheses where we enclose the argument
(or input) for the function. This is the information which our function will work with.
In our example, sin()
will calculate the sine
The return value of a function is a value that can be assigned to a variable.
function name
│
╭┴╮
x = sin(1)
▲ ╰┬╯
│ argument
│
╰── return value
Or we can use it in other operations:
a = sin(1) + cos(2)
Or we can use it in an if
condition:
if sin(1) < 3:
Or, even use it as an input for a different function:
print(sin(1))
… etc.
To some functions, we can pass multiple arguments. An example is print
,
which prints all its arguments consecutively. We separate the arguments by comma:
print(1, 2, 3)
print("One plus two equals", 1 + 2)
Some functions do not need any argument, the function print
is again an example for this.
But we still have to write the parentheses, even if they are empty.
Guess what print
without arguments will do?
print()
Be careful to write the parentheses, otherwise, the function is not called. You will not get the return value, but the function itself! Let’s try the following examples:
from math import sin
print(sin(1))
print(sin)
print(sin + 1)
Some functions can also work with named arguments. They are written similarly to the assignment of a variable, with an equals sign, but inside the parentheses:
For example, the function print
ends with printing a newline character at the end of a line by default,
but we can chnage that by using the named argument end
, and print something else.
We have to write this into a .py file to run it because we won't be able to see it properly in the interactive console.
print('1 + 2', end=' ')
print('=', end=' ')
print(1 + 2, end='!')
print()
At last, we will look at some basic functions which are built in. You can also download this cheatsheet.
We already know these functions.
print
writes non-named arguments separated by spaces into the output.
It will write a named argument end
in the end of a line (the default is a newline character).
And another named argument sep
defines what will be written between each argument (default is a space character).
We recommend to run the following example from a file, not from the Python console.
print(1, "two", False)
print(1, end=" ")
print(2, 3, 4, sep=", ")
The basic function for input is obviously input
.
It will print the question (or whatever you type in),
collect the input from the user, and return it
as a string.
input('Enter input: ')
In case we don’t want to work just with strings, here is a group of
functions that can convert strings to numbers and back.
But what to do when we don't won't to work with a string but, for example, with a number?
There's a group of functions that can help us convert strings to numbers and back.
Each of the three types of variables that we currently know
has a function which takes a value (as an argument) and returns it as a
value of its own type.
For integers there's the function int()
, for floating points there's
float
, and for strings there's str()
.
int(x) # conversion to integer
float(x) # conversion to real number
str(x) # conversion to string
Examples:
3 == int('3') == int(3.0) == int(3.141) == int(3)
8.12 == float('8.12') == float(8.12)
8.0 == float(8) == float('8') == float(8.0)
'3' == str(3) == str('3')
'3.141' == str(3.141) == str('3.141')
But not all conversions are possible:
int('blablabla') # error!
float('blablabla') # error!
int('8.9') # error!
We will tell you how to deal with errors at some other time.
Maths is sometimes useful so let's have a look how to work with numbers in Python :)
There is one mathematical function which is always available:
round(number) # rounding
Lots of others are imported from the math
module:
from math import sin, cos, tan, sqrt, floor, ceil
sin(angle) # sine
cos(angle) # cosine
tan(angle) # tangent
sqrt(number) # square root
floor(angle) # rounding down
ceil(angle) # rounding up
There are some more functions that help programmers: You can get help regarding a specific function from the program itself (or Python console) by using the help function. It's sometimes useful even for beginners, but if not - use Google.
Help will be shown, depending on your operating system, either in the browser or right there in the terminal. If the help is too long for the terminal, you can browse pages using (↑, ↓, PgUp, PgDn) and you can get "out" by pressing the key Q (like Quit).
You can get help for print
like that:
help(print)
You can also get help for a whole module:.
import math
help(math)
Last but not least, we will look at two functions from
random
which are very useful for games.
from random import randrange, uniform
randrange(a, b) # random integer from a to b-1
uniform(a, b) # random float from a to b
Beware that the randrange(a, b)
never returns b
.
If we need to randomly choose between 3 options, use randrange(0,3)
which returns 0
, 1
, or 2
:
from random import randrange
number = randrange(0, 3) # number - 0, 1, or 2
if number == 0:
print('Circle')
elif number == 1:
print('Square')
else: # 2
print('Triangle')
Remember that when you want to import the random
module, you can't
name your own file random.py
.
There are a lot more functions within Python itself, although you probably won't understand them at the beginning. All of them are in the Python documentation, e.g.: built-in, maths.