Knowledgebase

Language Fundamentals: Everything That Matters, Briefly Print

  • python, guide, howto, solution, zillionkinghost, hosting, support
  • 0

The summary.

CHOOSE THE RIGHT COLLECTION

A list for a sequence, a dictionary when you look things up by key, a set when uniqueness matters, a tuple when it must not change.

Sets and dictionaries are far faster than lists for membership checks on large collections.

THE MOST IMPORTANT NUMERIC RULE

Never use ordinary decimal numbers for money. They cannot represent some values exactly.

Use a decimal type designed for exact arithmetic, or work in the smallest unit as whole numbers.

THE HABIT THAT PREVENTS MANY FAILURES

Accessing a dictionary key that may not exist. Use the method returning a default, or check first.

And strip whitespace from text read from files or input before comparing it.

WHAT CATCHES EVERYONE ONCE

Positions start at zero.

A changeable value such as a list used as a default argument is reused across calls.

Modifying a collection while iterating over it.

WRITE SMALL FUNCTIONS THAT DO ONE THING

Pass values in, return results out. Functions depending on hidden external state are hard to test and hard to reason about.

CHECK THE STANDARD LIBRARY BEFORE INSTALLING ANYTHING

It covers far more than most people expect.

Never name your own file after a common library — it will shadow it.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot