6.100A teaches more than just programming; it teaches a general methodology for problem-solving.
In one theory of epistemology, there are three types of knowledge:
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.
The Babylonian method is an algorithm for computing square roots.
Specifically, given a number (input), the Babylonian method will find a number (output) such that .
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.
A special program on the computer, called an interpreter, reads to code and executes it.
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
# this doesn't print anything when running the file!
2+2
# this does print!
print(2+2)
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)
Parentheses are used to tell Python which operations to perform first. Operator precedence rules are used when no explicit parentheses are given.
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
John
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
calc_grades.py
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)
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
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)
If we look closer at the operations on strings, we see that we use
+
+
Operator overloading is the use of the same operator (e.g.
+
*=
%
Some examples:
+
*