Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the Python function

Shulou Source: shulou.com Published: 2022-06-02 20:52:06 09月18日 Update

This article mainly explains "how to use Python function". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Python function".

Variable assignment

Just like the functions * args and * * kwargs, you can use the same syntax in variable assignments:

Merge two dictionaries

Use the * * kwargs syntax in the iterable variable assignment method when merging dictionaries:

Be careful that if there is a common key between dictionaries, the latter key-value pair (in y) will replace the former. According to the latest Python release (3.9), a whole new syntax can be used to avoid this problem, that is, dictionary merging and updating operators:

Z = x | y # merge-behavior pattern described above

X | = y # Update-replace dictionary merge

Immutable set

In Python, you can use collections, that is, unordered collections of different objects. These sets are mutable, meaning that transformations can be done with add () and remove ()-- which means that variable sets are not hashable.

Instead, you can use the immutable set frozenset (), a set that cannot change its value. But because it's immutable, it's hashable-- this is true when you use set and frozenset as dictionary keys at the same time:

Using frozenset as a dictionary key does not seem practical, but the role of frozenset is to provide more detailed and purposeful code. This reminds future code readers that if you change me, everything will fall apart.

Multi-factor condition

Clean up those messy if statements instead of:

If 0

Tags: Function dictionary variable syntax variable check function one line code statement simultaneous learning two function global content situation more condition scope Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Shulou Tech Info Huawei Redmi