-
Notifications
You must be signed in to change notification settings - Fork 625
More content was added #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,7 +100,8 @@ Lists have a few [useful | |
methods](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists). | ||
Some of the most commonly used ones are append, extend and remove. | ||
`append` adds an item to the end of a list, `extend` adds | ||
multiple items from another list and `remove` removes an item. | ||
multiple items from another list and `remove` removes an item. | ||
It can also be added with the method of `insert` | ||
|
||
```python | ||
>>> names | ||
|
@@ -115,7 +116,9 @@ multiple items from another list and `remove` removes an item. | |
>>> names.extend(['go|dfish', 'theelous3']) # wb guys | ||
>>> names | ||
['wub_wub', 'RubyPinch', 'Nitori', 'Akuli', 'go|dfish', 'theelous3'] | ||
>>> | ||
>>> names.insert(len(names), "Aly") | ||
>>> names | ||
['wub_wub', 'RubyPinch', 'Nitori', 'Akuli', 'go|dfish', 'theelous3', 'Aly'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :) |
||
``` | ||
|
||
Note that `remove` removes only the first match it finds. | ||
|
@@ -128,6 +131,26 @@ Note that `remove` removes only the first match it finds. | |
>>> | ||
``` | ||
|
||
The method `pop` also works for delete elements of the list. | ||
|
||
```python | ||
>>> names = ['theelous3', 'go|dfish', 'theelous3'] | ||
>>> names.pop(1) | ||
>>> names # the second item was removed | ||
'go|dfish' | ||
>>> names | ||
['theelous3', 'theelous3'] | ||
|
||
>>> names = ['theelous3', 'go|dfish', 'theelous3'] | ||
>>> names.pop() | ||
theelous3' | ||
>>> names | ||
['theelous3', 'go|dfish'] | ||
|
||
>>> | ||
``` | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add an explanation about how |
||
If we need to remove all matching items we can use a simple while loop. | ||
We'll talk more about loops [in the next chapter](loops.md). | ||
|
||
|
@@ -230,6 +253,44 @@ like this: | |
|
||
 | ||
|
||
|
||
We can count the number of items that have a list. | ||
|
||
```python | ||
>>> a = [1, 2, 3, 4, 2, 5, 2] | ||
>>> a.count(2) | ||
3 | ||
>>> a.count(5) | ||
1 | ||
>>> a.count(9) | ||
0 | ||
>>> a = ['theelous3', 'wub_wub', 'RubyPinch', 'go|dfish', 'Nitori'] | ||
>>> a.count('wub_wub') | ||
1 | ||
``` | ||
|
||
|
||
We can sort the items that have a list | ||
|
||
```python | ||
>>> a = [1, 2, 3, 4, 2, 5, 2] | ||
>>> a.sort() | ||
>>> a | ||
[1, 2, 2, 2, 3, 4, 5] | ||
>>> a.sort(reverse = True) | ||
>>> a | ||
[5, 4, 3, 2, 2, 2, 1] | ||
>>> a = ['wub_wub', 'theelous3', 'RubyPinch', 'go|dfish', 'Nitori'] | ||
>>> a.sort() | ||
>>> a | ||
['Nitori', 'RubyPinch', 'go|dfish', 'theelous3', 'wub_wub'] | ||
>>> a.sort(reverse = True) | ||
['wub_wub', 'theelous3', 'go|dfish', 'RubyPinch', 'Nitori'] | ||
>>> | ||
``` | ||
|
||
|
||
|
||
## Tuples | ||
|
||
Tuples are a lot like lists, but they're immutable so they | ||
|
@@ -274,6 +335,41 @@ but some people like to do it this way. | |
>>> | ||
``` | ||
|
||
|
||
You can have nested tuples. | ||
|
||
```python | ||
>>> n = 1, 2, 3 | ||
>>> n | ||
(1, 2, 3) | ||
>>> n[0] | ||
1 | ||
>>> l = 'a', 'b', 'c' | ||
>>> l | ||
('a', 'b', 'c') | ||
>>> l[0] | ||
'a' | ||
>>> t = n, l | ||
>>> t | ||
((1, 2, 3), ('a', 'b', 'c')) #The tuples n and l are nested | ||
>>> t[0] | ||
(1, 2, 3) | ||
>>> t[1] | ||
('a', 'b', 'c') | ||
>>> t[1][2] | ||
'c' | ||
>>> v = ([1, 2, 3], [3, 2, 1,[7, 8, 9]]) | ||
>>> v | ||
([1, 2, 3], [3, 2, 1, [7, 8, 9]]) | ||
>>> v[1] | ||
[3, 2, 1, [7, 8, 9]] | ||
>>> v[1][3] | ||
[7, 8, 9] | ||
>>> v[1][3][0] | ||
7 | ||
``` | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The most common way to nest tuples and lists is to have a list of tuples. For example, if you have names and phone numbers, it could be like |
||
Tuples don't have methods like append, extend and remove | ||
because they can't change themselves in-place. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,6 +133,29 @@ immediately. | |
>>> | ||
``` | ||
|
||
While with a condition | ||
|
||
```python | ||
>>> cont = 10 | ||
>>> while (cont >= 0): | ||
... print(cont) | ||
... cont -= 1 | ||
... | ||
10 | ||
9 | ||
8 | ||
7 | ||
6 | ||
5 | ||
4 | ||
3 | ||
2 | ||
1 | ||
0 | ||
>>> | ||
``` | ||
|
||
|
||
## Until loops | ||
|
||
Python doesn't have until loops. If we need an until loop, we can use | ||
|
@@ -212,6 +235,7 @@ how about you | |
>>> | ||
``` | ||
|
||
|
||
Without the comments, that's only two simple lines, and one variable. | ||
Much better than anything else we tried before. | ||
|
||
|
@@ -232,6 +256,7 @@ Here the `in` keyword is just a part of the for loop and it has a | |
different meaning than it would have if we had `thing in stuff` without | ||
a `for`. Trying to do `for (thing in stuff):` creates an error. | ||
|
||
|
||
Right now the while loop version might seem easier to understand for | ||
you, but later you'll realize that for loops are much easier to work | ||
with than while loops and index variables, especially in large projects. | ||
|
@@ -260,9 +285,30 @@ c | |
>>> | ||
``` | ||
|
||
You can print the multiplication tables. | ||
|
||
```python | ||
>>> table = 2 | ||
>>> for i in range(1, 11): | ||
... print(table,"x", i, "=",(table*i)) | ||
... | ||
2 x 1 = 2 | ||
2 x 2 = 4 | ||
2 x 3 = 6 | ||
2 x 4 = 8 | ||
2 x 5 = 10 | ||
2 x 6 = 12 | ||
2 x 7 = 14 | ||
2 x 8 = 16 | ||
2 x 9 = 18 | ||
2 x 10 = 20 | ||
>>> | ||
``` | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there an |
||
If we can for loop over something, then that something is **iterable**. | ||
Lists, tuples and strings are all iterable. | ||
|
||
|
||
There's only one big limitation with for looping over lists. We | ||
shouldn't modify the list in the for loop. If we do, the results can | ||
be surprising: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,11 +118,40 @@ can also use `+=`, `-=`, `*=` and `/=` instead of `+`, `-`, `*` and | |
`/`. The "advanced" `%=`, `//=` and `**=` also work. | ||
|
||
```python | ||
>>> a = 11 | ||
>>> a += 2 # a = a + 2 | ||
>>> a | ||
13 | ||
|
||
>>> a = 11 | ||
>>> a -= 2 # a = a - 2 | ||
>>> a | ||
9 | ||
|
||
a = 11 | ||
>>> a *= 2 # a = a * 2 | ||
>>> a | ||
22 | ||
|
||
>>> a = 11 | ||
>>> a /= 2 # a = a / 2 | ||
>>> | ||
>>> a | ||
5.5 | ||
|
||
>>> a = 11 | ||
>>> a %= 2 # a = a % 2 | ||
>>> a | ||
1 | ||
|
||
>>> a = 11 | ||
>>> a //= 2 # a = a // 2 | ||
>>> a | ||
5 | ||
|
||
a = 11 | ||
>>> a **= 2 # a = a ^ 2 | ||
>>> a | ||
121 | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need this many examples? A couple examples would be good though, maybe with strings because not everyone like numbers and math. |
||
|
||
This is not limited to integers. | ||
|
@@ -136,6 +165,17 @@ This is not limited to integers. | |
>>> | ||
``` | ||
|
||
But, you can only use `+=` and `*=`. Because of with the others operators | ||
it causes a type error. | ||
|
||
```python | ||
>>> a = 'hello' | ||
>>> a -= 3 | ||
TypeError: unsupported operand type(s) for -: 'str' and 'str' | ||
>>> | ||
``` | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These things are best suited for "Fix this program" exercises IMO. What do you think? |
||
Now we also understand why typing hello to the prompt didn't work in | ||
the beginning of this tutorial. But we can assign something to a | ||
variable called hello and then type hello: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you remember when you used insert last time in a real program? I almost always need append. If people need something else, it's not hard to google "python add item to beginning of list" or whatever actually needs to be done. A mention about googling these things might be good though.