6.100A: Lecture 1 (2022-09-07)
#

Required readings
#

  • Guttag, Chapter 1
  • Guttag, Sections 2.1-2.3

Topics - Course info
#

  • What is computation?
  • Python basics
    • Mathematical operations
    • Python variables and types
  • Control flow

Summary
#

Definitions
#

  • declarative knowledge. statements of fact
  • imperative knowledge. recipes/how-tos
  • algorithm. a set of well-defined steps to solve a particular problem
  • fixed program computer. a computer that is able to execute a fixed set of algorithms.
  • stored program computer (von Neumann machine). a machine that stores and executes instructions. The key insight is that programs are just data.

Course info
#

6.100A teaches more than just programming; it teaches a general methodology for problem-solving.

What is computation?
#

In one theory of epistemology, there are three types of knowledge:

  1. declarative knowledge (other names: propositional knowledge, descriptive knowledge), which consists of statements of fact
  2. imperative knowledge, which consists of "how-to" knowledge, or recipes
  3. acquaintance knowledge, which is the difference between knowing facts about a person and actually knowing them

Computation is about imperative knowledge; writing recipes to generate outputs. When a recipe solves a particular problem, we call that recipe an algorithm. For example, Google Maps uses a shortest path algorithm to find quickest path to your destination, while your professor uses an ad hoc algorithm (that is, an algorithm for a specific scenario and not a class of problems) to calculate your grades.

Computers are machines that execute algorithms.

Babylonian algorithm
#

The Babylonian method is an algorithm for computing square roots.

Specifically, given a number xx (input), the Babylonian method will find a number yy (output) such that y2=xy^2 = x.

When analyzing, designing, or generally working with algorithms, it is important to clearly define your inputs and desired outputs.

An analysis of algorithms class will delve into proving the algorithm's correctness and understanding its running time.

Stored program computer
#

A special program on the computer, called an interpreter, reads to code and executes it.

Aspects of languages
#

  • Primitive constructs
  • Syntax
  • Semantics

The Python programming language
#

Python can be used in two ways: executing commands through the Python Interactive Shell or running programs with the Python interpreter. The former is usually used for testing just a few lines of code, or as an interactive calculator. The latter is used the majority of the time, for any programming where you would like to rerun your code in the future.

The Python Interactive Shell, often referred to as simply the "Python shell" or "Python REPL" (REPL stands for "read-eval-print loop", which is what the shell does), TODO

Python basics
#

# this doesn't print anything when running the file!
2+2 
# this does print!
print(2+2)

Types
#

Scalar types
#

print(type(14))
print(type(14/2))
print(type('14'))
print(type('14'*2))
print(type('14'/2))
print(type(round(12.2)))
print(type(round(13.2,2)))
print(type('14'/2))

If you don't know what something is/does in Python, just open up a Python shell and try it!

base = 3
height = 4
area = (base*height)/2
height = base
# recalculate area using new values
area = (base*height)/2
print(area)

Simple expressions
#

Parentheses are used to tell Python which operations to perform first. Operator precedence rules are used when no explicit parentheses are given.

Variables
#

Variables in computer science are different from variables in math.

Variables in computer science are used to store values for later use. For example, if we want to keep track of a person's name, we might define a variable called

name
and assign it a value of
John
using the following:

name = "John"

The reason variables are important in programming is that they allow us to write code that will be run on values we do not yet know. For example, if we are writing a program

calc_grades.py
to calculate final grades, the formula that calculates grades is determined at the beginning of the semester. However, the results depend on the actual students' scores, which are only determined at a later time. Therefore, when writing
calc_grades.py
, we would use variables to represent student scores.

Comments
#

pi = 335/113
radius = 2.2
# area of circle equation <- this is a comment
area = pi*(radius**2)
# circumference of circle
circumference = pi*(radius*2)
print(area)

Swapping values?
#

x = 1			
y = 2
y = x
x = y

x = 1			
y = 2
temp = y
y = x
x = temp

# If you want to learn more about this syntax, read more about 
# packing/unpacking in Python:
#     https://blog.teclado.com/destructuring-in-python/.
# You may want to wait until you have learned about lists and tuples.
x = 1			
y = 2
x, y = y, x

Strings
#

num = 5
s = "my num is" + str(num)
print(s)

x = 1
print("my fav num is", x, ".", "x =", x)
x_str = str(x)
print("my fav num is " + x_str + ". " + "x = " + x_str)
area = pi*(radius**2)
print(area)
radius = radius + 1
area = pi*(radius**2)
print(area)

f-strings
#

Operator overloading
#

If we look closer at the operations on strings, we see that we use

+
to represent concatenation. But earlier, we said that
+
represented addition. This is an example of operator overloading.

Operator overloading is the use of the same operator (e.g.

+
,
*=
,
%
) on different object types. The actual operation performed by the operator is selected by the interpreter by examining the types of its operands.

Some examples:

  • +
    operator
    • between two numbers: addition
    • between two strings: concatenation
  • *
    operator
    • between two numbers: multiplication
    • between a string and a number: repeated concatenation