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.
{ "data": { "sessionMaterial": { "id": "session-material:2018/pyladies-en-prague:loops:2", "title": "Functions", "html": "\n \n \n\n <p>If you would like to do some calculations with the number π (pi), how would you write it?</p>\n<p>3.14159265 ?</p>\n<p>Python has a lot of built-in features. You don’t have to reinvent the wheel,\nyou just have to know where to look.</p>\n<p>We can access <em>π</em> by importing the <code>math</code> module.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">from</span> <span class=\"nn\">math</span> <span class=\"kn\">import</span> <span class=\"n\">pi</span>\n\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">pi</span><span class=\"p\">)</span>\n</pre></div><p>As you can see, π is a bit hidden. Compared to <code>print</code> or <code>if</code>, which everyone needs, \nnot everyone needs <code>math</code>. Let's stick with this module for a bit longer.</p>\n<h2>Expressions</h2>\n<p>In mathematics we have a lot of different operations that are implemented as \nsymbols, like + or -. The same symbols are used in Python.</p>\n<ul>\n<li>3 + 4</li>\n<li><var>a</var> - <var>b</var></li>\n</ul>\n<p>It's a bit difficult with multiplying and dividing\nbecause you can't write the usual mathematical expression\non your keyboard:</p>\n<ul>\n<li>3 · 4</li>\n<li>¾</li>\n</ul>\n<p>Mathematicians were inventing more and more complex symbols \nthat cannot be just as easily replicated by programmers:</p>\n<ul>\n<li><var>x</var>²</li>\n<li><var>x</var> ≤ <var>y</var></li>\n<li>sin θ</li>\n<li>Γ(<var>x</var>)</li>\n<li>∫<var>x</var></li>\n<li>⌊<var>x</var>⌋</li>\n<li><var>a</var> ★ <var>b</var></li>\n<li><var>a</var> ⨁ <var>b</var></li>\n</ul>\n<p>There are even programming languages that need a\nspecial keyboard. But their programs can't be easily \nwritten and they aren't readable.</p>\n<div class=\"admonition note\"><p>For example this is program written in language APL:</p>\n<!--z http://catpad.net/michael/apl/ -->\n\n<div class=\"highlight\"><pre><code>⍎’⎕’,∈Nρ⊂S←’←⎕←(3=T)∨M∧2=T←⊃+/(V⌽”⊂M),(V⊖”⊂M),(V,⌽V)⌽”(V,V←1¯1)⊖”⊂M’</code></pre></div></div><p>There are relatively few operators in Python.\nAnd we already know almost half of them!\nSome operators are words.\nHere are all of them:</p>\n<div>\n <code>==</code> <code>!=</code>\n <code><</code> <code>></code>\n <code><=</code> <code>>=</code>\n <code class=\"text-muted\">|</code> <code class=\"text-muted\">^</code>\n <code class=\"text-muted\">&</code>\n <code class=\"text-muted\"><<</code> <code class=\"muted\">>></code>\n <code>+</code> <code>-</code>\n <code>*</code> <code class=\"text-muted\">@</code> <code>/</code>\n <code>//</code> <code>%</code>\n <code class=\"text-muted\">~</code>\n <code>**</code>\n <code class=\"text-muted\">[ ]</code> <code class=\"text-muted\">( )</code>\n <code class=\"text-muted\">{ }</code>\n <code class=\"text-muted\">.</code>\n</div><div>\n <code class=\"muted\">lambda</code>\n <code class=\"muted\">if else</code>\n <code>or</code> <code>and</code> <code>not</code>\n <code class=\"muted\">in</code> <code class=\"muted\">not in</code>\n <code class=\"muted\">is</code> <code class=\"muted\">is not</code>\n</div><p>It is clear now that some operations that we want to do in a program \ncannot be expressed by these operators.</p>\n<p>How to deal with this?</p>\n<p>One way which we have already mentioned is to define the operation in words.</p>\n<ul>\n<li><var>x</var> = sin <var>a</var></li>\n</ul>\n<p>And we can write that on our keyboards!\nWe just have to add parentheses (some editors will do that for us) to make it \nclear to what the operation applies:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">x</span> <span class=\"o\">=</span> <span class=\"n\">sin</span><span class=\"p\">(</span><span class=\"n\">a</span><span class=\"p\">)</span>\n</pre></div><p>But first of all you have to <em>import</em> <code>sin</code>,\nin the same way as you already imported <code>pi</code>.\nSo the whole program will look like this:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">from</span> <span class=\"nn\">math</span> <span class=\"kn\">import</span> <span class=\"n\">sin</span>\n\n<span class=\"n\">x</span> <span class=\"o\">=</span> <span class=\"n\">sin</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">)</span> <span class=\"c1\"># (in radian)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">x</span><span class=\"p\">)</span>\n</pre></div><div class=\"admonition warning\"><p class=\"admonition-title\">Import and files names</p>\n<p>When we want to import modules, we have to pay extra attention\nhow we name our own program files.\nIf you import the module <code>math</code> into your program, your file can't\nhave name <code>math.py</code> itself.</p>\n<p>Why? Because if you are importing a module, Python will look first\ninto the folder from which you are running the program.\nIt will find the file <code>math.py</code> and will try to import the <code>sin</code> function from there.\nAnd of course it won't find it.</p>\n</div><h2>Call functions</h2>\n<p>We call the function by its <em>name</em>.</p>\n<p>The name looks like a variable -– actually, it <em>is</em> a variable, the\nonly difference is that instead of a number or a string, we have a function stored inside.</p>\n<p>After the name of the function, we have parentheses where we enclose the <em>argument</em> \n(or <em>input</em>) for the function. This is the information which our function will work with.\nIn our example, <code>sin()</code> will calculate the <em>sine</em></p>\n<p>The <em>return value</em> of a function is a value that can be\nassigned to a variable.</p>\n<div class=\"highlight\"><pre><code> function name\n │\n ╭┴╮\n x = sin(1)\n ▲ ╰┬╯\n │ argument\n │\n ╰── return value</code></pre></div><p>Or we can use it in other operations:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">a</span> <span class=\"o\">=</span> <span class=\"n\">sin</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">)</span> <span class=\"o\">+</span> <span class=\"n\">cos</span><span class=\"p\">(</span><span class=\"mi\">2</span><span class=\"p\">)</span>\n</pre></div><p>Or we can use it in an <code>if</code> condition:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">if</span> <span class=\"n\">sin</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">)</span> <span class=\"o\"><</span> <span class=\"mi\">3</span><span class=\"p\">:</span>\n</pre></div><p>Or, even use it as an input for a different function:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">sin</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">))</span>\n</pre></div><p>… etc.</p>\n<h3>Arguments</h3>\n<p>To some functions, we can pass multiple arguments. An example is <code>print</code>, \nwhich prints all its arguments consecutively. We separate the arguments by comma:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"mi\">3</span><span class=\"p\">)</span>\n</pre></div><div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s2\">"One plus two equals"</span><span class=\"p\">,</span> <span class=\"mi\">1</span> <span class=\"o\">+</span> <span class=\"mi\">2</span><span class=\"p\">)</span>\n</pre></div><p>Some functions do not need any argument, the function <code>print</code> is again an example for this. \nBut we still have to write the parentheses, even if they are empty.</p>\n<p>Guess what <code>print</code> without arguments will do?</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">()</span>\n</pre></div><div class=\"solution\" id=\"solution-0\">\n <h3>Řešení</h3>\n <div class=\"solution-cover\">\n <a href=\"/2018/pyladies-en-prague/beginners-en/functions/index/solutions/0/\"><span class=\"link-text\">Ukázat řešení</span></a>\n </div>\n <div class=\"solution-body\" aria-hidden=\"true\">\n <p>The function <code>print</code> without arguments will print an empty line.</p>\n<p>It's exactly following the definition -- the function <code>print</code> will write all\nits arguments on a line.</p>\n </div>\n</div><h3>Functions have to be called</h3>\n<p>Be careful to write the parentheses, otherwise, the function is not called. \nYou will not get the return value, but the function itself! Let’s try the following examples:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">from</span> <span class=\"nn\">math</span> <span class=\"kn\">import</span> <span class=\"n\">sin</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">sin</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">))</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">sin</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">sin</span> <span class=\"o\">+</span> <span class=\"mi\">1</span><span class=\"p\">)</span>\n</pre></div><h3>Named arguments</h3>\n<p>Some functions can also work with <em>named</em> arguments. \nThey are written similarly to the assignment of a variable, with an equals sign, \nbut inside the parentheses:</p>\n<p>For example, the function <code>print</code> ends with printing a newline character at the end of a line by default,\nbut we can chnage that by using the named argument <code>end</code>, and print something else.</p>\n<div class=\"admonition note\"><p>We have to write this into a .py file to run it because we won't\nbe able to see it properly in the interactive console.</p>\n</div><div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'1 + 2'</span><span class=\"p\">,</span> <span class=\"n\">end</span><span class=\"o\">=</span><span class=\"s1\">' '</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'='</span><span class=\"p\">,</span> <span class=\"n\">end</span><span class=\"o\">=</span><span class=\"s1\">' '</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"mi\">1</span> <span class=\"o\">+</span> <span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"n\">end</span><span class=\"o\">=</span><span class=\"s1\">'!'</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">()</span>\n</pre></div><h2>Useful functions</h2>\n<p>At last, we will look at some basic functions which are built in.\nYou can also download this \n<a href=\"https://github.com/muzikovam/cheatsheets/blob/master/basic-functions/basic-functions-en.pdf\">cheatsheet</a>.</p>\n<h3>Input and output</h3>\n<p>We already know these functions.\n<code>print</code> writes non-named arguments separated by spaces into the output.\nIt will write a named argument <code>end</code> in the end of a line (the default is a newline character).\nAnd another named argument <code>sep</code> defines what will be written between each argument (default is a space character).</p>\n<div class=\"admonition note\"><p>We recommend to run the following example\nfrom a file, not from the Python console.</p>\n</div><div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"s2\">"two"</span><span class=\"p\">,</span> <span class=\"bp\">False</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"n\">end</span><span class=\"o\">=</span><span class=\"s2\">" "</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"mi\">4</span><span class=\"p\">,</span> <span class=\"n\">sep</span><span class=\"o\">=</span><span class=\"s2\">", "</span><span class=\"p\">)</span>\n</pre></div><p>The basic function for input is obviously <code>input</code>.\nIt will print the question (or whatever you type in),\ncollect the input from the user, and return it\nas a string.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"nb\">input</span><span class=\"p\">(</span><span class=\"s1\">'Enter input: '</span><span class=\"p\">)</span>\n</pre></div><h3>Type conversion (type casting)</h3>\n<p>In case we don’t want to work just with strings, here is a group of \nfunctions that can convert strings to numbers and back.\nBut what to do when we don't won't to work with a string but, for example, with a number?\nThere's a group of functions that can help us convert strings to numbers and back.\nEach of the three <em>types</em> of variables that we currently know\nhas a function which takes a value (as an argument) and returns it as a\nvalue of its own type. \nFor <em>integers</em> there's the function <code>int()</code>, for <em>floating points</em> there's\n<code>float</code>, and for <em>strings</em> there's <code>str()</code>.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"n\">x</span><span class=\"p\">)</span> <span class=\"c1\"># conversion to integer</span>\n<span class=\"nb\">float</span><span class=\"p\">(</span><span class=\"n\">x</span><span class=\"p\">)</span> <span class=\"c1\"># conversion to real number</span>\n\n<span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"n\">x</span><span class=\"p\">)</span> <span class=\"c1\"># conversion to string</span>\n</pre></div><p>Examples:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"mi\">3</span> <span class=\"o\">==</span> <span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"s1\">'3'</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"mf\">3.0</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"mf\">3.141</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">)</span>\n<span class=\"mf\">8.12</span> <span class=\"o\">==</span> <span class=\"nb\">float</span><span class=\"p\">(</span><span class=\"s1\">'8.12'</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"nb\">float</span><span class=\"p\">(</span><span class=\"mf\">8.12</span><span class=\"p\">)</span>\n<span class=\"mf\">8.0</span> <span class=\"o\">==</span> <span class=\"nb\">float</span><span class=\"p\">(</span><span class=\"mi\">8</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"nb\">float</span><span class=\"p\">(</span><span class=\"s1\">'8'</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"nb\">float</span><span class=\"p\">(</span><span class=\"mf\">8.0</span><span class=\"p\">)</span>\n<span class=\"s1\">'3'</span> <span class=\"o\">==</span> <span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"s1\">'3'</span><span class=\"p\">)</span>\n<span class=\"s1\">'3.141'</span> <span class=\"o\">==</span> <span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"mf\">3.141</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"s1\">'3.141'</span><span class=\"p\">)</span>\n</pre></div><p>But not all conversions are possible:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"s1\">'blablabla'</span><span class=\"p\">)</span> <span class=\"c1\"># error!</span>\n<span class=\"nb\">float</span><span class=\"p\">(</span><span class=\"s1\">'blablabla'</span><span class=\"p\">)</span> <span class=\"c1\"># error!</span>\n<span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"s1\">'8.9'</span><span class=\"p\">)</span> <span class=\"c1\"># error!</span>\n</pre></div><p>We will tell you how to deal with errors at some other time.</p>\n<h3>Mathematical functions</h3>\n<p>Maths is sometimes useful so let's have a look how to work \nwith numbers in Python :)</p>\n<p>There is one mathematical function which is always available:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"nb\">round</span><span class=\"p\">(</span><span class=\"n\">number</span><span class=\"p\">)</span> <span class=\"c1\"># rounding</span>\n</pre></div><p>Lots of others are imported from the <code>math</code> module:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">from</span> <span class=\"nn\">math</span> <span class=\"kn\">import</span> <span class=\"n\">sin</span><span class=\"p\">,</span> <span class=\"n\">cos</span><span class=\"p\">,</span> <span class=\"n\">tan</span><span class=\"p\">,</span> <span class=\"n\">sqrt</span><span class=\"p\">,</span> <span class=\"n\">floor</span><span class=\"p\">,</span> <span class=\"n\">ceil</span>\n\n<span class=\"n\">sin</span><span class=\"p\">(</span><span class=\"n\">angle</span><span class=\"p\">)</span> <span class=\"c1\"># sine</span>\n<span class=\"n\">cos</span><span class=\"p\">(</span><span class=\"n\">angle</span><span class=\"p\">)</span> <span class=\"c1\"># cosine</span>\n<span class=\"n\">tan</span><span class=\"p\">(</span><span class=\"n\">angle</span><span class=\"p\">)</span> <span class=\"c1\"># tangent</span>\n<span class=\"n\">sqrt</span><span class=\"p\">(</span><span class=\"n\">number</span><span class=\"p\">)</span> <span class=\"c1\"># square root</span>\n\n<span class=\"n\">floor</span><span class=\"p\">(</span><span class=\"n\">angle</span><span class=\"p\">)</span> <span class=\"c1\"># rounding down</span>\n<span class=\"n\">ceil</span><span class=\"p\">(</span><span class=\"n\">angle</span><span class=\"p\">)</span> <span class=\"c1\"># rounding up</span>\n</pre></div><h3>Help</h3>\n<p>There are some more functions that help programmers:\nYou can get help regarding a specific function from the program \nitself (or Python console) by using the help function.\nIt's sometimes useful even for beginners, but if\nnot - use Google.</p>\n<p>Help will be shown, depending on your operating system,\neither in the browser or right there in the terminal.\nIf the help is too long for the terminal, you can browse pages using\n (<kbd>↑</kbd>, <kbd>↓</kbd>,\n<kbd>PgUp</kbd>, <kbd>PgDn</kbd>) and you can get "out" by pressing the\nkey <kbd>Q</kbd> (like <em>Quit</em>).</p>\n<p>You can get help for <code>print</code> like that:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">help</span><span class=\"p\">(</span><span class=\"k\">print</span><span class=\"p\">)</span>\n</pre></div><p>You can also get help for a whole module:.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">import</span> <span class=\"nn\">math</span>\n\n<span class=\"n\">help</span><span class=\"p\">(</span><span class=\"n\">math</span><span class=\"p\">)</span>\n</pre></div><h3>Random</h3>\n<p>Last but not least, we will look at two functions from\n<code>random</code> which are very useful for games.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">from</span> <span class=\"nn\">random</span> <span class=\"kn\">import</span> <span class=\"n\">randrange</span><span class=\"p\">,</span> <span class=\"n\">uniform</span>\n\n<span class=\"n\">randrange</span><span class=\"p\">(</span><span class=\"n\">a</span><span class=\"p\">,</span> <span class=\"n\">b</span><span class=\"p\">)</span> <span class=\"c1\"># random integer from a to b-1</span>\n<span class=\"n\">uniform</span><span class=\"p\">(</span><span class=\"n\">a</span><span class=\"p\">,</span> <span class=\"n\">b</span><span class=\"p\">)</span> <span class=\"c1\"># random float from a to b</span>\n</pre></div><p>Beware that the <code>randrange(a, b)</code> never returns <code>b</code>. \nIf we need to randomly choose between 3 options, use <code>randrange(0,3)</code> \nwhich returns <code>0</code>, <code>1</code>, or <code>2</code>:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">from</span> <span class=\"nn\">random</span> <span class=\"kn\">import</span> <span class=\"n\">randrange</span>\n\n<span class=\"n\">number</span> <span class=\"o\">=</span> <span class=\"n\">randrange</span><span class=\"p\">(</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"mi\">3</span><span class=\"p\">)</span> <span class=\"c1\"># number - 0, 1, or 2</span>\n<span class=\"k\">if</span> <span class=\"n\">number</span> <span class=\"o\">==</span> <span class=\"mi\">0</span><span class=\"p\">:</span>\n <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'Circle'</span><span class=\"p\">)</span>\n<span class=\"k\">elif</span> <span class=\"n\">number</span> <span class=\"o\">==</span> <span class=\"mi\">1</span><span class=\"p\">:</span>\n <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'Square'</span><span class=\"p\">)</span>\n<span class=\"k\">else</span><span class=\"p\">:</span> <span class=\"c1\"># 2</span>\n <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'Triangle'</span><span class=\"p\">)</span>\n</pre></div><div class=\"admonition note\"><p>Remember that when you want to import the <code>random</code> module, you can't \nname your own file <code>random.py</code>.</p>\n</div><h3>And more...</h3>\n<p>There are a lot more functions within Python itself,\nalthough you probably won't understand them at the beginning.\nAll of them are in the Python documentation, e.g.:\n<a href=\"https://docs.python.org/3/library/functions.html\">built-in</a>,\n<a href=\"https://docs.python.org/3/library/math.html\">maths</a>.</p>\n\n\n " } } }