Skip to content

Commit 1298e9a

Browse files
committed
you->we, more examples, other fixes
1 parent 4a77f1b commit 1298e9a

13 files changed

+201
-142
lines changed

classes.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ tkinter GUI's before I understood how they work. Everything I did with
55
classes worked, but I didn't understand how. Hopefully you'll first
66
learn to understand classes, and then learn to use them.
77

8-
This tutorial assumes that you know how functions work and how to
9-
create your own functions with the `def` keyword. If you don't, I
10-
highly recommend learning that first, and then moving to classes.
8+
This tutorial assumes that you know [how functions work](using-functions.md)
9+
and [how to create your own functions](defining-functions.md). If you
10+
don't I highly recommend learning that first, and then moving to classes.
1111

1212
## Why should I use custom classes in my projects?
1313

@@ -81,13 +81,6 @@ Let's use it to define an empty class.
8181
>>>
8282
```
8383

84-
_**Note:** If you are using Python 2 I highly recommend using
85-
`class Website(object):` instead of `class Website:`. This creates a
86-
new-style class instead of an old-style class. Old-style classes are
87-
different than new-style classes in some ways and not supported in this
88-
tutorial. In Python 3, there are no old-style classes and
89-
`class Website(object):` does the same thing as `class Website:`._
90-
9184
Note that I named the class `Website`, not `website`. This way we know
9285
that it's a class. Built-in classes use lowercase names (like `str`
9386
instead of `Str`) because they are faster to type, but use CapsWord
@@ -126,11 +119,17 @@ True
126119
>>>
127120
```
128121

122+
As you can see, our Website is mutable, like lists are, not immutable
123+
like strings are. We can change the website in-place without creating a
124+
new Website.
125+
129126
`url`, `founding_year` and `free_to_use` are not variables, they are
130127
**attributes**. More specifically, they are **instance attributes**.
131-
The biggest difference is that variables are accessed by their name,
132-
and attributes are accessed by typing a name of an object (like
133-
stackoverflow), then a dot and then the name of the attribute.
128+
The biggest difference is that we need to use a dot for setting and
129+
getting values of attributes, but we don't need that with variables.
130+
Modules also use instance attributes for accessing their content. For
131+
example, when we do `random.randint`, `random` is a module instance and
132+
`randint` is one of its attributes.
134133

135134
If we make another Website, does it have the same `url`, `founding_year`
136135
and `free_to_use`?
@@ -336,7 +335,9 @@ available in the info method. We could also access them directly from
336335

337336
But we still need to call `stackoverflow.initialize`. In Python, there's
338337
a "magic" method that runs when we create a new Website by calling the
339-
Website class. It's called `__init__` and it does nothing by default.
338+
Website class. It's called `__init__` and it does nothing by default. If
339+
our `__init__` method takes other arguments than self we can call the
340+
class with arguments and they will be given to `__init__`. Like this:
340341

341342
```py
342343
>>> class Website:
@@ -378,6 +379,10 @@ class MyProgram:
378379
program = MyProgram()
379380
```
380381

382+
You should avoid using things like `print` and `input` in the `__init__`
383+
method. The `__init__` method should be simple and it should just set
384+
things up.
385+
381386
Usually you shouldn't use a class if you're only going to make one
382387
instance of it, and you don't need a class either if you're only going
383388
to have one method. In this example `MyProgram` has only one method and
@@ -393,10 +398,7 @@ word = input("Enter something: ")
393398
print("You entered " + word + ".")
394399
```
395400

396-
## Important things
397-
398-
Here are some of the most important things covered in this tutorial.
399-
Make sure you understand them.
401+
## Summary
400402

401403
- Object-orientated programming is programming with custom data types.
402404
In Python that means using classes and instances.

defining-functions.md

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Defining your own functions
1+
# Defining custom functions
22

3-
It's probably been a while since you read about using functions. [Read
4-
about it again](using-functions.md) if you need to.
3+
It's probably been a while since you read about using functions.
4+
[Read about it again](using-functions.md) if you need to.
55

66
## Why should I use custom functions?
77

@@ -40,7 +40,13 @@ else:
4040
print_box("You didn't enter Python :(")
4141
```
4242

43-
That's nice, but where do we get a `print_box` function like that?
43+
In this tutorial we'll learn to define a `print_box` function
44+
that prints text in a box. We can write the code for printing the
45+
box once, and then use it multiple times anywhere in the program.
46+
47+
Dividing a long program into simple functions also makes the code
48+
easier to work with. If there's a problem with the code we can
49+
test the functions one by one and find the problem easily.
4450

4551
## First functions
4652

@@ -167,7 +173,7 @@ However, modifying a global variable in-place from a function is easy.
167173

168174
This doesn't work if the value is of an immutable type, like string or
169175
integer because immutable values cannot be modified in-place.
170-
Fortunately, Python will tell you if something's wrong.
176+
Fortunately, Python will tell us if something's wrong.
171177

172178
```py
173179
>>> foo = 1
@@ -220,7 +226,7 @@ This function can be called in two ways:
220226
When the function was running it had a local `message` variable
221227
that pointed to `"hi"`. The function printed it twice.
222228

223-
Positional arguments are great for simple things, but if your
229+
Positional arguments are great for simple things, but if our
224230
function takes many positional arguments it may be hard to tell
225231
which argument is which.
226232

@@ -233,7 +239,12 @@ This function can be called in two ways:
233239
>>>
234240
```
235241

236-
Keyword arguments are great when your function needs to take many
242+
The name "keyword argument" is a little bit confusing because
243+
keyword arguments don't actually have anything to do with keywords
244+
(`if`, `else` etc). Keyword arguments are just a way to give names
245+
for our arguments.
246+
247+
Keyword arguments are great when our function needs to take many
237248
arguments, because each argument has a name and it's easy to see
238249
which argument is which.
239250

@@ -334,8 +345,8 @@ need to:
334345
```
335346

336347
The problem is that we have a keyword argument before a positional
337-
argument. Python doesn't allow this. You don't need to worry about
338-
this, because if you accidentally call a function like this you
348+
argument. Python doesn't allow this. We don't need to worry about
349+
this, because if we accidentally call a function like this we
339350
will get an error message.
340351

341352
## Output
@@ -394,11 +405,11 @@ about the `input()` function. It asks the user to enter something, and
394405
then the user enters something and that value is returned. If the input
395406
function would print the value instead of returning it, things like
396407
`name = input("Name: ")` wouldn't work and assigning the result to a
397-
variable would be much more difficult. Printing things is fine when you
398-
know that you'll only need to print the result and you'll never need to
408+
variable would be much more difficult. Printing things is fine when we
409+
know that we'll only need to print the result and we'll never need to
399410
assign it to a variable.
400411

401-
So if our function returns a value we can use it like this:
412+
If our function returns a value we can always print it, like this:
402413

403414
```py
404415
>>> def return_hi():
@@ -409,18 +420,6 @@ hi
409420
>>>
410421
```
411422

412-
If the function prints instead we can call it and just throw away
413-
the None it returns:
414-
415-
```py
416-
>>> def print_hi():
417-
... print("hi")
418-
...
419-
>>> print_hi()
420-
hi
421-
>>>
422-
```
423-
424423
## Examples
425424

426425
Ask yes/no questions.

editor-setup.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Setting up an editor for programming
22

33
Python comes with its IDLE, and you can use it in this tutorial. If you
4-
don't like using it for some reason, you need
5-
[PowerShell, command prompt or terminal](installing-python.md) for
6-
trying out things. You also need an editor for writing code that will
4+
don't like using it for some reason, you need [PowerShell, command prompt or
5+
terminal](installing-python.md#if-you-like-working-with-powershell-command-prompt-or-terminal)
6+
for trying out things. You also need an editor for writing code that will
77
be stored in files.
88

99
If you use IDLE as your editor, **it comes with everything set up for
@@ -12,7 +12,7 @@ don't, you probably need to change some settings to make your editor
1212
suitable for Python use.
1313

1414
Do **not** use word processors like Microsoft Word and LibreOffice
15-
Write for programming. They create their own files, but you need plain
15+
Writer for programming. They create their own files, but you need plain
1616
text files for programming.
1717

1818
Start by creating an empty file called `hello.py` and opening it with

exceptions.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ get when [processing files](files.md), and catching Exception catches
400400
all of these errors. You don't need to remember this tree, running
401401
`help('builtins')` should display a larger tree that this is a part of.
402402

403+
There's also a few exceptions that are not in this tree like SystemExit
404+
and KeyboardInterrupt, but most of the time you shouldn't catch them.
405+
Catching Exception doesn't catch them either.
406+
403407
## Summary
404408

405409
- Exceptions are classes and they can be used just like all other classes.

files.md

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ C:
3838
```
3939

4040
`C:\Users\me\hello.py` is an **absolute path**. But there are also
41-
**relative paths**. For example, if you're in `C:\Users`, `me\hello.py`
41+
**relative paths**. For example, if we are in `C:\Users`, `me\hello.py`
4242
is same as `C:\Users\me\hello.py`. The place we are in is sometimes
4343
called **current directory**, **working directory** or
4444
**current working directory**.
@@ -67,7 +67,7 @@ Let's create a file and write a hello world to it.
6767
```
6868

6969
Doesn't seem like it did anything. But actually it created a `hello.txt`
70-
somewhere on your system. On Windows it's probably in `C:\Users\YourName`,
70+
somewhere on our system. On Windows it's probably in `C:\Users\YourName`,
7171
and on most other systems it should be in `/home/yourname`. You can open
7272
it with notepad or any other plain text editor your system comes with.
7373

@@ -88,7 +88,7 @@ the current working directory.
8888

8989
The second argument was `w`... but where the heck does that come from?
9090
`w` is short for write, and that just means that we'll create a new file.
91-
There's some other modes you can use also:
91+
There's some other modes we can use also:
9292

9393
| Mode | Short for | Meaning |
9494
|-------|-----------|-----------------------------------------------------------------------|
@@ -100,7 +100,7 @@ The `w` and `a` modes create a new file if it exists already, but trying
100100
to read from a non-existent file is an error.
101101

102102
But what is that `with ourfile as f` crap? That's just a fancy way to make
103-
sure that the file gets closed, no matter what happens. As you can see,
103+
sure that the file gets closed, no matter what happens. As we can see,
104104
the file was indeed closed.
105105

106106
```py
@@ -115,7 +115,7 @@ to the file we opened using `file=f`.
115115

116116
## Reading from files
117117

118-
After opening a file with the `r` mode you can for loop over it, just
118+
After opening a file with the `r` mode we can for loop over it, just
119119
like it was a list. So let's go ahead and read everything in the file
120120
we created to a list of lines.
121121

@@ -130,7 +130,23 @@ we created to a list of lines.
130130
>>>
131131
```
132132

133-
But why is there a `\n` at the end of our hello world?
133+
Trying to open a non-existent file with `w` created the file for us, but
134+
doing that with `r` gives us an error instead. We'll learn more about
135+
errors later.
136+
137+
```py
138+
>>> with open('this-doesnt-exist.txt', 'r') as f:
139+
... print("It's working!")
140+
...
141+
Traceback (most recent call last):
142+
File "<stdin>", line 1, in <module>
143+
FileNotFoundError: [Errno 2] No such file or directory: 'this-doesnt-exist.txt'
144+
>>>
145+
```
146+
147+
So now we have the hello world in the `lines` variable, but it's
148+
`['Hello World!\n']` instead of `['Hello World!']`. So what the heck did
149+
that `\n` doing there?
134150

135151
`\n` means newline. Note that it needs to be a backslash, so `/n`
136152
doesn't have any special meaning like `\n` has. When we wrote the file
@@ -160,9 +176,9 @@ There we go, each of our lines now ends with a `\n`. When we for
160176
loop over the file it's divided into lines based on where the `\n`
161177
characters are, not based on how we printed to it.
162178

163-
But how to get rid of that `\n`? The `rstrip`
164-
[string method](handy-stuff-strings.md#string-methods) is great
165-
for this:
179+
But how to get rid of that `\n`? [The rstrip string
180+
method](https://docs.python.org/3/library/stdtypes.html#str.rstrip) is
181+
great for this:
166182

167183
```py
168184
>>> stripped = []
@@ -174,9 +190,9 @@ for this:
174190
>>>
175191
```
176192

177-
It's also possible to read lines one by one. Files have a
178-
`readline()` method that reads the next line, and returns `''`
179-
if we're at the end of the file.
193+
It's also possible to read lines one by one. Files have [a readline
194+
method](https://docs.python.org/3/library/io.html#io.TextIOBase.readline)
195+
that reads the next line, and returns `''` if we're at the end of the file.
180196

181197
```py
182198
>>> with open('hello.txt', 'r') as f:
@@ -189,8 +205,8 @@ if we're at the end of the file.
189205
'Hello two!\n'
190206
```
191207

192-
There's only one confusing thing about reading files. If you try
193-
to read it twice you'll find out that it only gets read once:
208+
There's only one confusing thing about reading files. If we try
209+
to read it twice we'll find out that it only gets read once:
194210

195211
```py
196212
>>> first = []
@@ -232,11 +248,12 @@ again and everything works.
232248
```
233249

234250
Usually it's best to just read the file once, and use the
235-
content you have read from it multiple times.
251+
content we have read from it multiple times.
236252

237-
As you can see, files behave a lot like lists. The `join()`
238-
string method joins together strings from a list, but we can
239-
also use it to join together lines of a file:
253+
As we can see, files behave a lot like lists. [The join string
254+
method](https://docs.python.org/3/library/stdtypes.html#str.join) joins
255+
together strings from a list, but we can also use it to join together
256+
lines of a file:
240257

241258
```py
242259
>>> with open('hello.txt', 'r') as f:
@@ -247,8 +264,8 @@ also use it to join together lines of a file:
247264
>>>
248265
```
249266

250-
But if you need all of the content as a string, you can just
251-
use the `read()` method.
267+
But if we need all of the content as a string, we can just use [the read
268+
method](https://docs.python.org/3/library/io.html#io.TextIOBase.read).
252269

253270
```py
254271
>>> with open('hello.txt', 'r') as f:
@@ -259,7 +276,7 @@ use the `read()` method.
259276
>>>
260277
```
261278

262-
You can also open full paths, like `open('C:\\Users\\me\\myfile.txt', 'r')`.
279+
We can also open full paths, like `open('C:\\Users\\me\\myfile.txt', 'r')`.
263280
The reason why we need to use `\\` when we really mean `\` is that
264281
backslash has a special meaning. There are special characters like
265282
`\n`, and `\\` means an actual backslash.
@@ -285,8 +302,9 @@ True
285302
>>>
286303
```
287304

288-
If you don't use Windows and your paths don't contain backslashes
289-
you don't need to double anything or use `r` in front of paths.
305+
If our program is not meant to be ran on Windows and the paths
306+
don't contain backslashes we don't need to double anything or use
307+
`r` in front of paths.
290308

291309
```py
292310
>>> print('/some/name')

0 commit comments

Comments
 (0)