Skip to content

Commit d47480d

Browse files
committed
2 parents aa7a962 + 4a98105 commit d47480d

12 files changed

+262
-84
lines changed

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
# Python programming tutorial
22

3-
This is a Python 3 programming tutorial for beginners. If you have never
4-
programmed before click [here](basics/what-is-programming.md) to find
5-
out what programming is like and get started.
3+
This is a concise Python 3 programming tutorial for people who think
4+
that reading is boring. I try to show everything with simple code
5+
examples; there are no long and complicated explanations with fancy
6+
words. If you have never programmed before click
7+
[here](basics/what-is-programming.md) to find out what programming is
8+
like and get started.
69

710
This tutorial is aimed at people with no programming experience at all
811
or very little programming experience. If you have programmed a lot in
912
the past using some other language you may want to read [the official
1013
tutorial](https://docs.python.org/3/tutorial/) instead.
1114

12-
You can use Python 3.2 or any newer Python with this tutorial. Don't use
13-
Python 2. If you write a Python 2 program now someone will need to port
14-
it to Python 3 later, so it's best to just write Python 3 to begin with.
15-
Python 3 code will work just fine in Python 4, so you don't need to
16-
worry about that.
15+
You can use Python 3.3 or any newer Python with this tutorial. **Don't
16+
use Python 2.** If you write a Python 2 program now someone will need to
17+
convert it to Python 3 later, so it's best to just write Python 3 to
18+
begin with. Python 3 code will work just fine in Python 4, so you don't
19+
need to worry about that. Python 2 also has horrible
20+
[Unicode](http://www.unicode.org/standard/WhatIsUnicode.html) problems,
21+
so it's difficult to write Python 2 code that works correctly with
22+
non-English characters (like π and ♫).
1723

1824
## List of contents
1925

TODO.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ This tutorial is not complete. It still needs:
3535
- "What the heck is this?" section for stuff i haven't talked about
3636
- regexes
3737

38+
- add a screenshot about geany's running settings to
39+
basics/editor-setup.md
40+
3841
***
3942

4043
If you have trouble with this tutorial please [tell me about

advanced/datatypes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ True
5555

5656
We can also convert anything [iterable](../basics/loops.md#summary) to a
5757
set [by calling the
58-
class](../basics/classes.md#why-should-I-use-custom-classes-in-my-projects).
58+
class](../basics/classes.md#what-are-classes).
5959

6060
```python
6161
>>> set('hello')

basics/classes.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# Defining and using custom classes in Python
1+
# Defining and using custom classes
22

33
When I was getting started in Python I learned to make classes for
44
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-
## Why should I use custom classes in my projects?
8+
## What are classes?
99

10-
Python comes with a lot of classes that you are already familiar with.
10+
Python comes with many classes that we know already.
1111

1212
```python
1313
>>> str
@@ -51,11 +51,11 @@ We can also get an instance's class with `type()`:
5151
>>>
5252
```
5353

54-
Let's say you make a program that processes data about websites. With a
55-
custom class, you're not limited to `str`, `int` and other classes
56-
Python comes with. Instead you can define a Website class, and make
57-
Websites and process information about websites directly. Defining your
58-
own object types like this is called **object-orientated programming**.
54+
Let's say that we make a program that processes data about websites.
55+
With a custom class, we're not limited to `str`, `int` and other classes
56+
Python comes with. Instead we can define a Website class, and make
57+
Websites and process information about websites directly. Defining our
58+
own types like this is called **object-orientated programming**.
5959

6060
## First class
6161

@@ -77,6 +77,9 @@ Let's use it to define an empty class.
7777
>>>
7878
```
7979

80+
The `pass` is needed here, just like [when defining functions that do
81+
nothing](defining-functions.md#first-functions).
82+
8083
Note that I named the class `Website`, not `website`. This way we know
8184
that it's a class. Built-in classes use lowercase names (like `str`
8285
instead of `Str`) because they are faster to type, but use CapsWord

basics/defining-functions.md

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ In this tutorial we'll learn to define a `print_box` function
4444
that prints text in a box. We can write the code for printing the
4545
box once, and then use it multiple times anywhere in the program.
4646

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.
47+
[Dividing a long program into simple functions](larger-program.md) also
48+
makes the code easier to work with. If there's a problem with the code
49+
we can test the functions one by one and find the problem easily.
5050

5151
## First functions
5252

@@ -68,9 +68,15 @@ Let's use it to define a function that does nothing.
6868
>>>
6969
```
7070

71-
Seems to be working so far, we have a function. Actually it's just
72-
a value that is assigned to a variable called `do_nothing`. Let's see
73-
what happens if we call it.
71+
Seems to be working so far, we have a function. It's just a value that
72+
is assigned to a variable called `do_nothing`. You can ignore the
73+
`0xblablabla` stuff for now.
74+
75+
The `pass` is needed here because without it, Python doesn't know when
76+
the function ends and it gives us a syntax error. We don't need the
77+
`pass` when our functions contain something else.
78+
79+
Let's see what happens if we call our function.
7480

7581
```python
7682
>>> do_nothing()
@@ -176,15 +182,15 @@ integer because immutable values cannot be modified in-place.
176182
Fortunately, Python will tell us if something's wrong.
177183

178184
```python
179-
>>> foo = 1
180-
>>> def bar():
181-
... foo += 1
185+
>>> thing = 1
186+
>>> def stuff():
187+
... thing += 1
182188
...
183-
>>> bar()
189+
>>> stuff()
184190
Traceback (most recent call last):
185191
File "<stdin>", line 1, in <module>
186-
File "<stdin>", line 2, in bar
187-
UnboundLocalError: local variable 'foo' referenced before assignment
192+
File "<stdin>", line 2, in stuff
193+
UnboundLocalError: local variable 'thing' referenced before assignment
188194
>>>
189195
```
190196

@@ -283,8 +289,8 @@ def print_box(message, character):
283289
print(character * len(message))
284290
```
285291

286-
Then we could change our existing code to always call `print_box` with
287-
a star as the second argument:
292+
Then we could change our code to always call `print_box` with a star as
293+
the second argument:
288294

289295
```python
290296
print_box("Hello World", "*")
@@ -355,11 +361,11 @@ need to:
355361
## Output
356362

357363
The built-in input function [returns a value](using-functions.md#return-values).
358-
Can our function return a value also?
364+
Can our function return a value too?
359365

360366
```python
361-
>>> def times_two(x):
362-
... return x * 2
367+
>>> def times_two(thing):
368+
... return thing * 2
363369
...
364370
>>> times_two(3)
365371
6
@@ -412,7 +418,7 @@ None
412418

413419
## Return or print?
414420

415-
There's two ways to output information from functions. They can print
421+
There are two ways to output information from functions. They can print
416422
something or they can return something. So, should we print or return?
417423

418424
Most of the time **returning makes functions much easier to use**. Think
@@ -435,6 +441,47 @@ hi
435441
>>>
436442
```
437443

444+
## Common problems
445+
446+
Functions are easy to understand, but you need to pay attention to how
447+
you're calling them. Note that `some_function` and `some_function()` do
448+
two completely different things.
449+
450+
```python
451+
>>> def say_hi():
452+
... print("howdy hi")
453+
...
454+
>>> say_hi # just checking what it is, doesn't run anything
455+
<function say_hi at 0x7f997d2a8510>
456+
>>> say_hi() # this runs it
457+
howdy hi
458+
>>>
459+
```
460+
461+
Typing `say_hi` just gives us the value of the `say_hi` variable, which
462+
is the function we defined. But `say_hi()` **calls** that function, so
463+
it runs and gives us a return value. The return value is None so the
464+
`>>>` prompt [doesn't show it](#variables.md#none).
465+
466+
But we know that the print function shows None, so what happens if we
467+
wrap the whole thing in `print()`?
468+
469+
```python
470+
>>> print(say_hi) # prints the function, just like plain say_hi
471+
<function say_hi at 0x7fd913f58488>
472+
>>> print(say_hi()) # runs the function and then prints the return value
473+
howdy hi
474+
None
475+
>>>
476+
```
477+
478+
The `print(say_hi())` thing looks a bit weird at first, but it's easy to
479+
understand. There's a print insnde `say_hi` and there's also the print
480+
we just wrote, so two things are printed. Python first ran `say_hi()`,
481+
and it returned None so Python did `print(None)`. Adding an extra
482+
`print()` around a function call is actually a common mistake, and I
483+
have helped many people with this problem.
484+
438485
## Examples
439486

440487
Ask yes/no questions.
@@ -463,7 +510,7 @@ def ask_until_correct(prompt, correct_options,
463510
while True:
464511
answer = input(prompt + ' ')
465512
if answer in correct_options:
466-
return answer # returning ends the function
513+
return answer
467514
print(error_message)
468515

469516

@@ -489,6 +536,8 @@ print("Your favorite color is %s!" % choice)
489536
function does. Returning also ends the function immediately.
490537
- Return a value instead of printing it if you need to do something with
491538
it after calling the function.
539+
- Remember that `thing`, `thing()`, `print(thing)` and `print(thing())`
540+
do different things.
492541

493542
## Exercises
494543

basics/getting-started.md

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ worked just fine. Later we'll learn what `(3, 14)` is.
6262

6363
## Comments
6464

65-
We can also type a `#` and then whatever we want after that. These bits
66-
of text are known as **comments**, and we'll find uses for them later.
65+
**Comments are text that does nothing.** They can be created by typing a
66+
`#` and then some text after it, and they are useful when our code would
67+
be hard to understand without them.
6768

6869
```python
6970
>>> 1 + 2 # can you guess what the result is?
@@ -74,7 +75,7 @@ of text are known as **comments**, and we'll find uses for them later.
7475
Again, I put a space after the `#` and multiple spaces before it just to
7576
make things easier to read.
7677

77-
If we write comment on a line with no code on it, the prompt changes
78+
If we write a comment on a line with no code on it, the prompt changes
7879
from `>>>` to `...`. To be honest, I have no idea why it does that and I
7980
think it would be better if it would just stay as `>>>`. The prompt goes
8081
back to `>>>` when we press Enter again.
@@ -85,6 +86,56 @@ back to `>>>` when we press Enter again.
8586
>>>
8687
```
8788

89+
## Strings
90+
91+
Strings are small pieces of text that we can use in our programs. We can
92+
create strings by simply writing some text in quotes.
93+
94+
```python
95+
>>> 'hello'
96+
'hello'
97+
>>> 'this is a test'
98+
'this is a test'
99+
>>>
100+
```
101+
102+
Strings can also be written with "double quotes" instead of 'single
103+
quotes'. This is useful when we need to put quotes inside the string.
104+
105+
```python
106+
>>> "hello there"
107+
'hello there'
108+
>>> "it's sunny"
109+
"it's sunny"
110+
>>>
111+
```
112+
113+
It's also possible to add single quotes and double quotes into the same
114+
string, but most of the time we don't need to do that so I'm not going
115+
to talk about it now.
116+
117+
It doesn't matter which quotes you use when the string doesn't need to
118+
contain any quotes. If you think that one of the quote types looks nicer
119+
than the other or you find it faster to type, go ahead and use that.
120+
121+
Strings can be joined together easily with `+` or repeated with `*`:
122+
123+
```python
124+
>>> "hello" + "world"
125+
'helloworld'
126+
>>> "hello" * 3
127+
'hellohellohello'
128+
>>>
129+
```
130+
131+
Note that a `#` inside a string doesn't create a comment.
132+
133+
```python
134+
>>> "strings can contain # characters"
135+
'strings can contain # characters'
136+
>>>
137+
```
138+
88139
## Using Python as a calculator
89140

90141
```diff
@@ -132,6 +183,15 @@ and `)` also work the same way.
132183
>>>
133184
```
134185

186+
You can also leave out spaces to show what's calculated first. Python
187+
ignores it, but our code will be easier to read for people.
188+
189+
```python
190+
>>> 1 + 2*3 # now it looks like 2*3 is calculated first
191+
7
192+
>>>
193+
```
194+
135195
Python also supports many other kinds of calculations, but most of the
136196
time you don't need them. Actually you don't need even these
137197
calculations most of the time, but these calculations are probably
@@ -146,7 +206,9 @@ enough when you need to calculate something.
146206
- We can enter any Python commands to the interactive `>>>` prompt, and
147207
it will echo back the result.
148208
- `+`, `-`, `*` and `/` work in Python just like in math.
149-
- Pieces of text starting with a `#` are comments.
209+
- Pieces of text starting with a `#` are comments and pieces of text in
210+
quotes are strings.
211+
- You can use single quotes and double quotes however you want.
150212

151213
***
152214

basics/lists-and-tuples.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,6 @@ like this:
230230

231231
![Different lists.](../images/differentlist.png)
232232

233-
If you're using Python 3.2 or older you need to do `a[:]` instead
234-
of `a.copy()`. `a[:]` is a slice of the whole list, just like
235-
`a[0:]`.
236-
237233
## Tuples
238234

239235
Tuples are a lot like lists, but they're immutable so they

basics/loops.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,6 @@ Or if we just want to clear a list, we can use the `clear`
300300
>>>
301301
```
302302

303-
If you're using Python 3.2 or older you need to use `stuff[:]` instead
304-
of `stuff.copy()` and `stuff[:] = []` instead of `stuff.clear()`.
305-
`stuff[:]` is a slice of the whole list, just like `stuff[0:]`.
306-
307303
## Summary
308304

309305
- A loop means repeating something multiple times.

0 commit comments

Comments
 (0)