Cara menggunakan python 3.9 merge dictionaries

Untuk menjalankan Python ada banyak cara yang bisa dilakukan. Anda bisa menggunakan shell, terminal atau menggunakan IDE (Integrated Development Environment). Di bawah ini adalah langkah-langkah menjalankan Python dengan cara yang paling mudah.

Linux

  1. Buka terminal CTRL+ALT+T
  2. Ketik python maka Anda akan masuk ke Python shell.
  3. Tuliskan script Python Anda, contoh: print("Selamat datang di Python"). jika sudah tekan tombol ENTER, dan script Python akan dijalankan/eksekusi.
  4. Untuk keluar dari Python shell ketik exit()

atau

  1. Gunakan teks editor, misalnya gedit.
  2. Buat file baru, dan ketikan script python Anda, contoh: print("Selamat datang di Python").
  3. Save As dengan ekstensi .py (contoh: cetak.py).
  4. Jalankan file dengan menggunakan Terminal.
  5. Buka terminal CTRL+ALT+T.
  6. Masuk ke direktori dimana file Python Anda disimpan (contoh: cd /Users/admin/Desktop/).
  7. Jalankan script Python dengan menggunakan python diikuti dengan nama file (contoh: python cetak.py).
  8. Script Python Anda akan dieksekusi/dijalankan.

Windows

Menggunakan Shell

  1. Buka IDLE (python shell di windows), Anda bisa mencarinya di tombol START.
  2. Tuliskan script Python Anda, contoh: print("Selamat datang di Python"). jika sudah tekan tombol ENTER, dan script Python akan dijalankan/eksekusi.

  1. Untuk keluar dari Python shell ketik exit()

Menggunakan Script Editor

  1. Untuk menjalankan script yang disimpan dalam file, buka IDLE (python shell di windows), Anda bisa mencarinya di tombol START.
  2. Klik menu print("Selamat datang di Python")0
  3. Tulis script Python pada window yang muncul, contoh:
print("Belajar Python") print("di belajarpython.com")
  1. Simpan script lewat menu print("Selamat datang di Python")1
  2. Jalankan program dengan klik menu print("Selamat datang di Python")2

Mac OS

  1. Buka terminal.
  2. Ketik python maka Anda akan masuk ke Python shell.
  3. Tuliskan script Python Anda, contoh: print("Selamat datang di Python"). jika sudah tekan tombol ENTER, dan script Python akan dijalankan/eksekusi.
  4. Untuk keluar dari Python shell ketik exit()

atau

  1. Gunakan teks editor.
  2. Buat file baru, dan ketikan script python Anda, contoh: print("Selamat datang di Python").
  3. Save As dengan ekstensi .py (contoh: cetak.py).
  4. Jalankan file dengan menggunakan Terminal.
  5. Buka terminal CTRL+ALT+T
  6. Masuk ke direktori dimana file Python Anda disimpan (contoh: cd /Users/admin/Desktop/).
  7. Jalankan script Python dengan menggunakan python diikuti dengan nama file (contoh: python cetak.py).
  8. Script Python Anda akan dieksekusi/dijalankan.

Edit tutorial ini

Python 3.9 was released on Oct. 5, 2020 and it introduces some neat features and optimizations including PEP 584, Union Operators in the built-in class x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} 4; the so-called Dictionary Merge and Update Operators.

In this blog post we will go over the new operators to see if there are any advantages or disadvantages of using them over the earlier ways of merging and updating dictionaries.

The Dictionary Merge Operator

Given two or more dictionaries, we fuse them into a single one.

Let's start by diving into a short example demonstrating the old way of merging two dictionaries:

x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = {**x, **y} print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}

This merge creates an entirely new x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} 4 object x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} 6 where we unpack every value from x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} 7 and x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} 8. This way of merging two dictionaries feels unnatural and hardly obvious. If both dictionaries have the same keys, the values of dictionary x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} 7 are overwritten by the values of dictionary x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} 8.

According to Guido:

"I'm sorry for PEP 448, but even if you know about d in simpler
contexts, if you were to ask a typical Python user how to combine two dicts
into a new one, I doubt many people would think of # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging x | y print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = x | y print(id(z)) # 2670466542912 print(z is x) # False print(z is y) # False 1. I know I
myself had forgotten about it when this thread started! If you were to ask
a newbie who has learned a few things (e.g. sequence concatenation) they
would much more likely guess d1+d2."

Here's an example demonstrating the new dictionary merge operator, # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging x | y print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = x | y print(id(z)) # 2670466542912 print(z is x) # False print(z is y) # False 2:

x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}

But remember that the merge operator creates new dictionaries and leaves the two merged dictionaries unchanged:

# before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging x | y print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = x | y print(id(z)) # 2670466542912 print(z is x) # False print(z is y) # False

If the expression isn't assigned to a variable, it will be lost.

The same concept applies to the legacy merging method:

# before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False

So we have seen they have similar behaviors, and using the merge operator # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging x | y print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = x | y print(id(z)) # 2670466542912 print(z is x) # False print(z is y) # False 2 allows us to write cleaner code but besides this, what good is it for?

The operators have also been implemented for several other standard library packages like # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging x | y print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = x | y print(id(z)) # 2670466542912 print(z is x) # False print(z is y) # False 4.

To demonstrate the usefulness of the merge operator, # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging x | y print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = x | y print(id(z)) # 2670466542912 print(z is x) # False print(z is y) # False 2, let's take a look at the following example using defaultdict:

from collections import defaultdict user_not_found_message = 'Could not find any user matching the specified user id.' ceo = defaultdict( lambda: user_not_found_message, {'id': 1, 'name': 'Jose', 'title': 'Instructor'} ) author = defaultdict( lambda: user_not_found_message, {'id': 2, 'name': 'Vlad', 'title': 'Teaching Assistant'} )

By using the double asterisk, # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging x | y print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = x | y print(id(z)) # 2670466542912 print(z is x) # False print(z is y) # False 6, merging the two dictionaries will work, but the method is not aware of the class object so we will end up with a traditional dictionary instead:

print({**author, **ceo}) # {'id': 2, 'name': 'Jose', 'title': 'Author', 'title': 'Instructor'} print({**ceo, **author}) # {'id': 1, 'name': 'Vlad', 'title': 'Teaching Assistant'}

The power of the merge operator # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging x | y print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = x | y print(id(z)) # 2670466542912 print(z is x) # False print(z is y) # False 2 is that it is aware of the class objects. As such, a # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging x | y print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = x | y print(id(z)) # 2670466542912 print(z is x) # False print(z is y) # False 8 will be returned:

print(author | ceo) # defaultdict(<function <lambda> at 0x000002212125DE50>, {'id': 2, 'name': 'Jose', 'title': 'Instructor'}) print(ceo | author) # defaultdict(<function <lambda> at 0x000002212127A3A0>, {'id': 1, 'name': 'Vlad', 'title': 'Teaching Assistant'})

Note: the order of operands is very important as they will behave differently depending on the order they are arranged. In the example above we use both placements so the latter keys and values overwrite the former ones.

Another advantage using the new dictionary merge operator # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging x | y print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = x | y print(id(z)) # 2670466542912 print(z is x) # False print(z is y) # False 2 is having chained expressions following this syntax: # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 0, equivalent to # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 1.

Let us show a practical example:

basic_data = {'id': 1, 'name': 'Vlad'} get_role = {'title': 'Teaching Assistant'} details = {'country': 'Denmark', 'active': True} vlad_info = basic_data | get_role | details print(vlad_info) # {'id': 1, 'name': 'Vlad', 'title': 'Teaching Assistant', 'country': 'Denmark', 'active': True} The Dictionary Update Operator

In the following example, the dictionary x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} 7 is being updated by the dictionary x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} 8, demonstrating the # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 4 method:

x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(x.update(y)) # None print(x) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}

The dictionary x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} 7 was updated, and due to the nature of how the built-in # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 4 method is designed, it operates in-place. The dictionary gets updated but the method returns # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 7.

Using the update operator, # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 8, we can achieve the same functionality with a cleaner syntax:

x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(x |= y) # SyntaxError: invalid syntax print(x) # {'key1': 'value1 from x', 'key2': 'value2 from x'} x |= y print(x) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}

However, compared to the legacy # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 4 method, the new dictionary update operator helps prevent misuse by throwing a from collections import defaultdict user_not_found_message = 'Could not find any user matching the specified user id.' ceo = defaultdict( lambda: user_not_found_message, {'id': 1, 'name': 'Jose', 'title': 'Instructor'} ) author = defaultdict( lambda: user_not_found_message, {'id': 2, 'name': 'Vlad', 'title': 'Teaching Assistant'} ) 0 if we use it inside a print statement, and the dictionary does not get updated. In the next line, following the required syntax the dictionary was updated successfully.

Let's see how updating the dictionary either with the legacy # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 4 method or with the update operator # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 8, does not change the object's id nor it creates a new one:

x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} 0

Another example is extending dictionaries with a list of tuples by using the update operator # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 8:

x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} 1

The example above is syntactic sugar for the legacy # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 4 method:

x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} 2

Besides the better syntax that the new dictionary update operator # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 8 has to offer, another advantage of using it is a safer dictionary update by throwing a from collections import defaultdict user_not_found_message = 'Could not find any user matching the specified user id.' ceo = defaultdict( lambda: user_not_found_message, {'id': 1, 'name': 'Jose', 'title': 'Instructor'} ) author = defaultdict( lambda: user_not_found_message, {'id': 2, 'name': 'Vlad', 'title': 'Teaching Assistant'} ) 0 instead of # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 7 when using it inside from collections import defaultdict user_not_found_message = 'Could not find any user matching the specified user id.' ceo = defaultdict( lambda: user_not_found_message, {'id': 1, 'name': 'Jose', 'title': 'Instructor'} ) author = defaultdict( lambda: user_not_found_message, {'id': 2, 'name': 'Vlad', 'title': 'Teaching Assistant'} ) 8.

Older versions
The dictionary update operator # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 8 and the merge operator # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging x | y print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = x | y print(id(z)) # 2670466542912 print(z is x) # False print(z is y) # False 2 are new features in Python 3.9, so if you are trying to use them in an earlier version you will encounter an error similar to this, so make sure you update to the latest version:

x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} z = x | y print(z) # {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} 3

Summary

The new operators are not here to replace the existing ways of merging and updating,
but rather to complement them. Some of the major takeaways are:

  • the merge operator, # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging x | y print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = x | y print(id(z)) # 2670466542912 print(z is x) # False print(z is y) # False 2, is class aware, offers a better syntax and it creates a new object.
  • the update operator, # before merging x = {"key1": "value1 from x", "key2": "value2 from x"} y = {"key2": "value2 from y", "key3": "value3 from y"} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # after merging {**x, **y} print(id(x)) # 2670466407744 print(id(y)) # 2670466407808 # assigning the expression to the variable `z` z = {**x, **y} print(id(z)) # 2670466553600 print(z is x) # False print(z is y) # False 8, operates in-place, catches common errors before they happen and it doesn't create a new object.
  • the operators are new features in Python 3.9

If you're learning Python and you find this kind of content interesting, be sure to follow us on Twitter or sign up to our mailing list to stay up to date with all out content. There's a form at the bottom of the page if you're interested.

We also just did a big update to our Complete Python Course, so check that out if you're interested in getting to an advanced level in Python. We have a 30 day money back guarantee, so you really have nothing to lose by giving it a try. We'd love to have you!

Postingan terbaru

LIHAT SEMUA