Skip to content

0. Tutorial

Kamal Banga edited this page Jul 26, 2019 · 11 revisions

#ToDo Installation & Setup

Python repl

Print 0 to 9

>>> [i for i in range(10)])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for i in range(10):
        print(i)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

range(n) object gives a list from 0 to n

Print even numbers in 0 to 9

[i for i in range(10) if i % 2 == 0]

Print squares of all numbers in 0 to 9

[i*i for i in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
  1. Dynamically-typed
  2. Arrays are lists.
  3. Has great data structures built-in: sets and dictionaries (key-value map, associative array)

Functions

Object references are passed by value. When you call a function with a parameter, a new reference is created that refers to the object passed in.


Iterators


Generators

From Introduction to Systems Notes

A coroutine is a routine which does not enforce a last-in first-out ordering on control transfers that we associate with procedures and functions. As a result, activation records for coroutines cannot usually be allocated on a stack, but must be allocated from a heap. Like a procedure, a coroutine is a body of code which must be associated with an activation record before it is run. Unlike procedures, however, the activation records for coroutines are not automatically allocated as a result of control transfers. Instead, the user must explicitly allocate coroutine activation records prior to a transfer of control, and the user may transfer control to a given coroutine, running in a given activation record, a number of times before that coroutine terminates.

The transfer of control to a coroutine is accomplished by a resume operation. This causes the computation which initiated the control transfer to be suspended, as a coroutine, and it causes the resumption of the computation to which control is transferred. As a result, two or more coroutine activations may easily transfer control back and forth, where each time a coroutine regains control, it picks up at the point it was most recently executing.

From lewissbaker

A normal function can be thought of as having two operations: Call and Return. ... Coroutines generalise the operations of a function by separating out some of the steps performed in the Call and Return operations into three extra operations: Suspend, Resume and Destroy.

You can think of the activation frame as the block of memory that holds the current state of a particular invocation of a function. This state includes the values of any parameters that were passed to it and the values of any local variables.

With “normal” functions, all activation frames have strictly nested lifetimes. This strict nesting allows use of a highly efficient memory allocation data-structure for allocating and freeing the activation frames for each of the function calls. This data-structure is commonly referred to as “the stack”. When an activation frame is allocated on this stack data structure it is often called a “stack frame”.

With “normal” functions, all activation frames have strictly nested lifetimes.

Since coroutines can be suspended without destroying the activation frame, we can no longer guarantee that activation frame lifetimes will be strictly nested. This means that activation frames cannot in general be allocated using a stack data-structure and so may need to be stored on the heap instead.


This section is a stub, please move on to Reference

Test GIF

Direct links

Iterators

Bell Curve

Clone this wiki locally