Close

2022-05-04

Zen of Python

Zen of Python

The Zen of Python is a collection of 19 “guiding principles” for writing computer programs that influence the design of the Python programming language. Software engineer Tim Peters wrote this set of principles and posted it on the Python mailing list in 1999.

Peters’s list left a 20th principle “for Guido to fill in,” referring to Guido van Rossum, the original author of the Python language. The vacancy for a 20th principal has not been filled.

The principles are listed as follows:

  1. Beautiful is better than ugly.
  2. Explicit is better than implicit.
  3. Simple is better than complex.
  4. The complex is better than the complicated.
  5. The flat is better than nested.
  6. Sparse is better than dense.
  7. Readability counts.
  8. Exceptional cases aren’t special enough to break the rules.
  9. Although practicality beats purity.
  10. Errors should never pass silently.
  11. Unless explicitly silenced.
  12. In the face of ambiguity, refuse the temptation to guess.
  13. There should be one– and preferably only one –obvious way to do it.
  14. Although that way may not be obvious at first unless you’re Dutch.
  15. Now is better than never.
  16. Although never is often better than right now.
  17. If the implementation is hard to explain, it’s a bad idea.
  18. It may be a good idea if the implementation is easy to explain.
  19. Namespaces are one honking great idea – let’s do more of those!

Code Samples

Beautiful is better than ugly.

This principle emphasizes the importance of code that is easy to read and understand. Beautiful code is more likely to be maintainable and reusable.

# This code is beautiful because it is easy to read and understand.

def factorial(n):
  """
  Calculates the factorial of a number.

  Args:
    n: The number to calculate the factorial of.

  Returns:
    The factorial of n.
  """
  if n == 0:
    return 1
  else:
    return n * factorial(n - 1)

Explicit is better than implicit.

This principle emphasizes the importance of using clear and concise language in code. Explicit code is more likely to be correct and easier to debug.

# This code is explicit because it uses clear and concise language.

def is_prime(n):
  """
  Returns True if n is a prime number, False otherwise.

  Args:
    n: The number to check.

  Returns:
    True if n is a prime number, False otherwise.
  """
  for i in range(2, n):
    if n % i == 0:
      return False
  return True

Simple is better than complex.

This principle emphasizes the importance of keeping code simple and straightforward. Complex code is more difficult to understand, maintain, and debug.

# This code is simple because it is straightforward and easy to understand.

def reverse_string(s):
  """
  Reverses a string.

  Args:
    s: The string to reverse.

  Returns:
    The reversed string.
  """
  return s[::-1]

The complex is better than the complicated.

This principle states that complex code can be better than complicated code. Complex code is code that is designed to solve a specific problem. Complicated code is code that is difficult to understand and maintain.

# This code is complex because it is designed to solve a specific problem.

def find_largest_number(numbers):
  """
  Finds the largest number in a list of numbers.

  Args:
    numbers: The list of numbers to search.

  Returns:
    The largest number in the list.
  """
  largest_number = numbers[0]
  for number in numbers:
    if number > largest_number:
      largest_number = number
  return largest_number

The flat is better than nested.

This principle emphasizes the importance of using flat code structures. Flat code is easier to read and understand than nested code.

# This code is flat because it does not use nested structures.

def calculate_average(numbers):
  """
  Calculates the average of a list of numbers.

  Args:
    numbers: The list of numbers to calculate the average of.

  Returns:
    The average of the numbers in the list.
  """
  total = 0
  for number in numbers:
    total += number
  return total / len(numbers)

Readability counts.

This principle emphasizes the importance of writing code that is easy to read. Readable code is more likely to be understood and maintained.

# This code is readable because it uses clear and concise language.

def print_hello_world():
  """
  Prints "Hello, world!" to the console.
  """
  print("Hello, world!")
https://ozgurozkok.com/why-tensorflow-for-python-is-dying-a-slow-death/