You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/_sources/conditionals.txt
+12-12Lines changed: 12 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -4,19 +4,21 @@ Conditional statements
4
4
Introduction
5
5
============
6
6
7
-
So far we have accomplished predefined tasks, but in all honesty we were accomplishing no better achievements than the 18th century automata, or the music boxes following one set of instruction to the end. What makes programming so much more powerful is conditional statements. This is the ability to *test* a variable (or a name) against a value and act in one way if the condition is met by the variable or an other way if not. They are also commonly called by programmers *if statements*.
7
+
So far we have accomplished predefined tasks, but in all honesty we were accomplishing no better achievements than the 18th century automata, or the music boxes following one set of instruction to the end. What makes programming so much more powerful are conditional statements. This is the ability to *test* a variable against a value and act in one way if the condition is met by the variable or an other way if not. They are also commonly called by programmers *if statements*.
8
8
9
9
To know if a condition is *True* of *False*, we need a new type of data:
10
10
the booleans. They allow logical operations.
11
-
A logic statement or operation can be evaluated to be *true* or *false*.
11
+
A logic statement or operation can be evaluated to be *True* or *False*.
12
12
Our conditional statement can then be understood like this:
13
13
14
-
**if** *(evaluation of a condition returns true)* **then** *do some operation* **else** *do an other operation*.
15
-
And any operation that can be evaluated as *true* or *false* can but put to the test. All comparisons return *true* or *false*: *=,>,<*.
16
-
14
+
**if** *(evaluation of a condition returns true)* **then** *do some operation*
15
+
**else** *do an other operation*. And any operation that can be evaluated as
16
+
*True* or *False* can be put to the test. All comparisons return *True* or
17
+
*False*: ``==``, ``>``, ``<``.
17
18
18
19
Examples
19
20
========
21
+
20
22
Here are some simple examples::
21
23
22
24
condition = True
@@ -33,20 +35,19 @@ Here are some simple examples::
33
35
turtle.left(180)
34
36
turtle.forward (-direction)
35
37
36
-
37
38
Condition an action
38
39
===================
39
40
40
41
Exercise
41
42
--------
42
43
43
-
Let's try to make our turtle to stop when it reaches the end
44
-
of the window. Let's only take into account horizontal movements.
45
-
We want the turtle to go forward for a specified distance (taken as a function parameter) until it reaches this distance, or stop and turn around if it reaches the border of the screen.
44
+
Let's try to make our turtle stop when it reaches the end of the window. Let's
45
+
only take into account horizontal movements. We want the turtle to go forward
46
+
for a specified distance (taken as a function parameter) until it reaches this
47
+
distance and stops.
46
48
47
49
.. note::
48
50
* We start from the center of the screen, which has coordinate (0;0)
49
-
* We won't turn, until we reach the boundary our turtle wants to get away as quickly as possible
50
51
* We can get the x coordinate of our turtle with the function ``turtle.xcor()``
51
52
* We can get the width of the screen with the function ``turtle.window_width()``
Copy file name to clipboardExpand all lines: en/_sources/functions.txt
+7-3Lines changed: 7 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
-
User-defined funtions
2
-
*********************
1
+
User-defined functions
2
+
**********************
3
3
4
4
Introduction
5
5
============
@@ -29,7 +29,11 @@ You can access names in functions as well::
29
29
turtle.forward(size)
30
30
turtle.backward(size)
31
31
32
-
.. note:: Python uses *whitespaces to identify blocks of code* belonging together. While other languages use special characters (like curly brackets) in python a block is introduced with a colon at the end of the line and commands within a deeper identation level - ususally 4 spaces. The block ends with the first line with a lesser identation level.
32
+
.. note:: Python uses *whitespace to identify blocks of code* belonging
33
+
together. While other languages use special characters (like curly brackets)
34
+
in Python a block is introduced with a colon at the end of the line and
35
+
commands within a deeper indentation level - usually 4 spaces. The block ends
36
+
with the first line with a lesser indentation level.
Copy file name to clipboardExpand all lines: en/_sources/functions_parameters.txt
+18-10Lines changed: 18 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,14 @@ Functions with parameters
3
3
Introduction
4
4
============
5
5
6
-
Now we know how to *factorize* this code a little. But functions as we have defined them so far are not flexible. The variables are defined inside the function, and we need to rewrite a whole function to change the value of an angle, or a distance in it.
6
+
Now we know how to *factorize* this code a little. But functions as we have
7
+
defined them so far are not flexible. The variables are defined inside the
8
+
function, and we need to rewrite a whole function to change the value of an
9
+
angle, or a distance in it.
7
10
8
-
That is why we need to be able to give parameters, or also called *arguments* so that *names* we use in the function can be used with different values each time we call the function:
11
+
That is why we need to be able to give parameters, or also called *arguments*
12
+
so that *variables* we use in the function can be used with different values
13
+
each time we call the function:
9
14
10
15
Remember how we defined the function ``line_without_moving`` in the previous section::
11
16
@@ -19,15 +24,18 @@ We can improve it by giving it a parameter::
19
24
turtle.forward(length)
20
25
turtle.backward(length)
21
26
22
-
The parameter acts as a *name* only known inside the function's definition. We use the newly defined function by calling it with the value we want the parameter to have like this::
27
+
The parameter acts as a *variable* only known inside the function's definition.
28
+
We use the newly defined function by calling it with the value we want the
29
+
parameter to have like this::
23
30
24
31
line_without_moving(50)
25
32
line_without_moving(40)
26
33
27
-
We have been using functions with parameters since the beginning of the tutorial with the *forward*, *left*, etc...
34
+
We have been using functions with parameters since the beginning of the
35
+
tutorial with the *forward*, *left*, etc...
28
36
29
-
30
-
And we can put as many arguments (or parameters) as we want, separating them with commas and giving them different names::
37
+
And we can put as many arguments (or parameters) as we want, separating them
38
+
with commas and giving them different names::
31
39
32
40
def tilted_line_without_moving(length, angle):
33
41
turtle.left(angle)
@@ -40,18 +48,18 @@ A parameterized function for a variable size hexagon
40
48
41
49
Exercise
42
50
--------
43
-
Write a function that takes allows you to draw hexagons of any size you want, each time you call the function.
44
-
45
51
52
+
Write a function that allows you to draw hexagons of any size you want, each
53
+
time you call the function.
46
54
47
55
A function of several parameters
48
56
================================
49
57
50
58
Exercise
51
59
--------
52
60
53
-
Write a function that draws a honeycomb with a variable number of hexagons, of variable sizes
54
-
61
+
Write a function that draws a honeycomb with a variable number of hexagons, of
Copy file name to clipboardExpand all lines: en/_sources/getting_started.txt
+21-8Lines changed: 21 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -4,35 +4,48 @@ Getting started
4
4
Starting Python
5
5
===============
6
6
7
-
After installing Python on your system successfully, you can start the interactive python prompt by typing "python" in the command and press <enter>. It will show you some context information about python similar to this::
7
+
After installing Python on your system successfully, you can start the interactive Python prompt by typing ``python`` in the command and press <enter>. It will show you some context information about Python similar to this
8
+
9
+
.. code-block:: bash
8
10
9
11
Python 2.7.2 (default, Feb 1 2012, 00:28:57)
10
12
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
11
13
Type "help", "copyright", "credits" or "license" for more information.
12
14
>>>
13
15
14
16
15
-
.. note:: On windows the installer is not always setting up the "path" correctly. If that is the case on your system, you probably didn't see this message before but have to follow the steps described on the `python docs <http://docs.python.org/using/windows.html#excursus-setting-environment-variables>`_ and **restart your command line**.
17
+
.. note:: On windows the installer is not always setting up the "path" correctly. If that is the case on your system, you probably didn't see this message before but have to follow the steps described on the `Python docs <http://docs.python.org/using/windows.html#excursus-setting-environment-variables>`_ and **restart your command line**.
16
18
17
-
Those three ">>>" in the last line indicate that you are now in the interactive shell of python. Type for example::
19
+
Those three ">>>" in the last line indicate that you are now in the interactive shell of Python. Type for example::
18
20
19
21
print("Hello world")
20
22
21
-
press <enter> and see what happens. You will now see the phrase "Hello world" appear and then python will bring you back to the interactive input, where you could enter another command now::
23
+
press <enter> and see what happens. You will now see the phrase "Hello world" appear and then Python will bring you back to the interactive input, where you could enter another command now::
22
24
23
25
>>> print("Hello world")
24
26
Hello world
25
27
>>>
26
28
29
+
.. TODO: exit interpreter
27
30
28
-
Running python files
31
+
Running Python files
29
32
====================
30
33
31
-
But you don't want to type everything into the Python shell everytime but have a file with commands for python to execute instead. In order to do that you can just pass a file name to the python command in your shell and it will execute that file. Let's try that. Just open the file "hello.py" in this directory in your favourite text editor and paste the print command from above. Now save that file, go back the command line and type::
34
+
But you don't want to type everything into the Python shell every time. Instead
35
+
having a file with commands and handing that to Python to execute it would be
36
+
much better. In order to do that you can just pass a file name to the ``python``
37
+
command in your shell and it will execute that file. Let's try that. Create a
38
+
file ``hello.py`` in your current directory with your favorite text editor and
39
+
paste the print command from above. Now save that file, go back the command line
40
+
and type
41
+
42
+
.. code-block:: bash
32
43
33
-
python hello.py
44
+
python hello.py
34
45
35
-
When pressing <enter> now, the file is executed and you see the print as before. But this time, after python executed all commands from that file, it exited instead of going back to the interactive shell. In some opearting systems (and depending on your setup) you might also be able to just double click on the hello.py file to execute it
46
+
When pressing <enter> now, the file is executed and you see the print as before.
47
+
But this time, after Python executed all commands from that file, it exited
48
+
instead of going back to the interactive shell.
36
49
37
50
.. note:: Wordpad, TextEdit, Notepad and Word are **not** suited text editors. If you are unsure whether you already have a usable editor, you might want to download and install `Sublime Text2 <http://www.sublimetext.com/>`_. Sophisticated editors like this also take care of identation and help you run and debug your code.
0 commit comments