Python has some standards, it need to be written in an idiomatic way called pythonic way. The key characteristics include:
Following PEP 8 guidelines for code formatting and style:
# Non-Pythonic
def myfunction(x,y,z):
result = x+y+z
return result
# Pythonic
def my_function(x, y, z):
result = x + y + z
return result
Please go through all the rules in the provided source
Using the sum() function instead of writing a custom summation loop:
# Non-Pythonic
total = 0
for num in [1, 2, 3, 4, 5]:
total += num
# Pythonic
total = sum([1, 2, 3, 4, 5])
Creating a new list with a list comprehension:
# Non-Pythonic
squares = []
for num in [1, 2, 3, 4, 5]:
squares.append(num ** 2)
# Pythonic
squares = [num ** 2 for num in [1, 2, 3, 4, 5]]