Skip to content

Commit 6b7126d

Browse files
committed
Merge branch 'wip' of github.com:OpenTechSchool/python-beginners into wip
2 parents f0b0238 + f8957fe commit 6b7126d

File tree

5 files changed

+127
-2
lines changed

5 files changed

+127
-2
lines changed

docs/en/conditional_loops.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Conditional Loops
2+
*****************
3+
4+
Introduction
5+
============
6+
7+
8+
Exercise
9+
========
10+
11+

docs/en/conditionals.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Conditional statements
2+
**********************
3+
Introduction
4+
============
5+
6+
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+
8+
To know if a condition is *true* of *false*, we need a new type of data:
9+
the booleans. They allow logic operations. A logic statement or operation
10+
can be avaluated to be true or false. Our conditional statement can then be
11+
understood like this: **if** *(evaluation of a condition returns true)* **then** *do some operation* **else** *do an other operation*. And any operation that can be avaluated as *true* or *false* can but put to the test. All comparisons return *true* or *false*: *=,>,<*.
12+
13+
14+
Examples
15+
========
16+
Here are some simple examples::
17+
18+
condition = True
19+
if condition:
20+
print "condition met"
21+
22+
if not condition:
23+
print "condition not met"
24+
25+
direction = -30
26+
if direction > 0 :
27+
forward(direction)
28+
else:
29+
left(180)
30+
forward (-direction)
31+
32+
33+
Exercise
34+
========
35+
36+
[some example...]
37+
38+
Bonus Exercise
39+
==============

docs/en/functions_parameters.rst

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,70 @@
11
Functions with parameters
22
*************************
3+
Introduction
4+
============
35

4-
Todo
6+
Now we know how to *factorise* this code a little. But functions as we have defined them so far are not flexible. the variables are defined inside the function,
7+
and we need to rewrite a whole function to cahnge the value of an angle, or a distance in it.
8+
9+
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:
10+
11+
Remember how we defined the function ``line_without_moving`` in the previous section::
12+
13+
def line_without_moving():
14+
forward(50)
15+
backward(50)
16+
17+
We can improve it by giving it a parameter::
18+
19+
def line_without_moving(length):
20+
forward(length)
21+
backward(length)
22+
23+
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::
24+
25+
line_without_moving(50)
26+
line_without_moving(40)
27+
28+
We have been using functions with parameters since the beginning of the tutorial with the *forward*, *left*, etc...
29+
30+
31+
And we can put as many arguments (or parameters) as we want, separating them with commas and giving them different names::
32+
def tilted_line_without_moving(length,angle):
33+
left(angle)
34+
forward(length)
35+
backward(length)
36+
37+
38+
A parameterised function for a variable size hexagon
39+
====================================================
40+
41+
Exercise
42+
--------
43+
Write a function that takes allows you to draw hexagons of any size you want, each time you call the function.
44+
45+
46+
47+
A function of several parameters
48+
================================
49+
50+
Exercise
51+
--------
52+
53+
Write a function that draws a honeycomb with a variable number of hexagons, of variable sizes
54+
55+
56+
Solution
57+
--------
58+
59+
::
60+
61+
def hexagon(size):
62+
for i in range(6):
63+
forward(size)
64+
left(60)
65+
def honeycomb(size,count):
66+
for i in range(count):
67+
hexagon(size)
68+
forward(size)
69+
right(60)
70+

docs/en/index.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ Table of contents:
1313
functions
1414
loops
1515
functions_parameters
16-
16+
conditionals
17+
logical_operators
18+
conditional_loops

docs/en/logical_operators.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Logical operators
2+
=========================
3+
4+
We can also do some more complex things with
5+
We can return the opposite of a logic operation, with **not** as a keyword in front of your expression, or combine several logic statements together with logical **and** and **or** links between them (they are usually called *operators*)
6+
7+

0 commit comments

Comments
 (0)