Skip to content

Commit

Permalink
drop support for old pythons
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Jul 25, 2020
1 parent 4c220ea commit 5c14594
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 70 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -12,7 +12,7 @@ or very little programming experience. If you have programmed a lot in
the past using some other language you may want to read [the official
tutorial](https://docs.python.org/3/tutorial/) instead.

You can use Python 3.3 or any newer Python with this tutorial. **Don't
You can use Python 3.5 or any newer Python with this tutorial. **Don't
use Python 2 because it's no longer supported.**

## List of contents
Expand Down
67 changes: 12 additions & 55 deletions advanced/datatypes.md
Expand Up @@ -316,68 +316,25 @@ TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
>>>
```

Dictionaries have an `update` method that adds everything from another
dictionary into it. So we can merge dictionaries like this:
Usually it's easiest to do this:

```python
>>> merged = {}
>>> merged.update({'a': 1, 'b': 2})
>>> merged.update({'c': 3})
>>> merged
{'c': 3, 'b': 2, 'a': 1}
>>>
```

Or we can [write a function](../basics/defining-functions.md) like this:

```python
>>> def merge_dicts(dictlist):
... result = {}
... for dictionary in dictlist:
... result.update(dictionary)
... return result
...
>>> merge_dicts([{'a': 1, 'b': 2}, {'c': 3}])
{'c': 3, 'b': 2, 'a': 1}
>>>
>>> dict1 = {'a': 1, 'b': 2}
>>> dict2 = {'c': 3}
>>> {**dict1, **dict2}
{'a': 1, 'b': 2, 'c': 3}
```

Kind of like counting things, merging dictionaries is also a commonly
needed thing and there's a class just for it in the `collections`
module. It's called ChainMap:
Dictionaries also have an `update` method that adds everything from another
dictionary into it, and you can use that too. This was the most common way to
do it before Python supported `{**dict1, **dict2}`.

```python
>>> import collections
>>> merged = collections.ChainMap({'a': 1, 'b': 2}, {'c': 3})
>>> merged = {}
>>> merged.update({'a': 1, 'b': 2})
>>> merged.update({'c': 3})
>>> merged
ChainMap({'b': 2, 'a': 1}, {'c': 3})
>>>
```

Our `merged` is kind of like the Counter object we created earlier. It's
not a dictionary, but it behaves like a dictionary.

```python
>>> for key, value in merged.items():
... print(key, value)
...
c 3
b 2
a 1
>>> dict(merged)
{'c': 3, 'b': 2, 'a': 1}
>>>
```

Starting with Python 3.5 it's possible to merge dictionaries like this.
**Don't do this unless you are sure that no-one will need to run your
code on Python versions older than 3.5.**

```python
>>> first = {'a': 1, 'b': 2}
>>> second = {'c': 3, 'd': 4}
>>> {**first, **second}
{'d': 4, 'c': 3, 'a': 1, 'b': 2}
{'a': 1, 'b': 2, 'c': 3}
>>>
```

Expand Down
7 changes: 5 additions & 2 deletions basics/exceptions.md
Expand Up @@ -319,7 +319,7 @@ it's usually better to use `sys.stderr` and `sys.exit`.
## Exception hierarchy

Exceptions are organized like this. I made this tree with [this
program](https://github.com/Akuli/classtree/) on Python 3.4. You may
program](https://github.com/Akuli/classtree/) on Python 3.7. You may
have more or less exceptions than I have if your Python is newer or
older than mine, but they should be mostly similar.

Expand All @@ -333,6 +333,7 @@ older than mine, but they should be mostly similar.
├── BufferError
├── EOFError
├── ImportError
│ └── ModuleNotFoundError
├── LookupError
│ ├── IndexError
│ └── KeyError
Expand All @@ -357,7 +358,9 @@ older than mine, but they should be mostly similar.
│ └── TimeoutError
├── ReferenceError
├── RuntimeError
│ └── NotImplementedError
│ ├── NotImplementedError
│ └── RecursionError
├── StopAsyncIteration
├── StopIteration
├── SyntaxError
│ └── IndentationError
Expand Down
24 changes: 12 additions & 12 deletions basics/modules.md
Expand Up @@ -28,17 +28,17 @@ gave us?

```python
>>> random
<module 'random' from '/usr/lib/python3.4/random.py'>
<module 'random' from '/usr/lib/python3.7/random.py'>
>>>
```

So it's a module, and it comes from a path... but what does
all that mean?

Now open the folder that contains your `random.py` is. On my
system it's `/usr/lib/python3.4`, but yours will probably be
Now open the folder that contains your `random.py`. On my
system it's `/usr/lib/python3.7`, but yours will probably be
different. To open a folder in your file manager you can press
Windows-R on Windows or Alt+F2 on most GNU/Linux distributions,
Windows-R on Windows or Alt+F2 on most Linux distributions,
and just type your path there. I don't have an up-to-date copy
of OSX so unfortunately I have no idea what you need to do on
OSX.
Expand Down Expand Up @@ -138,11 +138,11 @@ places that modules are searched from:
<module 'sys' (built-in)>
>>> sys.path
['',
'/usr/lib/python3.4',
'/usr/lib/python3.4/plat-i386-linux-gnu',
'/usr/lib/python3.4/lib-dynload',
'/home/akuli/.local/lib/python3.4/site-packages',
'/usr/local/lib/python3.4/dist-packages',
'/usr/lib/python37.zip',
'/usr/lib/python3.7',
'/usr/lib/python3.7/lib-dynload',
'/home/akuli/.local/lib/python3.7/site-packages',
'/usr/local/lib/python3.7/dist-packages',
'/usr/lib/python3/dist-packages']
>>>
```
Expand Down Expand Up @@ -234,9 +234,9 @@ hello
>>>
>>> # information about Python's version, behaves like a tuple
>>> sys.version_info
sys.version_info(major=3, minor=4, micro=2, releaselevel='final', serial=0)
>>> sys.version_info[:3] # this is Python 3.4.2
(3, 4, 2)
sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)
>>> sys.version_info[:3] # this is Python 3.7.3
(3, 7, 3)
>>>
>>> sys.exit() # exit out of Python
```
Expand Down

0 comments on commit 5c14594

Please sign in to comment.