How to Think Like a Computer Scientist: Learning with Python - Notes

The Way of the Program

This book aims to teach the reader how to think like a computer scientist, blending mathematics, engineering, and natural science thinking. Key is problem-solving: formulating problems, thinking creatively about solutions, and expressing solutions clearly.

The Python Programming Language

Python is a high-level language (others include C++, Perl, Java).
Computers execute low-level languages. High-level programs need processing via interpreters or compilers.
Interpreters execute code line by line, while compilers translate the entire program before execution.
Python is interpreted and can be used in command-line mode or script mode (.py files).

What is a program?

A program is a sequence of instructions for a computation. Basic instructions include:

  • Input: Getting data.

  • Output: Displaying data.

  • Math: Performing calculations.

  • Conditional execution: Checking conditions.

  • Repetition: Repeating actions.

Programming involves breaking down complex tasks into smaller subtasks achievable with these basic instructions.

What is Debugging?

Debugging is tracking down and correcting programming errors (bugs). Errors can be:

  • Syntax errors: Violations of the programming language's structure.

  • Runtime errors: Errors occurring during program execution (exceptions).

  • Semantic errors: Program runs but produces incorrect results due to flawed logic.

Debugging is an experimental process of hypothesizing, modifying, and testing to eliminate errors.

Formal and Natural Languages

Natural languages (e.g., English) evolved organically and are often ambiguous. Formal languages are designed for specific purposes and are nearly unambiguous.
Formal languages (including programming languages) have strict syntax rules regarding tokens (basic elements) and structure.
Parsing is the process of understanding the structure of a sentence. Formal languages are more dense and require careful attention to detail compared to natural languages.

The First Program

The traditional first program, "Hello, World!" demonstrates basic output.
In Python: print "Hello, World!"

Glossary

Key terms include problem-solving, high-level language, low-level language, portability, interpreting vs. compiling, source code, object code, executables, scripts, programs, algorithms, bugs and debugging, syntax and semantics, and formal vs. natural languages.

Variables, Expressions, and Statements

Values and Types

A value is a fundamental entity (e.g., number, string) manipulated by a program. Each value belongs to a type:

  • int: Integers (e.g., 2, -5).

  • str: Strings (e.g., "Hello").

  • float: Floating-point numbers (e.g., 3.14).
    Type can be checked using the type() function. Commas in integers are semantic errors.

Variables

A variable is a name referring to a value. Assignment statements (e.g., x = 5) create variables. State diagrams visualize variables and their values.

Variable Names and Keywords

Variable names should be meaningful and can be arbitrarily long. They can include letters, numbers, and underscores, but they must start with a letter. Case matters (e.g Bruce and bruce are different variables).
Certain keywords (e.g., class, def, if) are reserved and cannot be used as variable names.

Statements

A statement is an instruction that the Python interpreter can execute (e.g., assignment, print). Scripts consist of sequences of statements.

Evaluating Expressions

An expression is a combination of values, variables, and operators. The interpreter evaluates expressions and displays the result.
Evaluating an expression is different from printing a value.

Operators and Operands

Operators are symbols representing computations (+, -, *, /, **). Operands are the values operators use. Python performs integer division when both operands are integers, which may lead to unexpected results. Floating point division can be forced by using a float as one of the operands.

Order of Operations

Python follows PEMDAS (Parentheses, Exponentiation, Multiplication/Division, Addition/Subtraction) for order of operations. Operators with the same precedence are evaluated from left to right.

Operations on Strings

Mathematical operations on strings are generally not allowed. The '+' operator concatenates strings, and the '*' operator repeats them.

Composition

Statements and expressions can be combined for complex computations (e.g print 17 + 3).

Comments

Comments (using #) explain the code in natural language.

Glossary

Key terms include value, type, variable, statement, assignment, state diagram, keyword, operator, operand, expression, evaluate, integer division, rules of precedence, concatenate, composition, and comment.

Functions

Function Calls

Functions are called by using parentheses. They take arguments, and return values. An example is the type() function.

Type conversion

Built-in functions like int(), float(), and str() convert values between types. Int truncats.

Type coercion

Python coerces types automatically in some cases, for example, converting integers to floats in mixed-type arithmetic operations.

Math functions

Python's math module provides functions like sin(), log(), etc. You need to import the math module first. These functions use dot notation.

Composition

Python functions can be composed, meaining you can use one expression as an argument to another function.

Adding new functions

The syntax for a function definition is def NAME( LIST OF PARAMETERS ): STATEMENTS. Functions simplify programs by hiding complex computations, and make it possible to eliminate repetetive code.

Definitions and use

Function definitions are executed when encountered, but statements inside them are executed when the function is called. Before execution you have to create the function.

Flow of execution

Execution always begins at the first statement of the program and continues from top to bottom, except for the calls of functions, where the execution jumps to the called function and returns to the caller function.

Parameters and arguments

Arguments are values that controls how the function does its job. Inside the function, the values that are passed get assigned to variables called parameters. The name of the variable we pass as an argument has nothing to do with the name of the parameter.

Variables and parameters are local

Local variables are only available inside the function. Paraameters are also local.

Stack diagrams

Stack diagrams show the value of each variable but also show the function to which each variable belongs. Each function is represented by a frame with the name of a function beside it and the parameters and variables of the function inside it.

Functions with results

Python offers a range of built-in functions that comes with results.

Glossary

Key terms include function call, argument, return value, type conversion, type coercion, module, dot notation, function, function definition, flow of execution, parameter, local variable, stack diagram, frame, and traceback.