CS 61A: Structure and Interpretation of Computer Programs

Dimethyl

About this course

Pure functions have some input (arguments) and return some output (result of applying them). A pure function must always return the same value when called twice with same arguments.

Non-pure function can generate side effects which make some changes to the state of interpreter or computer such as generating additional output beyond the return value with print function.

Defining New Functions

Both def statements and assignment statements can bind names to values and any existing bindings are lost.

A description of the formal parameters of a function is called the function’s signature.

Applying a user-defined function introduces a second local frame which is only accessible to that function.

Building Abstrations with Data

Every value in Python has a class that determined what type of value it is. The type function returns the class of a value.

Literals are expressions that evaluate to values of native types.

Python includes three native numeric types: integers (int), floating-point numbers (float), and complex numbers (complex). However, float cannot represent real numbers exactly.

The basic idea of data abstraction is to structure programs so that they operate on abstract data. That is, our programs should use data in such a way as to make as few assumptions about the data as possible. At the same time, a concrete data representation is defined as an independent part of the program.

In general, we can express abstract data using a collection of selectors and constructors, together with some behavior conditions.

Sequences

Sequences share common behaviours, in particular, length and element selection.

If the iterable name is not used in the for loop, it is a good practice to use an underscore _ as the name of the iterable.

List Comprehensions: [<map expression> for <name> in <sequence expression> if <filter expression>].

To evaluate a list comprehension, Python evaluates the <sequence expression>, which must return an iterable value. Then, for each element in order, the element value is bound to <name>, the filter expression is evaluated, and if it yields a true value, the map expression is evaluated. The values of the map expression are collected into a list.

Membership: in and not in operators can be used to check if a value is in a sequence.

Slicing: sequence[start:end] returns a new sequence containing the elements from start to end - 1. If start is omitted, it defaults to 0, and if end is omitted, it defaults to the length of the sequence.

Strings: Native data type for text in Python is called str. Strings satisfy the two basic conditions of a sequence: they have a length and they support element selection.

Membership operators in string match substrings rather than elements.

A string can be created from any object in Python by calling the str constructor function with an object value as its argument. This feature of strings is useful for constructing descriptive strings from objects of various types.

Mutable Data

A single data can represent something that evolves independently of the rest of the program. Adding state to data is a central ingredient of a paradigm called object-oriented programming.

The Object Metaphor

Objects combine data values with behavior. They represent information but also behave like things that they represent.

A class represents a kind of value and individual objects are instances of the class.

Objects have attributes which are named values that are part of the object and can be designated by . operator.

Objects also have methods which are function-valued arrtibutes that compute their results fron both their arguments and the object itself.

Sequence Objects

Sharing and Identity. In Python if assigned by their name, the name is similar to the role of reference in C++. As a result, two objects in Python is considered identical only if they are equal in their current value and any change to one will always be reflected in the other.

The is operator checks if two names refer to the same object, in the other words, if they are identical. The == operator only checks if two names refer to the objects with the same value.

Tuples. A tuple, an instance of the built-in type tuple, is an immutable sequence. It can be created using a tuple literal that separated by commas. () are optional but used commonly. Any type of data is accepted in a tuple, including other tuples.

While it is impossible to change the value of a tuple, it is possible to change the value of a mutable object in a tuple since the tuple only records the reference to the object.

Tuples are used implicitly in multiple assignment. An assignment of two values to two names creates a two-element tuple and then unpacks it.

Dictionaries. A dictionary contains key-value pairs, where both the keys and values are objects. A list of key-value pairs can be converted into a dictionary by calling the dict constructor function.

A key of a dictionary must be immutable. There can be at most one value for a given key.

Local State

  • Title: CS 61A: Structure and Interpretation of Computer Programs
  • Author: Dimethyl
  • Created at : 2025-06-28 10:54:16
  • Updated at : 2025-07-27 17:43:30
  • Link: https://dimethyl.online/2025/06/28/CS-61A-SICP/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
CS 61A: Structure and Interpretation of Computer Programs