Skip to content

Commit ca24f05

Browse files
Merge pull request #379 from danielquinn/gh-pages
Unify format & syntax across Python tutorial
2 parents d179c41 + 359c855 commit ca24f05

File tree

3 files changed

+69
-66
lines changed

3 files changed

+69
-66
lines changed

python/lesson2/tutorial.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ it a name then set its value.
1414

1515
Now in the REPL type:
1616

17-
>>> year = 2016
17+
>>> year = 2016
1818

1919
In this example you have now stored the value `2016` into the variable `year`.
2020
See what happens next when you type `year' into the REPL. Does it show it back
@@ -30,9 +30,9 @@ variables with the maths operations we learnt in the previous tutorial.
3030

3131
Now in the REPL type the following:
3232

33-
>>> revenue = 1000
34-
>>> costs = 200
35-
>>> profit = revenue - costs
33+
>>> revenue = 1000
34+
>>> costs = 200
35+
>>> profit = revenue - costs
3636

3737
Now type `profit` to see the results of this calculation.
3838

@@ -49,22 +49,23 @@ strings.
4949

5050
Now in the REPL type:
5151

52-
>>> name = 'codebar'
53-
>>> url = "codebar.io"
52+
>>> name = 'codebar'
53+
>>> url = "codebar.io"
5454

55-
Now type `name` and `url` to see these strings shown back to you. As you can
56-
see Python allows both single and double quotes to denote a string variable.
57-
Double quotes are required if there is going to be an apostrophe in the string.
55+
Now type `print(name)` and `print(url)` to see these strings shown back to you.
56+
As you can see Python allows both single and double quotes to denote a string
57+
variable. Double quotes are required if there is going to be an apostrophe in
58+
the string.
5859

5960
For example:
6061

61-
message = "I'm a string"
62+
message = "I'm a string"
6263

6364
Sometimes you will need to use an apostrophe within a single quote, on
6465
occasions like this it is recommended to use "string escaping". This would look
6566
like:
6667

67-
message ='I\'m a string'
68+
message ='I\'m a string'
6869

6970
Try storing a string within a variable without quotes, see what happens?
7071
Numbers do not require quotation marks, whereas they are mandatory for storing
@@ -122,20 +123,20 @@ command. Let's create a variable in which to store the user input.
122123

123124
Now type this into your REPL:
124125

125-
>>> lucky_number = input("What is your lucky number? ")
126+
>>> lucky_number = input("What is your lucky number? ")
126127

127128
Type back your answer after it asks you.
128129

129130
Now in the REPL type:
130131

131-
>>> food = input("What is your favourite food? ")
132+
>>> food = input("What is your favourite food? ")
132133

133134
Now we are going to put your response into another variable.
134135

135136
Now try:
136137

137-
>>> my_name = input("What is your name? ")
138-
>>> greeting = "Hello " + my_name
138+
>>> my_name = input("What is your name? ")
139+
>>> greeting = "Hello " + my_name
139140

140141
Then type `greeting` into your REPL to receive your message.
141142

@@ -146,10 +147,10 @@ around with decision making and changing prints based on your answer. In Python
146147
(and many other languages), one of the most common ways in which this is done
147148
is using an `if` statement. For example:
148149

149-
if number > 3:
150-
print("Bigger than three")
151-
elif number < 3:
152-
print("Smaller than three")
150+
>>> if number > 3:
151+
... print("Bigger than three")
152+
... elif number < 3:
153+
... print("Smaller than three")
153154

154155
Here we can see that if a number we have passed into this decision making code
155156
is bigger than three, we will receive a message telling us so. There is a

python/lesson3/tutorial.md

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ which is where most people would expect things to start. This means that the
3939
*first* element of a list is referred to with a `0`, the second element is
4040
referred to with a `1`, and so on. Perhaps an example would be helpful:
4141

42-
>>> places_to_visit[0]
42+
>>> print(places_to_visit[0])
4343
'Mexico'
44-
>>> places_to_visit[2]
44+
>>> print(places_to_visit[2])
4545
'Kenya'
4646

4747
Positive numbers aren't the only thing you can use for indexes though. Negative
4848
numbers invert the index, so you can get the *last* element of a list by using
4949
`-1`, or the third-to-last by using `-3`:
5050

51-
>>> places_to_visit[-1]
51+
>>> print(places_to_visit[-1])
5252
'New Zealand'
53-
>>> places_to_visit[-3]
53+
>>> print(places_to_visit[-3])
5454
'Kenya'
5555

5656
Try it yourself now: Try to get `Nepal` out of `places_to_visit` using both a
@@ -71,23 +71,23 @@ There's a lot that you can do with lists, so much that we simply can't cover it
7171
all here, so instead here are some examples, and a [link to the documentation](https://docs.python.org/3.5/tutorial/datastructures.html#more-on-lists "Methods of lists")
7272
for a more complete reference:
7373

74-
>>> places_to_visit
74+
>>> print(places_to_visit)
7575
['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand']
7676

7777
>>> places_to_visit = ['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand']
7878
>>> places_to_visit[1] = "Peru"
79-
>>> places_to_visit
79+
>>> print(places_to_visit)
8080
['Mexico', 'Peru', 'Kenya', 'Nepal', 'New Zealand']
8181

8282
>>> places_to_visit = ['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand']
83-
>>> places_to_visit.pop()
83+
>>> print(places_to_visit.pop())
8484
'New Zealand'
85-
>>> places_to_visit
85+
>>> print(places_to_visit)
8686
['Mexico', 'Portugal', 'Kenya', 'Nepal']
8787

8888
>>> places_to_visit = ['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand']
8989
>>> places_to_visit.append("Colombia")
90-
>>> places_to_visit
90+
>>> print(places_to_visit)
9191
['Mexico', 'Portugal', 'Kenya', 'Nepal', 'New Zealand', 'Colombia']
9292

9393
Try some of these out yourself:
@@ -103,22 +103,24 @@ But we're not finished! You can do even more interesting things with `[` and
103103
to get the first four elements of a list *as another list*. That's easy with
104104
the subset syntax:
105105

106-
>>> places_to_visit[0:4]
106+
>>> print(places_to_visit[0:4])
107107
["Mexico", "Portugal", "Kenya", "Nepal"]
108-
>>> places_to_visit[:4]
108+
109+
>>> print(places_to_visit[:4])
109110
["Mexico", "Portugal", "Kenya", "Nepal"]
110111

111112
You can do some interesting things with negative numbers too:
112113

113-
>>> places_to_visit[-3:4]
114+
>>> print(places_to_visit[-3:4])
114115
['Kenya', 'Nepal']
115-
>>> places_to_visit[-3:]
116+
117+
>>> print(places_to_visit[-3:])
116118
['Kenya', 'Nepal', 'New Zealand']
117119

118120
...and as getting a subset returns a list itself, you can get a subset of a
119121
subset:
120122

121-
>>> places_to_visit[0:4][-1]
123+
>>> print(places_to_visit[0:4][-1])
122124
'Nepal'
123125

124126
### Nesting
@@ -128,15 +130,15 @@ you from putting a list inside a list. In fact, you can put a list inside a
128130
list, inside a list, inside a... you get the idea. You're only limited by how
129131
many levels deep you can go before your code is too confusing:
130132

131-
cake_flavours = ["chocolate", ["chocolate", "vanilla"], "red velvet"]
133+
>>> cake_flavours = ["chocolate", ["chocolate", "vanilla"], "red velvet"]
132134

133-
cake_flavours[0]
135+
>>> print(cake_flavours[0])
134136
'chocolate'
135137

136-
cake_flavours[1]
138+
>>> print(cake_flavours[1])
137139
['chocolate', 'vanilla']
138140

139-
cake_flavours[1][1]
141+
>>> print(cake_flavours[1][1])
140142
'vanilla'
141143

142144
There's a lot more things you can do with lists including concatenating,
@@ -152,31 +154,31 @@ will result in a `TypeError`.
152154

153155
Tuples are represented using `(` and `)`:
154156

155-
places_to_visit = ("Mexico", "Portugal", "Kenya", "Nepal", "New Zealand")
157+
>>> places_to_visit = ("Mexico", "Portugal", "Kenya", "Nepal", "New Zealand")
156158

157-
places_to_visit[1]
159+
>>> print(places_to_visit[1])
158160
'Portugal'
159161

160-
places_to_visit[0:3]
162+
>>> print(places_to_visit[0:3])
161163
('Mexico', 'Portugal', 'Kenya')
162164

163165
This however will fail with a `TypeError`:
164166

165-
places_to_visit[1] = "Canada"
167+
>>> places_to_visit[1] = "Canada"
166168

167169
Tuples are commonly used in cases where you're defining something that
168170
shouldn't ever change, but should you ever need to modify something that's in a
169171
tuple, you can always *cast* it as a list:
170172

171-
my_tuple = (1, 2, 3)
173+
>>> my_tuple = (1, 2, 3)
172174

173-
my_list = list(my_tuple)
174-
my_list[2] = 99
175+
>>> my_list = list(my_tuple)
176+
>>> my_list[2] = 99
175177

176-
my_list
178+
>>> print(my_list)
177179
[1, 2, 99]
178180

179-
my_tuple
181+
>>> print(my_tuple)
180182
(1, 2, 3)
181183

182184
This will make a **copy** of the tuple that's a list, so you can edit it, but
@@ -194,7 +196,7 @@ corresponding *values*. You'll often seen them as a simple way to have a
194196
lookup table or crude database in an application. For this tutorial we'll use
195197
a dictionary for a phone book:
196198

197-
my_phone_book = {
199+
>>> my_phone_book = {
198200
"Arya": "+4407485376242",
199201
"Brienne": "+3206785246863",
200202
"Cersei": "+14357535455",
@@ -210,7 +212,7 @@ though. This will give you Cersei's phone number:
210212
There's nothing restricting you to strings as values in your dictionary though.
211213
You can have *anything* in there:
212214

213-
my_crazy_dictionary = {
215+
>>> my_crazy_dictionary = {
214216
"a number": 7,
215217
"a float": 1.23456789,
216218
"a string": "hello, world!",
@@ -228,7 +230,7 @@ can be used for keys, though this can sometimes lead to reduced readability,
228230
so use this with caution. For example, while this is valid Python, it's not
229231
exactly easy to understand:
230232

231-
my_unreadable_dictionary = {
233+
>>> my_unreadable_dictionary = {
232234
("this", "is", "a", "tuple!"): {"a string": "hello, world!"}
233235
}
234236

@@ -247,15 +249,15 @@ doing this should hopefully feel intuitive by now:
247249

248250
Change a value for a key:
249251

250-
my_phone_book["Brienne"] = "+830685432195"
252+
>>> my_phone_book["Brienne"] = "+830685432195"
251253

252254
Add a new key/value pair:
253255

254-
my_phone_book["Ellaria"] = "+560538942621"
256+
>>> my_phone_book["Ellaria"] = "+560538942621"
255257

256258
This one is new: delete a key/value pair:
257259

258-
del(my_phone_book["Ellaria"])
260+
>>> del(my_phone_book["Ellaria"])
259261

260262
Now that you've got the tools to change your dictionaries, give a shot
261263
yourself. Try changing Cersei's phone number. In fact, change it to a list of
@@ -274,13 +276,13 @@ manageable parts. Thankfully, Python has you covered with `.keys()`,
274276
"Cersei": "+14357535455",
275277
"Davos": "+244562726258"
276278
}
277-
>>> my_phone_book.keys()
279+
>>> print(my_phone_book.keys())
278280
dict_keys(['Davos', 'Cersei', 'Brienne', 'Arya'])
279281

280-
>>> my_phone_book.values()
282+
>>> print(my_phone_book.values())
281283
dict_values(['+3206785246863', '+14357535455', '+244562726258', '+4407485376242'])
282284

283-
>>> my_phone_book.items()
285+
>>> print(my_phone_book.items())
284286
dict_items([('Brienne', '+3206785246863'), ('Cersei', '+14357535455'), ('Davos', '+244562726258'), ('Arya', '+4407485376242')])
285287

286288
As you can see, `.keys()` and `.values()` do what you'd expect: they return the
@@ -290,20 +292,20 @@ These are sort of like tuples: you can't edit them, and if you want to do
290292
anything with them other than read them as a complete entity, you'll have to
291293
cast them as a list:
292294

293-
>>> list(my_phone_book.keys())
295+
>>> print(list(my_phone_book.keys()))
294296
['+3206785246863', '+14357535455', '+244562726258', '+4407485376242']
295297

296-
>>> list(my_phone_book.keys())[2]
298+
>>> print(list(my_phone_book.keys())[2])
297299
'+244562726258'
298300

299301
The last one there, `.items()` is interesting. It returns all of the data in
300302
your dictionary, but dumps it out as `dict_items` which is a sort of *tuple of
301303
tuples*. This allows you to reference your dictionary with list syntax:
302304

303-
>>> tuple(my_phone_book.items())[0]
305+
>>> print(tuple(my_phone_book.items())[0])
304306
('Brienne', '+3206785246863')
305307

306-
>>> tuple(my_phone_book.items())[0][1]
308+
>>> print(tuple(my_phone_book.items())[0][1])
307309
'+3206785246863'
308310

309311
Truth be told though, you probably won't be accessing these values directly

python/lesson4/tutorial.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ later to run these statements. Python comes with plenty of functions already
1313
built in that you can use to perform common tasks. One example should be
1414
something you're already familiar with:
1515

16-
>>> print("This is a function")
17-
This is a function
18-
16+
>>> print("This is a function")
17+
This is a function
18+
1919
It is also possible to store the output of a function in a variable for future
2020
use. Let's try this with the built in function `len()`, which will provide us
2121
with the length of a string:
2222

23-
>>> length_of_my_string = len("This is my string")
24-
>>> print(length_of_my_string)
25-
17
23+
>>> length_of_my_string = len("This is my string")
24+
>>> print(length_of_my_string)
25+
17
2626

2727
The function `len()` takes one *argument*: the string it's measuring, and it
2828
returns an *integer*: the length of the string. Here, we're storing the output
@@ -133,7 +133,7 @@ effort of defining and calling a function, rather than just writing the code
133133
independent of such things. Defining tasks as functions reduces the need to
134134
copy and paste the same code multiple times to achieve the same effect. Simply
135135
calling the function multiple times makes it much easier to not only write
136-
code, but to read it also.
136+
code, but to read it as well.
137137

138138
Using functions also makes it a lot easier to fix and change code. If you are
139139
performing the same tasks in multiple places and discover a bug, without

0 commit comments

Comments
 (0)