Skip to content

Commit 811da97

Browse files
committed
working on stuff
1 parent 85108ad commit 811da97

File tree

6 files changed

+249
-85
lines changed

6 files changed

+249
-85
lines changed

TODO.md

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,25 @@
22

33
This tutorial is not complete. It still needs:
44

5-
- Dictionaries and tuples (can be in the same thing since tuples
6-
are needed for dict keys)
7-
- Explain `for a, b in thing` somewhere
5+
- range somewhere
6+
- Better lists chapter, also introduce tuples in it
87
- **More exercises and examples everywhere!**
9-
- Exceptions
108
- explain bool(x) and why it matters
119
- classes part 2
12-
- non-public `_variables`
13-
- singular inheritance, inheritance of built-in classes
10+
- non-public `_variables` (maybe reading PEP-8 is enough to explain this?)
11+
- "singular" inheritance, inheritance of built-in classes
1412
- using super
1513
- no classes part 3 with multiple inheritance, it's not something people need
16-
- quick overview of some of the most useful built-in functions and classes
17-
- range
18-
- zip
19-
- classmethod and staticmethod
2014
- iterables and iterators: something most Python programmers need to be
2115
aware of
2216
- last chapter: "What should I do now?" links to other resources
2317
- GUI programming tutorials
2418
- easygui
2519
- tkinter in effbot (warn the readers about star imports)
26-
- pyqt5 in zetcode
20+
- pyqt5 in zetcode (warn about mixedCase)
2721
- gtk+ 3 in readthedocs
2822
- a pygame tutorial
2923
- David Beazley's metaprogramming and other talks
30-
- a regex tutorial
31-
32-
33-
## Fix these
34-
35-
- the chapter about installing python should mention that python 2 and 3
36-
can be and often are installed at the same time
24+
- "What the heck is this?" section for stuff i haven't talked about
25+
- regexes
26+
- yield

dicts.md

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# Dictionaries
2+
3+
**TODO:** write the lists-and-tuples.md this tutorial links to.
4+
5+
Now we know how [lists and tuples](lists-and-tuples.md) work and how
6+
to [for loop](loops.md#for-loops) over them. We also did an exercise
7+
with code like this:
8+
9+
```py
10+
userlist = [
11+
('me', 'my password'),
12+
('you', 'your password'),
13+
]
14+
```
15+
16+
Then to check if a username and password were correct we did
17+
`(username, password) in userlist`. Adding new users was also easy as
18+
appending to that list.
19+
20+
What if we need to check if a username is in the users, but we don't
21+
need to know the password? `username in userlist` is always False
22+
because the user list consists of (username,password) pairs, so we need
23+
to for loop over the whole list:
24+
25+
```py
26+
username_exists = False
27+
for user in userlist:
28+
if user[0] == username:
29+
username_exists = True
30+
break
31+
if username_exists:
32+
# do something
33+
```
34+
35+
Getting a user's password also requires a similar loop:
36+
37+
```py
38+
password = None
39+
for user in userlist:
40+
if user[0] == username:
41+
password = user[1]
42+
break
43+
# make sure password isn't still None and do something with it
44+
```
45+
46+
This works just fine because our user list only contains two users, but
47+
it would be slow if the userlist was bigger.
48+
49+
## What are dictionaries?
50+
51+
A better way to store user information might be a dictionary.
52+
53+
```py
54+
passwords = {
55+
'me': 'my password',
56+
'you': 'your password',
57+
}
58+
```
59+
60+
Here `'me'` and `'you'` are **keys** in the dictionary, and
61+
`'my password'` and `'your password'` are their **values**. Dictionaries
62+
are often named by their values. This dictionary has passwords as its
63+
values so I named the variable `passwords`.
64+
65+
There are a few big differences between dictionaries and lists of pairs:
66+
67+
- Dictionaries are not ordered. There's **no guarantees** about which
68+
order the username:password pairs appear in when we do something
69+
with the dictionary.
70+
- Checking if a key is in the dictionary is simple and fast. We don't
71+
need to for loop through the whole dictionary.
72+
- Getting the value of a key is also simple and fast.
73+
- We can't have the same key in the dictionary multiple times, but
74+
multiple different keys can have the same value. This means that
75+
**multiple users can't have the same name, but they can have the
76+
same passwords**.
77+
78+
But wait... this is a lot like variables are! Our variables are not
79+
ordered, getting a value of a variable is fast and easy and we can't
80+
have multiple variables with the same name.
81+
82+
Variables are actually stored in a dictionary. We can get that
83+
dictionary with the globals function. In this dictionary, keys are
84+
variable names and values are what our variables point to.
85+
86+
```py
87+
>>> globals()
88+
{'userlist': [('me', 'my password'), ('you', 'your password')],
89+
'passwords': {'me': 'my password', 'you': 'your password'},
90+
...many other things we don't need to care about...
91+
}
92+
>>>
93+
```
94+
95+
So if you have trouble remembering how dictionaries work just compare
96+
them to variables.
97+
98+
## What can we do with dictionaries?
99+
100+
Dictionaries have some similarities with lists. For example, both
101+
lists and dictionaries have a length.
102+
103+
```py
104+
>>> len(userlist) # contains two elements
105+
2
106+
>>> len(passwords) # contains two key:value pairs
107+
2
108+
>>>
109+
```
110+
111+
We can get a value of a key with `the_dict[key]`. Trying to get the
112+
value of a non-existing key gives us an error. We can also add new
113+
key:value pairs by doing `the_dict[key] = value`.
114+
115+
```py
116+
>>> passwords['me']
117+
'my password'
118+
>>> passwords['you']
119+
'your password'
120+
>>> passwords['lol']
121+
Traceback (most recent call last):
122+
File "<stdin>", line 1, in <module>
123+
KeyError: 'lol'
124+
>>> passwords["lol"] = "lol's password"
125+
>>> passwords
126+
{'lol': "lol's password", 'you': 'your password', 'me': 'my password'}
127+
>>>
128+
```
129+
130+
For looping over a dictionary gets its keys, and checking if something's
131+
in the dictionary checks if the dictionary has a key like that. This can
132+
be confusing at first but you'll get used to this.
133+
134+
```py
135+
>>> 'me' in passwords
136+
True
137+
>>> 'my password' in passwords
138+
False
139+
>>> for name in passwords:
140+
... print(name)
141+
...
142+
lol
143+
you
144+
me
145+
>>>
146+
```
147+
148+
Dictionaries have a values method that we can use if we want to do
149+
something with the values:
150+
151+
```py
152+
>>> passwords.values()
153+
dict_values(["lol's password", 'your password', 'my password'])
154+
>>>
155+
```
156+
157+
The values method returned a `dict_values` object. Things like this
158+
behave a lot like lists and usually we don't need to convert them to
159+
lists.
160+
161+
```py
162+
>>> for password in passwords.values():
163+
... print(password)
164+
...
165+
lol's password
166+
your password
167+
my password
168+
>>>
169+
```
170+
171+
We can do things like `list(passwords.values())` if we need a real list
172+
for some reason, but doing that can slow down our program if the
173+
dictionary is big. There's also a keys method, but usually we don't need
174+
it because the dictionary itself behaves a lot like a list of keys.
175+
176+
If we need both keys and values we can use the items method with the
177+
`for first, second in thing` trick.
178+
179+
```py
180+
>>> passwords.items()
181+
dict_items([('lol', "lol's password"),
182+
('you', 'your password'),
183+
('me', 'my password')])
184+
>>> for name, password in passwords.items():
185+
... print(name + ": " + password)
186+
...
187+
lol: lol's password
188+
you: your password
189+
me: my password
190+
>>>
191+
```

loops.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
In programming, a **loop** means repeating something multiple times.
44
There are different kinds of loops:
55

6-
- **While loops** repeat something while a condition is true.
7-
- **Until loops** repeat something until a condition is true.
8-
- **For loops** repeat something for each element of a sequence.
6+
- [While loops](#while-loops) repeat something while a condition is true.
7+
- [Until loops](#until-loops) repeat something while a condition is false.
8+
- [For loops](#for-loops) repeat something for each element of a list.
99

1010
We'll talk about all of these in this tutorial.
1111

@@ -230,11 +230,22 @@ how about you
230230
Note that `for thing in stuff:` is not same as `for (thing in stuff):`.
231231
Here the `in` keyword is just a part of the for loop and it has a
232232
different meaning than it would have if we had `thing in stuff` without
233-
a `for`.
233+
a `for`. Trying to do `for (thing in stuff):` creates an error:
234+
235+
```py
236+
>>> for (thing in stuff):
237+
File "<stdin>", line 1
238+
for (thing in stuff):
239+
^
240+
SyntaxError: invalid syntax
241+
>>>
242+
```
234243

235244
Right now the while loop version might seem easier to understand for
236245
you, but later you'll realize that for loops are much easier to work
237246
with than while loops and index variables, especially in large projects.
247+
For looping is also a little bit faster than while looping with an index
248+
variable.
238249

239250
There's only one big limitation with for looping over lists. We
240251
shouldn't modify the list in the for loop. If we do, the results can

modules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ that are built into Python. The official documentation is
217217
>>> # stdin is short for standard input
218218
>>> # stdout is short for standard output
219219
>>> # stderr is short for standard errors
220-
>>> print("Hello!", file=sys.stdout)
220+
>>> print("Hello!", file=sys.stdout) # this is where prints go by default
221221
Hello!
222222
>>> print("Hello!", file=sys.stderr) # use this for error messages
223223
Hello!

trey-hunner-zip-and-enumerate.md

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ assigning values to multiple variables at once. It works like this:
1818

1919
We can use `()` and `[]` around these values however we want and
2020
everything will still work the same way. `[]` creates a list, and
21-
`()` creates a tuple. We'll talk more about tuples later.
21+
`()` creates a tuple.
2222

2323
```py
2424
>>> [a, b] = (1, 2)
@@ -41,7 +41,7 @@ side.
4141
>>>
4242
```
4343

44-
What actually happened is that Python created a tuple automatically.
44+
Python created a tuple automatically.
4545

4646
```py
4747
>>> 1, 2
@@ -76,67 +76,9 @@ c 3
7676
>>>
7777
```
7878

79-
Now you're ready to read [this looping
79+
Now you're ready to read [this awesome looping
8080
tutorial](http://treyhunner.com/2016/04/how-to-loop-with-indexes-in-python/).
81-
Read it now, then read the summary and do the exercises.
82-
83-
## Summary
84-
85-
Assigning multiple values at once:
86-
87-
```py
88-
>>> a, b, c = 1, 2, 3
89-
>>> a
90-
1
91-
>>> b
92-
2
93-
>>> c
94-
3
95-
>>>
96-
```
97-
98-
For looping over a list of pairs:
99-
100-
```py
101-
>>> stuff = [('a', 1), ('b', 2), ('c', 3)]
102-
>>> for a, b in stuff:
103-
... print(a, b)
104-
...
105-
a 1
106-
b 2
107-
c 3
108-
>>>
109-
```
110-
111-
For looping over two lists at once:
112-
113-
```py
114-
>>> colors = ['red', 'green', 'blue']
115-
>>> letters = ['R', 'G', 'B']
116-
>>> for letter, color in zip(letters, colors):
117-
... print(letter, color)
118-
...
119-
R red
120-
G green
121-
B blue
122-
>>>
123-
```
124-
125-
For looping over a list with indexes:
126-
127-
```py
128-
>>> fillernames = ['foo', 'bar', 'biz', 'baz', 'spam', 'eggs']
129-
>>> for index, name in enumerate(fillernames):
130-
... print(index, name)
131-
...
132-
0 foo
133-
1 bar
134-
2 biz
135-
3 baz
136-
4 spam
137-
5 eggs
138-
>>>
139-
```
81+
Read it now, then come back here and do the exercises.
14082

14183
## Exercises
14284

0 commit comments

Comments
 (0)