Skip to content

Latest commit

 

History

History
217 lines (136 loc) · 7.92 KB

20221228-notes.md

File metadata and controls

217 lines (136 loc) · 7.92 KB

Notes

Lecture 1: What is Computation?

Source: https://ocw.mit.edu/courses/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/resources/lecture-1-what-is-computation/

Description: In this lecture, Dr. Bell introduces the theory of computation and explains some aspects of computational thinking. Programming languages are discussed, with an emphasis on basic Python syntax and data structures.

Course Info

Take good notes.

This is a fast-paced course.

Position yourself to succeed!!

  • Read psets when they come out and come back to them later.

New to something? PRACTICE. Try things out. PRACTICE.

(Since I already have worked with Python and coding before, I will probably have an easier time with this course than if I were an absolute beginner at programming.)

3 Core Goals of this Course

  1. Knowledge of Concepts
  2. Programming Skill
  3. Problem-Solving

Topics

  • Represent knowledge w. data structs.
  • iteration and recursion as computational metaphors
  • abstraction of procedures and data types
  • organize and modularize systems using object classes and methods
  • different classes of algorithms, searching and sorting
  • completity of algos.

Write code that is easy for others to understand. You YOURSELF will be wondering about what exactly you were thinking. LOL.

What is Computation?

What does a computer do?

  • Fundamentally:
    • performs calculations (like a billion calcs per sec)
    • remembers results (like 100s of gigabytes of storage)
  • What kinds of calculations?
    • built-in to the lang
    • Ones that you define as the programmer
  • Computers only know/do what you tell them.

Computers don't inherently know anything.

Types of Knowledge

  • Declarative knowledge is statements of fact.
  • Imperative knowledge is a recipe or a "how to"

The IDE that the instructor is using in this class in Anaconda. However, I may use something else, like this Linux VM on my laptop: image

In this example, I am generating random nums using Python in my terminal. This would be useful if I need a random number.

When you are faced with a problem in your everyday life, consider whether using a computer would enhance your ability to solve it.

When it comes to building a recipe (imperative knowledge) you need

  1. A sequence of simple steps
  2. Flow of control process that specifies when each step is executed.
  3. A means of determining when to stop.

1+2+3 = an algo!

Computers are machines

Back in the day, there were fixed-program computers, like calculators (add, subtract, mult, divide, etc.)

Now, we also have stored program computers, where the machine stores and executes instructions. This is the modern computer.

Basic Machine Architecture

  1. Memory (holds data and sequence of instructions)
  2. Arithmetic Logic Unit (do primitive ops)
  3. I/O
  4. Control Unit. image

With control flow a program may or may not progress linearly, but it may "jump" around from one part of the instructions to another and back, etc. Skip a line. Repeat a line, etc.

When the last instruction is finished, you might output something.

That is the basic way a computer works.

Stored-program computer

  • Secuence of instructions stored inside computer
    • Build from predefined set of primitive instructions
      1. Arithmetic & logic
      2. Simple tests
      3. Moving data
  • Special program (interpreter) executes each instruction in order
    • Use tests to change flow of control through the sequence
    • Stop when done.

Basic Primitives

Alan Turing said that you can compute anything using 6 Primitives

  1. Move left
  2. Move right
  3. Read
  4. Write
  5. Scan
  6. Do nothing

Using those 6 and a piece of tape, he showed you can compute anything.

Programming langs derived/descended from these basic building block steps.

If you can compute x in one programming lang, you can also do it in another lang.

Creating Recipes

  • A programming lang provides a set of primitive ops
  • Expressions are complex but legal combinations of primitives in a programming lang
  • Expressions and computations have values and meanings in a programming lang.

image https://www.techtarget.com/whatis/definition/primitive#:~:text=1)%20In%20computer%20programming%2C%20a,sophisticated%20program%20elements%20or%20interfaces. Definition of primitive.

Let's use the English language as a parallel to computer languages.

Primitive constructs

  • English:words
  • Programming language: numbers, strings, simple operators. image

image

Your expressions need to be syntactically valid.

image

Think about the MEANING of the phrase. image

For computers, there is only 1 meaning. It might be something you did not intend. But there is really only one meaning to a programming expression.

ERROR CODES ARE YOUR FRIEND. They help you identify bugs and learn.

image

Python Basics

image

Objects

Programs manipulate data objects.

Objects can have a type that defines the kinds of things programs can do with them.

Objects are

  • Scalar (cannot be subdivided) image

    • e.g. INT like 5. image
  • Non-Scalar (have internal structures that can be accessed)

    • e.g. LIST like [5, 6, 7, 8]

All py objects have a type.

Everything is an object in Python.

You can cast objects from one type to another image

print()

The print() function is one of the most basic. It shows the user what is passed through it.

Mathematical Operations

Operators

image

To assign a value, use the =

x = 2 + 45

image

We give names to values of expressions in order to reuse the NAMES instead of the VALUES. It enables our code to look a lot nicer.

image

Programming vs. Math

If you want the computer to solve for x, then you need to tell the computer exactly how to do it. image

Changing bindings

image

radius = radius + 1

...and radius is no longer bound to 2.2. So watch out!!

Next time:

How to add control flow to our programs

NOTE: Slides and code files up before each lecture

  • Highly encouraged to download them b4 lecure
  • Take notes and run code files when instructor does.
  • Use computer to answer in-class practice exercises.