ANI

Built-in-Ins-in-Ins that seemed to be a joke (until you used it)

Built-in-Ins-in-Ins that seemed to be a joke (until you used it)
Photo for Editor | Chatgt

Obvious Introduction

Working with Python means relying on many built-in activities, especially data science activities. Popular activities such as len, max, rangeEtc., they are common in scientific and useful tool in various cases. However, many built-in-built-in-built-in-law remain unknown because it is seen as worthless.

In this article, we will examine 7 different-built 7 to think that a joke but it is actually very useful for their applications. These ins differentiation is different from your standard code, but will find their place in your work travel when you see their help.

Want to know? Let's get into it.

Obvious 1. The divmod Built-in-built work

Most people rarely use the divmod Built-in-built work, or knows that there is. This page divmod Building work restores two numbers: The result of the bottom of the floor and the performance of the Modulus. Can seem to be unemployed as we can use a syntax like a // b including a % b Without the need is built within the built-in, especially when we often need both results at the same time.

In cases of real world use, we often need both and consistently and consistently to make the process immediately. Few divmod Applications – including the conversion of time, fluctuations, monitoring, and the Hash hashed Math – Show its use.

Let's look at an example of using:

total_seconds = 7132
minutes, seconds = divmod(total_seconds, 60)
hours, minutes = divmod(minutes, 60)
print(f"Time: {hours:02d}:{minutes:02d}:{seconds:02d}")

When the release is displayed below:

For one work, we can separate the numbers equally, useful for many applications.

Obvious 2. The slice Built-in-built work

This page slice Building work is used to distinguish or remove parts of sequence as a string, list, or tuples. Can seem unwanted because we just caused the slide thing like obj[1:10:2].

However, the power of slice Built-in-built work appears when you need to re-exercise the same law of the same light on different things. The work helps us to save the changing logic and allow for data processing strategies.

Here is an example of implementation:

evens = slice(0, None, 2)
text = "abcdefghij"
print(text[evens])

Release is displayed below:

This page slice In addition to the built-in is to enable the unreliable law to take all the second character.

Obvious 3. The iter Built-in-built work

This page iter Built-in work creates an item of Itemator that processes items in order, each one. This can seem unnecessary as we already have `when it's the factory` of a pattern” breaking the law. However, it is useful as allowing a clean code and a better structure on the data pipeline.

For example, we can use iter Controlling the data processing:

import io
f = io.BytesIO(b"12345678")

for block in iter(lambda: f.read(3), b""):
    print(block)

When the release is displayed below:

Obvious 4. The memoryview Built-in-built work

This page memoryview The built-in work creates a memory view from the internal information without copying. It can be seen as an unnecessary work that has not space in the ordinary Python Workflow travel.

However, memoryview The job is useful when managing large binary data because it allows users to access and change them without creating something new. For example, you can replace the data part in the same memory without copying, as shown in the following code.

buf = bytearray(b"hello")
mv = memoryview(buf)
mv[0] = ord('H')
print(buf)
mv[:] = b"HELLO"
print(buf)

Release is displayed below:

bytearray(b'Hello')
bytearray(b'HELLO')

The allocation is made from the same memory, useful for any sensitive function.

Obvious 5. The any Built-in-built work

This page any The built-in work returns the boolean value by looking at things on material. Retreat True If there is any unsure thing true and False otherwise. The work seems to be unemployed, as it seems to replace the simple test.

However, the power of any The work is lying in its ability to avoid temporary creation and use a lazy tests with manufacturers, which means the work can have a clear loop logic and reduce the use of memory.

An example of the Python code is shown below:

files = ["report.txt", "sales.csv", "notes.md"]
print(any(f.endswith(".csv") for f in files))

When the release is displayed below:

It is useful when using the charges that require verification or monitoring conditions.

Obvious 6. The all Built-in-built work

This page all Built-in-built work is the same anyBut the difference was that it was a pre-refund True if all The things in the penalty are true. Similar to anythe all Built-in work is often seen as an indirect assistance as craft loop can reach the same effect.

It is an important built-in because it provides fun and short Confirmation of all the objects being strengthened by Lazy test. This means that the code is clean and takes a small memory.

The use of an example is used for the following code:

required = ["id", "name", "email"]
record = {"id": 1, "name": "Ana", "email": "[email protected]"}
print(all(k in record for k in required))

When the release is displayed below:

Applications include Schema checks and installation verification.

Obvious 7. The zip Built-in-built work

This page zip The built-in function is used to integrate objects from many things that cannot be reached in the structures. May seem unnecessary, as an engineer can only come in with indices, such as for i in range(len(x)): loop, or because you only return to tuples.

This page zip Built-in-built work, however, you help a lot because it allows you to import many things that cannot work without juggling indices. As the work creates only pairs where necessary, it will not waste memory. It also avoids any common indicator errors where we handle the index.

An example of Python is shown below:

keys = ["id", "name", "email"]
vals = [1, "Ana", "[email protected]"]
record = dict(zip(keys, vals))
print(record)

When the release is displayed below:

{'id': 1, 'name': 'Ana', 'email': '[email protected]'}

Many applications are available, such as the similarities of datasets or tupper complains without the index arithmetic.

Obvious Store

Python in a good language of applicable programs for any function related to the data, and its built-in tasks is an important part of that state. However, some built-in-built-in-built-in-built-in-built-in-work – that is, until you really use them and realize how important they are.

I hope this has helped!

Cornellius Yudha Wijaya It is a scientific science manager and the database author. While working full-time in Allianz Indonesia, she likes to share the python and data advice with social media and media writing. Cornellius writes to a variety of AI and a study machine.

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button