While looking for how to reduce the Python code array, I saw the following example:
arr = [1, 2, 3, 4]
# Not like Python
result = []
for i in range(len(arr)):
if arr[i] % 2 == 1:
result.append(arr[i])
# Python code (using List compression)
result = [num for num in arr if num % 2]
I didn't understand the word Python, but I can see that it can be organized in a single line of code.
But I have a question.
What is the difference between "code not like Python" and "code not like Python" in executing code?And.
Does Python-like code mean that it maximizes Python's work efficiency?
I wonder if it's called Python-like code because it can be made simple like this only in Python.
Unfortunately, I can't find any writing about this, so I'm leaving a question like this because I thought I might be able to find the answer to others.
python
When asked to write pythonic code, it means to write it concisely using Python's features rather than performance problems.
It is recommended that the shorter the cord, the more likely it is to reduce the number of bugs, and it is also helpful for maintenance, so it should be kept concise.
Sometimes you see code where if, for load depth is 4 to 5, but this code, which is less readable and difficult to maintain, is not a good code. These codes are subject to refactoring and should be considered for separation as a function and should be written as concisely as possible.
Simply use the if overlapping phrase, which is one true, as follows.
def one():
print("one")
def two():
print("two")
def three():
print("three")
value = 10
if value > 10:
one()
elif value < 10:
two()
elif value == 10:
three()
three
d = {value > 10:one, value < 10:two, value == 10:three}
d[True]()
three
Take a look at the book called effective python. I was very helpful.
© 2024 OneMinuteCode. All rights reserved.