From e89e711028cd3bdd315dbd45d475a062411918d8 Mon Sep 17 00:00:00 2001 From: Luis Date: Sun, 21 Oct 2018 00:16:47 -0500 Subject: [PATCH 1/4] variable feedback --- basics/variables.md | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/basics/variables.md b/basics/variables.md index 4b9c25b..d5e373e 100644 --- a/basics/variables.md +++ b/basics/variables.md @@ -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 ``` This is not limited to integers. @@ -136,6 +165,16 @@ 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' +>>> + + 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: From 5ed57c0d28a7f65dfe25d3a9266347bdf1b8a6e8 Mon Sep 17 00:00:00 2001 From: Luis Date: Sun, 21 Oct 2018 00:23:31 -0500 Subject: [PATCH 2/4] variable feedback correction --- basics/variables.md | 1 + 1 file changed, 1 insertion(+) diff --git a/basics/variables.md b/basics/variables.md index d5e373e..497e6b0 100644 --- a/basics/variables.md +++ b/basics/variables.md @@ -173,6 +173,7 @@ it causes a type error. >>> a -= 3 TypeError: unsupported operand type(s) for -: 'str' and 'str' >>> +``` Now we also understand why typing hello to the prompt didn't work in From c8c4dbf88f0ea027a35b3d9eacb847350b2e597b Mon Sep 17 00:00:00 2001 From: Luis Date: Sun, 21 Oct 2018 13:13:24 -0500 Subject: [PATCH 3/4] more content was added to lists and tuples --- basics/lists-and-tuples.md | 100 ++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 2 deletions(-) diff --git a/basics/lists-and-tuples.md b/basics/lists-and-tuples.md index 77ea0ca..2d913df 100644 --- a/basics/lists-and-tuples.md +++ b/basics/lists-and-tuples.md @@ -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'] ``` 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'] + +>>> +``` + + 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: ![Different lists.](../images/differentlist.png) + +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 +``` + + Tuples don't have methods like append, extend and remove because they can't change themselves in-place. From b3a0219c26a16627c8b6fa9fbc73c850001a3a7c Mon Sep 17 00:00:00 2001 From: Luis Date: Sun, 21 Oct 2018 14:56:58 -0500 Subject: [PATCH 4/4] more content was added to loops --- basics/loops.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/basics/loops.md b/basics/loops.md index 57f6b50..3f27c41 100644 --- a/basics/loops.md +++ b/basics/loops.md @@ -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 +>>> +``` + 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: