Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python 101 Day 2

Anna
November 18, 2017
44

Python 101 Day 2

Anna

November 18, 2017
Tweet

Transcript

  1. What is Python? ▪ Interpreted Language – don't have to

    compile it ▪ Multi-paradigm: OOP (no access specifiers), Functional and others... ▪ Guido van Rossum ("Benevolent Dictator For Life") ▪ Dynamic typing – Don't have to specify data types for variables ▪ Pythonic code and Zen of Python ▪ Cython, PyPy ▪ Indentation ▪ Built in garbage collector
  2. Built in types ▪ Immutable built-in types of Python –

    Numbers – Strings – Tuples ▪ Mutable built-in types of Python – List – Dictionaries – Sets
  3. Assignments = ▪ x = 2 ▪ C/C++: "typed variable

    name x receives a copy of numeric value 2". The (right-hand) value is copied into an allocated storage location for which the (left-hand) variable name is the symbolic address. ▪ Python: "(generic) name x receives a reference to a separate, dynamically allocated object of numeric (int) type of value 2."
  4. Let's do some maths! ▪ // vs / ▪ **

    ▪ Results of a division is always a float ▪ 1 is True 0 is False ▪ Is 2 True?
  5. If else ▪ x if c else y ▪ elif

    ▪ Pass ▪ No switches
  6. Lists ▪ append, pop, remove, insert, index, extend ▪ Slicing

    ▪ :: ▪ list[start:end:step] ▪ join ▪ List comprehensions ▪ [:] deep copy ▪ in
  7. for ▪ Calls iter() on object ▪ range – values

    on demand for x in range(0, 10, 1): print(x)
  8. Any and All ▪ Any returns True if any one

    item in the iterable is True ▪ All returns True if all the items in the iterable is True
  9. We do have arrays... ▪ Array ▪ Have to be

    of a specific type ▪ https://www.python.org/doc/essays/list2str/
  10. Dictionary ▪ What keys are hashable? ▪ get(), setdefault() ▪

    Dict comprehensions ▪ {x: x+2 for x in range(1,100)}
  11. def ▪ Functions are first class objects – I can

    assign them to variables ▪ Pass by value vs pass by reference??? ▪ *args – Variable number of args ▪ **kwargs – Variable number of keyword args
  12. Magic methods ▪ Overloaded operators in C++ ▪ What if

    I want to compare two objects with < and > ? ▪ object.__lt__(self, other)
  13. Maps and filters ▪ map(function, data) ▪ filter(function, data) ▪

    Reduce - from functools import reduce – You need two args in the lambda
  14. Try except ▪ Else – Runs only if no exceptions

    were raised ▪ Finally – Run everytime ▪ raise
  15. Sets ▪ Elements of set have to be immutable ▪

    No order ▪ & | - >= <= ▪ {x for x in range(1,100)}
  16. class ▪ First class objects ▪ Self - myobject.method(arg1, arg2)

    => MyClass.method(myobject, arg1, arg2) ▪ Hidden variables – Not really hidden ▪ Supports Multiple inheritance ▪ Isinstance ▪ issubclass