Notes on Python

Note: Some of the points made below are specific to Python 3

  • Math:
    • Exponentiation (2 ** 3 == 8)
    • 35 / 5 = 7.0 (division runs float by default)
    • 7.0 / 2 = 3.5
    • 7 / 2 = 3.5
    • 7 // 2 = 3 (takes the floor of the integer)
  • Equality:
    • Just "=="; no "===" (will throw an error)
  • Basic functions:
    • # single line comment
    • print() - equivalent to console.log
    • int() - typecast into integer
      • note: doesn't work like parseInt - will throw an error if you try to do something like int("hi5")
    • str() - typecast into a string (even works on lists)
    • list() - typecast into a list
    • range() - gives you a list from 0 up to but not including the number passed in
      • range(4,8) # [4,5,6,7]
      • range(3) # [0, 1, 2]
  • Booleans are True or False (must be capitalized)
    • None, 0, empty list / dicts / string are False. All others are True.
  • None is an object (is this like a null object?)
  • Strings are immutable; you cannot do implicit conversion between string and integers (unlike Javascript which does type coercion)
  • Lists are mutable; Tuples are immutable and take less space in memory
    • append() equivalent to push() in JS array
    • pop() removes and returns the last element 
    • a_list_of_numbers.sort(Reverse=True) (sort in memory)
    • sorted(a_list_of_numbers) - returns a new sorted list
    • Concise syntax for doing slices [start:end:step]
      • a_list[::] - shorthand for copying a list
    • You can do destructuring (aka unpacking) for list and tuples
    • list.extend() li.extend(li2) (adds elements of the second list into the first list)
  • Dictionaries are key-value in-memory stores (similar to Plain Old Javascript Object POJO)
    • Note: Unlike Javascript, there is no implicit string conversion on the keys which means you can use variables for the key
    • dict1.update() adds key-value pairs from the second dict to the first dict
  • Remove elements from list or dicts with del keyword (e.g. del li[2])
  • Functions have positional (which one came first) and keyword (what name is assigned to the argument) arguments 
    • Example of keyword arguments:
      • def func1(name, age):
      • func1(age=24, name="will")
    • def varArgs(*args):
    • def kwArgs(**kwargs):
    • def bothArgs(*args, **kwargs) (I believe the positional args globber must come before the keyword args globber)
    • Automatically uses local scope in function. To refer to global scope, must use global keyword
    • Lambdas / anonymous functions:
      • (lambda x: x > 2)
  • List comprehension is a way to create a list through another list
  • Module system: Module is a Python file (you don't need to specify the .py file extension)
    • Syntax for importing
      • import random - for importing an entire module
      • from math import ceil, floor - import specific functions from a module
      • import math as m - alias an import
  • Template string is """ and """
    • Allows you to do multi-line strings
    • Sometimes used for commenting
  • Conditionals:
    • if x > 10:
    • elif x > 0:
    • else:
  • For loop:
    • for dog in ["alfred","beagle"]:
      • print(dog)
  • Being Pythonic
    • Use snake_case for identifiers
    • Check for existence in list: (e.g. 3 in integer_list)


Sources:

  • Introducing Python: Modern Computing in Simple Packages
  • TDD Python
  • Django docs https://docs.djangoproject.com/en/1.8/intro/overview/
views