Python Essentials: What Programmers Coming From Other Languages Need to Know

Welcome to the world of Python! If you’re transitioning from another programming language or if you’ve been dabbling in various languages and decided to give Python a deeper look, there are some essential quirks and features you should be aware of. Python, often lauded for its readability and elegance, does have its unique characteristics. In this post, we’ll walk through a curated list of Python-specific features and conventions to give you a head start.

Why this List?
Every programming language has its idiosyncrasies and conventions. When switching from one language to another, understanding these can significantly reduce your learning curve and potential frustrations. In Python, some things might seem unusual, especially if you come from a C-style language or a statically-typed background. This list aims to bridge that gap.

  1. Indentation Matters: Python uses whitespace (indentation) to denote blocks of code. Unlike languages that use braces ({}) for this purpose, correct indentation in Python is not just a matter of style but is syntactically essential.
  2. Dynamic Typing: While Python 3.5 introduced optional type hints, Python is dynamically typed. This means that you don’t have to declare a variable’s type in advance.
  3. Immutable Strings: Strings in Python are immutable. If you try to reassign a value within a string, it’ll throw an error!
  4. List Comprehensions: Python has a concise way to create lists with a single line of code, called List Comprehensions. This is an incredibly powerful feature worth diving into.
  5. Colon in Control Structures: Function definitions and structures like if, while, and for are ended with a colon (:).
  6. “self” in Class Methods: Instance methods in classes need to have self as their first parameter, which refers to the object being instantiated.
  7. Underscores:
    • Single Leading Underscore: Conventionally used to denote a weak “internal use” variable or method. It’s treated as private by convention.
    • Double Leading Underscore: This mangles the attribute names, making it harder (but not impossible) to access the attribute from outside the class.
    • Double Leading and Trailing Underscore: These are “magic” or “dunder” methods in Python (e.g., __init__, __str__). They have special meanings.
  8. Naming Conventions:
    • Variables and function/method names are typically snake_case (e.g., my_function).
    • Class names use CamelCase (e.g., MyClass).
    • Constants are typically in UPPER_SNAKE_CASE.
  9. Multiple Ways to Format Strings: Python provides several ways to format strings, including %-formatting, str.format(), f-strings (from Python 3.6), and more.
  10. Everything is an Object: In Python, everything is an object, including numbers, strings, functions, classes, and even types.
  11. Slicing: Lists, strings, and other iterable data types can be sliced, which is a way to extract a portion of these types.
  12. “is” vs “==”: is checks for identity (i.e., both variables point to the same memory location), while == checks for equality (i.e., both variables have the same value).
  13. Global Interpreter Lock (GIL): The CPython implementation (standard Python implementation) has something called the GIL, which means that even on multi-threaded code, only one operation executes at a time in the interpreter. This is often a surprise for developers coming from other languages.
  14. Multiple Implementations: While CPython is the standard and most widely used implementation, there are others like Jython (for Java platform), IronPython (for .NET), PyPy (just-in-time compiler), etc.
  15. Extensive Standard Library: Python comes “batteries included,” meaning it has a rich standard library that provides a broad range of facilities as part of its standard distribution.