Why no operators ++ and — in Python

Python doesn’t have operators ++ and -- like most C-based languages. Instead you need to use += and -=.

x += y
x -= z

These are shortcuts for the following assignments:

x = x + y
x = x - z

The reason is pretty obvious when you think about it – Python variables are immutable, so it would be impossible to have operators that modify them.

Getting the Python version

You can get the Python version with sys.version:

import sys
print(sys.version)
'3.4.3 (default, Oct 14 2015, 20:28:29) \n[GCC 4.8.4]'

However, rather than checking the version it’s much simpler and more Pythonic to try to use a version-dependent function and handle failure if it isn’t there.

Calling a top-level function that doesn’t exist will raise a NameError:

try:
    might_exist()
except NameError as err:
    # Do something else instead

While calling a module-level function that doesn’t exist will raise an AttributeError

try:
    maybe_module.might_exist()
except AttributeError as err:
    pass
    # Do something else instead

Counting the items in a list in Python

To count the number of a single item in a list, use the count() method:

from random import randint

dishes = ['spam', 'eggs', 'ham']
# Fill a list with 100 randomly selected dishes
orders = [dishes[randint(0, 2)] for i in range(1, 101)]
# Print the count of spam orders
print("spam: {0}".format(orders.count('spam')))
spam: 31

To get the counts of all items, use a collections.Counter

from collections import Counter

counter = Counter(orders)
print(counter)
Counter({'ham': 37, 'eggs': 32, 'spam': 31})

Difference between lists and tuples in Python

Lists and tuples in Python are both sequence types, and share a number of operations common to all sequence types:

  • in
  • +
  • *
  • slices
  • len()
  • min()
  • max()
  • index()
  • count()

However they are quite different in purpose and they way they are most commonly used:

  • Lists are mutable, and are generally used to store homogeneous objects (objects of the same type), which are accessed by iteration
  • Tuples are immutable, and are generally used to store heterogeneous objects, which are accessed by unpacking or indexing

This makes tuples more like lightweight objects, which is how they are often used. The namedtuple type takes this further by allowing indexing by named attributes.

Environment variables in Python

To get an variable, retrieve if from os.environ

    print(os.environ['HOME'])

It’s just like a dictionary, so you can print all of the entries:

    print(os.environ)

Just like a dictionary, the get() method will return None if the requested key isn’t present, so it never raises an KeyError, and can take a default value:

    print(os.environ.get('SPAM', 'EGGS'))

The os.getenv()` function behaves exactly the same way:

    print(os.getenv('SPAM', 'EGGS'))