| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,7 @@ | |||
| 4 | 4 | I have come to see that most new python programmers have a hard time | |
| 5 | 5 | figuring out the \*args and \*\*kwargs magic variables. So what are they | |
| 6 | 6 | ? First of all let me tell you that it is not necessary to write \*args | |
| 7 | - or \*\*kwargs. Only the ``*`` (aesteric) is necessary. You could have | ||
| 7 | + or \*\*kwargs. Only the ``*`` (asterisk) is necessary. You could have | ||
| 8 | 8 | also written \*var and \*\*vars. Writing \*args and \*\*kwargs is just a | |
| 9 | 9 | convention. So now lets take a look at \*args first. | |
| 10 | 10 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,7 +34,7 @@ not. So we can do: | |||
| 34 | 34 | ||
| 35 | 35 | favourite_colours = defaultdict(list) | |
| 36 | 36 | ||
| 37 | - for name, colour in order: | ||
| 37 | + for name, colour in colours: | ||
| 38 | 38 | favourite_colours[name].append(colour) | |
| 39 | 39 | ||
| 40 | 40 | print(favourite_colours) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -67,12 +67,12 @@ recently: | |||
| 67 | 67 | .. code:: python | |
| 68 | 68 | ||
| 69 | 69 | mcase = {'a': 10, 'b': 34, 'A': 7, 'Z': 3} | |
| 70 | - | ||
| 70 | + | ||
| 71 | 71 | mcase_frequency = { | |
| 72 | 72 | k.lower(): mcase.get(k.lower(), 0) + mcase.get(k.upper(), 0) | |
| 73 | 73 | for k in mcase.keys() | |
| 74 | 74 | } | |
| 75 | - | ||
| 75 | + | ||
| 76 | 76 | # mcase_frequency == {'a': 17, 'z': 3, 'b': 34} | |
| 77 | 77 | ||
| 78 | 78 | In the above example we are combining the values of keys which are same | |
@@ -87,14 +87,10 @@ comprehensions a lot. You can also quickly switch keys and values of a dictionar | |||
| 87 | 87 | ^^^^^^^^^^^^^^^^^^^^^^ | |
| 88 | 88 | ||
| 89 | 89 | They are also similar to list comprehensions. The only difference is | |
| 90 | - that they use round brackets ``()`` and return a generator. Here is an | ||
| 91 | - example: | ||
| 90 | + that they use braces ``{}``. Here is an example: | ||
| 92 | 91 | ||
| 93 | 92 | .. code:: python | |
| 94 | 93 | ||
| 95 | - squared = (x**2 for x in range(10)) | ||
| 94 | + squared = {x for x in [1, 1, 2]} | ||
| 96 | 95 | print(squared) | |
| 97 | - # Output: <generator object <genexpr> at 0x00000000029931B0> | ||
| 98 | - squared.next() | ||
| 99 | - # Output: 0 | ||
| 100 | - | ||
| 96 | + # Output: {1, 2} | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,7 @@ expressions. | |||
| 12 | 12 | ||
| 13 | 13 | .. code:: python | |
| 14 | 14 | ||
| 15 | - condition_is_true if condition_true else condition_is_false | ||
| 15 | + condition_is_true if condition else condition_is_false | ||
| 16 | 16 | ||
| 17 | 17 | **Example:** | |
| 18 | 18 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments