Python Flashcards

1
Q

Companies which use Python at sacle

A

Youtube, Dropbox

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Frameworks

A

Web applications and API Design
Django, Flask, Pyramid

Configuration
Ansible, Boto for AWS

Data Analysis
Pandas, MatplotLib, Bokeh, Tensoflow, SciKitLearn

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Language Features

A
  • strongly typed - every object has a definite type
  • dynamically typed - no type checking of code prior to running it
  • uses duck-typing - suitability of an object is decided at run time
    (Java is statically typed - compiler does typechecking)
  • interpreted language
  • syntax - clear readable and expressive
  • used whitespace to distinguish code blocks so reduced all the parentheses used by other languages

There are multiple interpretations of Python

  • CPython (written in C)
  • Jython - targets JVM
  • IronPython - .NET
  • PyPy - R

Versions
2/3

Batteries Included
- large amount of libraries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Zen Of Python

A

> > > import this

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let’s do more of those!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Python REPL

A

Read Eval Print Loop
4 spaces for indentation
all constructs end with colon :

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

PEP

A

Python Enhancement Proposal

PEP 8 - style guide for python
PEP 20 - Zen of Python

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Get help for any module

A

> > > help(module_name)
help(math)
prints all functions and descriptions

> > > help(math.factorial)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Import Modules

A

this will import full library into the current namespace
»> import math
»> math.factorial(10)

this will import only one function into current namespace
»> from math import factorial
»> factorial(10)

> > > from math import factorial as f

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Built In Data Types

A

Scalar types - int, float, None, bool

int

  • has unlimited precision
  • binary => 0b10 == 2
  • hex => 0x10 == 16
  • convert string to int => int(“300”) == 300

float

  • 3e8 == 300000000
  • convert integer to float => float(7) == 7.0, float(“1.23”) == 1.23

None
- no value

bool

  • only 0 integer is false rest all are true
  • only 0.0 float is false rest all are true
  • only empty string i.e. “” is false
  • only empty list i.e. [] is false

bool([]) - False
bool([1, 2, 3]) - True
bool(“”) - False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Control Flow Structures

A

Breaking a while loop

if expression:
print(“”)
else:
print(“”)

while exp:
print()

while expression:
if expressions:
break;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

String Datatype (str)

A
  • IMMUTABLE sequence of unicode chars
  • string literals (not objects) can use single or double quotes
  • multiline string use three quotes i.e. “”” fssfss “””
  • create strings from other type use str() constructor i.e. str(10.7) => “10.7”
  • help(str) -> gives all methods
  • strings are sequence types i.e. they support certain interface operations i.e. indexing,
c = 'oslo'
c1 = c.capitalize()

c was not modified but instead anew string was created. Because strings are immutable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Bytes

A
  • IMMUTABLE sequences of Bytes
  • default encoding is UTF-8
  • need to prefix them with lowercase b
    i. e. b’hello’ or b”hello”

split returns a list of byte objects
»> split(b’hello world’)
»> [b’hello’, b’world’]

**** very important ***
encoding - converts strings to bytes
decoding - converts bytes to string

all http response, files etc are transmitted as bytes but we choose to work in string with UTF-8 encoding

nor = “some norwegian chars”
data = nor.encode(“UTF-8”)
»> data
»> b’norwegian chars’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Lists

A
  • MUTABLE sequences of objects
  • a = [1, 2, 3]
  • append()

list constructor - used to create list from other objects
»> list(“hello”)
»> [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Dictionary

A
  • MUTABLE map of key to value
  • d = {“alice”:”123”, “bob”: “456”}
  • retrieve value using key i.e. d[“alice”]
  • adding new value, d[‘jina’] = ‘487’
  • can update values on the fly
  • d[“alice”] = “000”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

For Loop

A

cities = [“London”, “Paris”, “Berlin”]
for city in cities:
print(city)

countries = {“usa”: “washington”, “england”: “london”}
for country in countries:
print(capitals[country])

for country, capital in countries:
print(country, capital)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Decoding and Encoding

A

names = [b”alice”, b”bob”, b”cindy”]
for name in names:
print(name.decode(“UTF-8”))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Functions

A
  • special attributes are delimited by double underscores
  • functions args are pass by object reference not pass by value of the object

__name__

  • evals to __main__ if we execute python3 filename.py
  • evals to actual module name if imported

from hello import (foo, bar) => import only certain funcs
from hello import * => import everything

  • Doc String

def foo(arg):
“”” This is a function
Args:
bar: this is bar

Returns:
A message
“””
return “foo”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Command Line args

A
## hello.py
import sys

if __name__ == “__main__”:
print(sys.argv[0], sys.argv[1])

python3 hello.py “foo”

> > > hello, foo

argv[0] - filename

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

id() function

A

gives back id of an object i.e. 10 in this case and not the id of reference i.e. i

there is object and . object reference
i.e. i = 10
i is object reference and 10 is the integer object
====> named references to objects

i = 10
id(i) => 244224355343
i += 10
id(i) => 5345452345342

Python does not have variables per se. It only has named references to object - which allows us to retrieve objects

value equality and identity are different
- a variable can have the same value as another variable but is not necessarily the same object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Python objects

A
p = [1, 2, 3]
q = [1, 2, 3]

p == q => T
p is q => F

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How to prove function args are pass by object reference?

A
def foo(k):
  return k
a = [10, 20, 30]
b = foo(a)

a is b => T
a == b => T

####################

def modify(a):
    a.append(100)
b = [1, 2, 3]
modify(b)
>>>a
>>> [1, 2, 3, 100]
>>>b
>>>[1, 2, 3, 100]
both have been modified since its pass by reference

now same code with a twist

#######################

def modify(a):
    a = [100, 200, 300]
b = [1, 2, 3]
modify(b)
>>>a
>>> [100, 200, 300]
>>>b
>>>[1, 2, 3]

here the value of a is assigned to another list hence the original list is not modified

22
Q

Default args

A
  • need to be after required args
  • always use immutable objects for default arguments
def foo(arg1, arg2="bar"):
  print(arg1, arg2)

arg1 - positional arg
arg2 = keyword argument

foo(“hello”)
foo(“hello”, world)
foo(“hello”, arg2=”world”)

all keyword args must be specified after positional args

23
Q

Default value trap

A
def add_eggs(arg1=[]):
  arg1.append("eggs")
  return arg1

foo(“rice”) => [“rice”, “eggs”]
foo(“veg”) > [“veg”, “eggs”]

foo() => [“eggs”]
foo() => [“eggs”, “eggs”]

## right way
def add_eggs(arg1=None):
  if arg1 == None:
    arg1 = []
  arg1.append("egg")
  return arg1

foo() => [“eggs”]
foo() => [“eggs”]

24
Q

Python Name Scopes

A

foo.py

i = 10 # where is i stored?
scopes are contexts in which named references can be looked up

LEGB
local scope - functions
enclosing
global - module - import statement, functions, global vars
buitin 

module scopes

  • import statements
  • function names

count = 0

def show():
   print(count)
def increment(c):
   count = c
>>> show()
>>> 0
>>> increment()
>>> show()
>>> 0
This is unexpected. But we have created a new variable called count inside of the increment() which shadows the global variable
For this to work properly we need to use golbal
i.e. 
def increment(c):
  global count
  count = c
25
Scoping Issues
count = 0 ``` def get_count(): return count ``` ``` def set_count(c): count = c ``` ``` >>> get_count() >>>0 >>> set_count() >>> get_count() >>>0 . # because the count defined in function is a totally new variable ``` ``` we can change it to behave like what we want def set_count(c): global count count = c ```
26
How to explore a variable
type() - displays type of the object dir() => gives all attributes of a module __name__ => gives names of an object __doc__ => gives doc string of an object ``` ## hello.py say_hello(): print('hello') ``` ``` >>> import hello >>> type(hello) >>> >>> dir(hello) >>> ["say_hello"] >>> type(hello.say_hello) >>> ```
27
Tuples
- heterogeneous immutable sequence - when created the objects cannot be removed and no new elements can be added t = (1, 2, 3, 4) t[0] = 1 t + (4, 5) >>> (1, 2, 3, 4, 5,) unpacking tuple >>>a, b = (1, 2, 3, 4, 5) a = 1, b=2 empty tuple t = () single element tuple t = (10, )
28
Strings
''.join("hello", "world") => helloworld ';'.join("hello", "world") => "hello;world" "hello-world".split("-") greeting, separator, user = "hello:bob".partition(':') "hello {0}".format("bob") format == "hello {fname} {lname}".format(fname=foo, lname=bar) import math "pi={m.pi}, e={m.e}".format(m=math)
29
Range
- represents sequence of arithmetic progression range(end) range(start, end) range(start, end, step) this produces a lazy sequence, so to realize it we need to use list i.e. list(range(100))
30
Enumerate
t = [10, 20, 30, 40] for item in enumerate(t): print(item) (0, 10) (1, 20) (2, 30) (3, 40) for index, val in enumerate(t): print("index={0}, value={1}".format(index, val))
31
List
0 1 2 3 4 names = ["zero", "one", "two", "three", "four"] -5 -4 -3 -2 -1 names = ["zero", "one", "two", "three", "four"] names[0] => zero names[-1] => four slicing operation names[1:4] => one, two, three names[:4] => zero, one, two, three names[1:-1] => one, two, three ``` #copy list new_names = names[:] newer_names = list(names) newest_names = names.copy() ``` new_names is names => F (not same objects) new_names == names => T (same values) checking membership "zero" in names => T "zero" not in names => F ``` #counting names.count("zero") => 1 ``` ``` # getting position of an element names.index("zero") => 0 ``` ``` # remove by position del names[1] => removed one ``` ``` # remove by value names.remove(1) => removes on2 ``` names = ["zero", "one", "two", "three", "four"] #insert elements names.insert(2, "halooo") => zero, one, haloo, two, three, four ``` # convert list to string ' '.join(names) ``` sorting (in place) names. sort() names. sort(reversed=True) => sorting reverse names. sort(key=len) => sorting by length ``` # sorting without modifying current list sorted_list = sorted(names) => returns new list ``` ``` # reversing in place names.reverse() ``` ``` # reversing without modifying the source list reversed_names = reversed(names) => returns an iterator reversed_name = list(reversed(names)) ```
32
Dictionary
keys must be immutable => string, tuples, numbers ``` capitals = {"usa": "washington dc", "spain": "madrid"} data = [("usa", "washington"), ("spain", "madrid)] capitals = dict(data) => dictionary ``` ``` # coping dict (always makes a shallow copy) new_capitals = dict(capitals) ``` ``` #extend dictionary more_capitals = {"france": "paris"} capitals.extend(more_capitals) ``` ``` # iterate though dict for key in capitals: print("{key} {value}".format(key=key, value=capitals[key])) ``` ``` # iterate over values for value in capitals.values(): ``` ``` # iterate for keys for key in capitals.keys(): ``` ``` # iterate over both keys and values for key, value in capitals.items(): ``` ``` # membership test only work on keys "paris" in capitals => true ```
33
Pretty Printing
from pprint import pprint as pp | pp(capitals)
34
Set
- collection of unique immutable elements ``` primes = {1, 2, 3, 5, 7, 11} foo = set() => creates empty set (cannot use foo = {} => it will create a dict) ``` ``` # add primes.add(13) ``` ``` # update primes.update([17, 19]) ``` ``` # remove primes.remove(17) # produces error if element not present ``` ``` # discard primes.discard(17) ``` ``` # set operations union, intersection, difference, issubset, isdisjoint ```
35
Python Standard Protocols Container - str, list, range, tuple, byte, set, dict Sized - str, list, range, tuple, byte, set, dict Iterable - str, list, range, tuple, byte, set, dict Sequence - str, list, range, tuple, byte Mutable Sequence - list Mutable Set - set Mutable Mapping - dict
Protocol is a set of methods that the type must support if it implements this protocol. Protocols need not be defined in a separate class like Java - just the type needs to have a function with the same signature. Container - in, not in Sized - len() Iterable - iter() Sequence - items should be abled to retrieved with [] and integer index i.e. foo[10]. - Items needs to be searched for using foo.index('bar') - foo.count() - reversed(foo) Mutable Sequence Mutable Set Mutable Mapping
36
Exception Handling
``` def divide(i, j): try: return i/j except(ValueError, TypeError) as e: return -1 finally: pass ``` ``` def divide(i, j): try: return i/j except(ValueError, TypeError) as e: raise finally: pass ```
37
Common Exception Types
IndexError - index value out of range a = [1, 2, 3] a[10] # raises exception ValueError - invalid value but right type - object is of right type but contains in appropriate value int("wer") Key Error - lookup in a mapping fails Better not to catch type errors
38
List Comprehension Comprehensions are python syntax to concisely describe list, sets in a declarative or functional way Comprehensions should be purely functional and it should not have any side effects i.e. printing etc. For side effects use for loops instead.
lists ===> [ expression(item) for item in iterable if predicate(item) ] sets ===> { expression(item) for item in iterable } dictionary ===> {key: expression(item) for item in iterable } s = ["hello", "world"] ``` ## Declarative s_len = [len(item) for item in items] => list s_len = {len(item) for item in items} => Set ``` Imperative s_len = [] for item in items: s_len.append(len(item))
39
Dict Comprehension
country_capital = {"spain": "madrid", "france": "paris"} capital_country = {val: key for key, val in country_capital.items()}
40
Filtering in Comprehensions
``` allowed_ids = [10, 20, 30] check_ids = [100, 200, 10, 20, 300] ``` white_list = [item for item in items if item in allowed_ips]
41
Iterator and Iterable
Iterable object can be passed to the built in function called iter() which will return an iterator object iterator = iter(iterable) Iterator This object should be able to be passed to build in next() function to get the next element item = next(iterator) Example: ``` iterable = ["Sunday", "Monday", "Tuesday"] iterator = iter(iterable) item1 = next(iterator) => Monday item2 = next(iterator) => Tuesday item3 = next(iterator) => Wednesday ```
42
Generators
- Generators are Iterators - resumes execution from the last called position - maintains state - lazy eval - used to model infinite sequences - since values are produced only when needed ``` def gen123(): yield 1 yield 2 yield 3 ``` g = gen123() next(g) => 1 next(g) => 2 next(g) => 3 ``` ####### def gen_foo(): a = [1, 2, 3, 4, 5] for item in a: yield item ``` ``` foo = gen_foo() next(foo) => 1 next(foo) => 2 next(foo) => 3 next(foo) => 4 ``` OR for item in gen_foo(): print(item) OR g = (item for item in gen_foo()) # note we are using () and not [] g is a generator next(g) => 1
43
Generator Comprehension
squares = (x*x for x in range(0, 100)) ^ is a generator next(squares) ``` ## forced execution list(squares) ```
44
Classes
``` ## hello.py Class Greeting: def __init__(self): pass ``` ``` def say_hello(self): return "Hello" ``` ``` >>> from hello import Greeting g = Greeting() g.say_hello() ## no need to pass any arg ## default call => Greeting.say_hello(g) # never used ``` __init__ is not a constructor but a initializer Properties - Polymorphism - Inheritance
45
Inheritance
``` class Person: def __init__(self, name, age): self.name = name self.age = age ``` ``` class Warrior(Person): def __init__(self, name, age): super.__init__(name, age) ``` ``` def use_sword(self): print('using sword') ``` ``` warrior1 = Warrior("foo", 21) warrior2 = Warrior("bar", 22) ```
46
Files
``` f = open("hello.txt", mode="wt", encoding="UTF-8") f.write("hello world...") f.close() modes=read, write, append file type=binary, text ``` f. read() f. readlne() f. seek(0) => goto beginning of the file f. write() f. writelines([]) import sys sys.getdefaultencoding()
47
With Blocks
- helps to cleanup resources - can be used with any object which exposed a context manager protocol ``` with open("hello.txt", "rt") as f: f.read() ``` no need to call close
48
Unit Testing
- unittest module - test fixtures - runs before and after the tests - setup and teardown - assertions import unittest ``` class MyTester(unittest.TestCase): def test_hello_world(self): print("helllo world) ``` if __name__ == "__main__": unittest.main()
49
Python Type System (compared to other languages)
static dynamic ============================= strong haskell python c++ ruby weak JS, Perl ================================ dynamic typing - object types are only determined at runtime ``` example: def add(a, b) return a + b ``` add(1, 2) add(1.1, 2.2) add("a", "b") add([1, 2], [3, 4]) strong typing - no implicit type conversion example: add("hello", 1) Object References have no types
50
Mutable or Immutable
Mutable - list - dictionary - Immutable - tuple - string
51
Iteration Functions
sum() all() => all true i.e. all([True, True, False]) => False any() => any true i.e. any([True, True, False]) => True zip() => combines two sequences i.e. zip([1, 2, 3], [4, 5, 6]) => [(1, 4), (2, 5), (3, 6)] min() max() enumerate