Skip to content

Commit 9019195

Browse files
committed
Merge dictionaries to Readme and jpynb
1 parent c8413bf commit 9019195

4 files changed

Lines changed: 54 additions & 10 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ All contributions are welcome:
7979
- [The get() Method](#the-get-method)
8080
- [The setdefault() Method](#the-setdefault-method)
8181
- [Pretty Printing](#pretty-printing)
82+
- [Merge two dictionaries](#merge-two-dictionaries)
8283
- [sets](#sets)
8384
- [Initializing a set](#initializing-a-set)
8485
- [sets: unordered collections of unique elements](#sets-unordered-collections-of-unique-elements)
@@ -1542,6 +1543,22 @@ Using `setdefault` we could make the same code more shortly:
15421543

15431544
[*Return to the Top*](#python-cheatsheet)
15441545

1546+
### Merge two dictionaries
1547+
1548+
```python
1549+
# in Python 3.5+:
1550+
>>> x = {'a': 1, 'b': 2}
1551+
>>> y = {'b': 3, 'c': 4}
1552+
>>> z = {**x, **y}
1553+
>>> z
1554+
{'c': 4, 'a': 1, 'b': 3}
1555+
1556+
# in Python 2.7
1557+
>>> z = dict(x, **y)
1558+
>>> z
1559+
{'c': 4, 'a': 1, 'b': 3}
1560+
```
1561+
15451562
## sets
15461563

15471564
From the Python 3 [documentation](https://docs.python.org/3/tutorial/datastructures.html)

blog_files/pysheet.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
From **Highest** to **Lowest** precedence:
66

7-
| Operators | Example |
8-
| --------- | ---------------- |
9-
| ** | `2 ** 3 = 8` |
10-
| % | `22 % 8 = 6` |
11-
| // | `22 // 8 = 2` |
12-
| / | `22 / 8 = 2.75` |
13-
| * | `3 * 3 = 9` |
14-
| - | `5 - 2 = 3` |
15-
| + | `2 + 2 = 4` |
7+
| Operators | Operation | Example |
8+
| --------- | ---------------- | --------------- |
9+
| ** | Exponent | `2 ** 3 = 8` |
10+
| % | Modulus/Remaider | `22 % 8 = 6` |
11+
| // | Integer division | `22 // 8 = 2` |
12+
| / | Division | `22 / 8 = 2.75` |
13+
| * | Multiplication | `3 * 3 = 9` |
14+
| - | Subtraction | `5 - 2 = 3` |
15+
| + | Addition | `2 + 2 = 4` |
1616

1717
Examples of expressions in the interactive shell:
1818

@@ -1225,6 +1225,7 @@ Using `setdefault` we could make the same code more shortly:
12251225
'w': 2,
12261226
'y': 1}
12271227
```
1228+
12281229
### Merge two dictionaries
12291230

12301231
```python
@@ -1241,7 +1242,6 @@ Using `setdefault` we could make the same code more shortly:
12411242
{'c': 4, 'a': 1, 'b': 3}
12421243
```
12431244

1244-
12451245
## sets
12461246

12471247
From the Python 3 [documentation](https://docs.python.org/3/tutorial/datastructures.html)

python_cheat_sheet.ipynb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
" - [The get() Method](#the-get-method)\n",
8686
" - [The setdefault() Method](#the-setdefault-method)\n",
8787
" - [Pretty Printing](#pretty-printing)\n",
88+
" - [Merge two dictionaries](#merge-two-dictionaries)\n",
8889
"- [sets](#sets)\n",
8990
" - [Initializing a set](#initializing-a-set)\n",
9091
" - [sets: unordered collections of unique elements](#sets-unordered-collections-of-unique-elements)\n",
@@ -2599,6 +2600,32 @@
25992600
"source": [
26002601
"[*Return to the Top*](#python-cheatsheet)\n",
26012602
"\n",
2603+
"### Merge two dictionaries"
2604+
]
2605+
},
2606+
{
2607+
"cell_type": "code",
2608+
"execution_count": null,
2609+
"metadata": {},
2610+
"outputs": [],
2611+
"source": [
2612+
"# in Python 3.5+:\n",
2613+
">>> x = {'a': 1, 'b': 2}\n",
2614+
">>> y = {'b': 3, 'c': 4}\n",
2615+
">>> z = {**x, **y}\n",
2616+
">>> z\n",
2617+
"{'c': 4, 'a': 1, 'b': 3}\n",
2618+
"\n",
2619+
"# in Python 2.7\n",
2620+
">>> z = dict(x, **y)\n",
2621+
">>> z\n",
2622+
"{'c': 4, 'a': 1, 'b': 3}"
2623+
]
2624+
},
2625+
{
2626+
"cell_type": "markdown",
2627+
"metadata": {},
2628+
"source": [
26022629
"## sets\n",
26032630
"\n",
26042631
"From the Python 3 [documentation](https://docs.python.org/3/tutorial/datastructures.html)\n",

python_cheat_sheet.pdf

1.74 KB
Binary file not shown.

0 commit comments

Comments
 (0)