logo

Python - Overview

Last Updated: 2024-01-20

Why Python

  • Easy to learn.
  • Available everywhere: Linux, macOS, Windows.
  • Super popular in machine learning / data science, competing with R.
  • help() gets docstring for any module / method / function.
  • Simple, compact and powerful.
    • list comprehensions.
    • class magic methods.
    • very powerful OOP.
    • built-in strings and array slicing.
    • huge and coherent standard library.

Ecosystem

Python specific features

Swap

a, b = b, a

If You Forget Function Name

Use dir() to check the function names:

>>> dir([])
[... 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Or pretty print:

>>> import pprint; pprint.pprint(dir([]))
[ ...
  'append',
  'clear',
  'copy',
  'count',
  'extend',
  'index',
  'insert',
  'pop',
  'remove',
  'reverse',
  'sort']

All and ANY

ALL: if every one is truthy

>>> all([1, "abc", [False]])
True
>>> all([1, 0, 1])
False

ANY: if any one is truthy

>>> any([1, 0, 0])
True
>>> any(["", 0, 0])
False

Resources