Astrasum

Home Explore
Support person
Home person

Lecture 1. What is Computation?

Add YouTube video lectures
Lecture
Delete?
Lecture
Delete?
Lecture
Delete?
Lecture
Delete?
Lecture
Delete?
Lecture
Delete?
Lecture
Delete?
Lecture
Delete?
Lecture
Delete?
Lecture
Delete?
Lecture
Delete?
Lecture
Delete?
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Lecture
Add another lecture
Cancel

AstraPath Info +

AstraNote Collection +

MIT 6.0001 Notes

# Lecture 1: What is Computation?
   1.1 Topics of the courses
* iteration and recursion
  * organize and modularize
* ALGORITHMS!
* complexity of algorithms
   1.2 Construction of languages
* primitive constructs - numbers, strings, simple operators
* syntax - 3.2 * 5
* static semantics - syntactically valid strings have meaning; e.g. 3.2 * "hi" #static semantic error
* semantics - meaning associated with a syntactically correct string of symbols with no static semantic errors
        # syntactic + static semantic + semantic = all those errors come from
   1.3 Objects (or type)
      1.3.1 scalar object (can not be subdivided)
* int - represents integers, ex. 5
* float - represent real numbers, ex. 3.27
* bool - represent Boolean values True and False
* Nonetype - special and has one value, None
        # type() - see the type of an object
        # int() or float() - type conversions (cast)
      1.3.2 non-scalar (internal structure)
* list - [1, 2, 3]
   1.4 Expression <object><operator><object>
        # an expression has a value, which has a type
* i + j - the sum
* i - j - the difference
* i * j - the product
* i / j - the division #result is float
** i % j - the remainder when i is divided by j
** i ** j - i to the power of j
        # use parentheses () to tell Python to do these operations first
    1.5 Binding variables and values
* equal sign is an assignment of a value to a variable name
* pi = 3.14159; radius = 2.2; area = pi * (radius ** 2)
        # radius = radius +1 is equal to radius += 1

  
# Lecture 2: Branching and iteration
   2.1 string object type
      2.1.1 strings - letters, special characters, spaces, digits 
* hi = "hello there"; name = "qixiang"; - enclose in quotation marks or single quotes
* greeting = hi + " " + name - concatenate strings
* silly = hi + " " + name * 3 - operations on string
      2.1.2 INPUT/OUTPUT - print("") & input("")
* 2 way of printing sentences - print("My name is", x, "."); print("My name is " + x + " .")
      2.1.3 comparision operators on int, float, string (ascii table) 
* i == j - equality test
* i != j - inequality test
      2.1.4 logic operators on bools
* not a - True if a is False, False if a is True
* a and b - True if both are True
* a or b - True if either or both are True 
   2.2 branching and conditionals (CONTROL FLOW)
      2.2.1 if<condition> + <expression>
      2.2.2 elif<condition> + <expression>
      2.2.3 else: +<expression>
* <condition> has a value True or False
* evaluate expressions in the block if <condition> is True
   2.3 indentation - how you denote (indiate) block of code
* vs == and != 
   2.4 iteration and loops
      2.4.1 while loops
* while <condition>: <expression> 
   # <condition> evaluates to a Boolean; 
      if <condition> is True do <expression>;
      check <condition> again; 
      repeat until <condition> is False
* iteration through nums in a sequence - i = 0; while num < i: <expression>
      2.4.2 for loops
* for <variable> in range(start, stop, step)
   # loop until value is stop -1
      2.4.3 break STATEMENT
* exits only innermost loop!
      2.4.4 comparation between for and while LOOPS
* for LOOPS - 
   # known number of iterations;
      with an inner counter; 
      rewritiable through while LOOPS
* while LOOPS - 
   # unbounded number of iterations; 
      can use a counter but must initialize; 
      MAYNOT rewritiable through for LOOPS

# Lecture 3: String Manipulation, Guess and Check, Approximations, Bisection
   # last 3 are ALGORITHMS for guessing
   3.1 string manipulation
      3.1.1 definition and basic operation
* sequence of case sensitive characters
* compare operation - ==, >, < etc.
* length operation - len()
      3.1.2 indexing
* square brackets to index string
   # s = "abc" index: 0 1 2 or -3 -2 -1
      s[0] == s[-3] = "a"; s[1] == s[-2] = "b"; s[2] == s[-1] = "c"
      3.1.3 slicing
* slice strings using [start: stop: step]
   # s = "abcdefgh"
      s[3: 6: 2] = "df" 
* strings are "IMMUTABLE" - cannot be modified
   # change s = "hello" to s = "yello"
      s[0] = 'y' - ERROR!
      s = 'y' + s[1: len(s)]
      3.1.4 LOOPS RECAP (string loops)
* loop variable 
   - numbers - for var in range(4, 6): <expression>
   - indexing in string - for index in range(len(s)): <expression>
   - CHAR - for char in s: <expression>
      # more pythonic for char in word
* example - Robot cheerleader & Common letter
   3.2 guess and check algorithms (exhuasted enumeration)
* for guess in range(cube + 1) - test from 0 to cube
if guess ** 3 == cube:
print("Cube root of", cube, "is", guess)
   # guess a value for solution
   # check if the solution is correct
   # keep guessing and checking ...unitil find solution or guessed all values
   3.3 approximate solutions (find an approximate solution)
* Initial value
   - epsilon (how close to the target) and increment (how large each step is)
   - target and guess, and num_guesses
* while abs(guess ** 3 - cube) >= epsilon and guess <=cube:
guess += increment
num_guesses += 1
   # good enough approximation
   # start with a guess and increment by small value
   # keep guessing if abs(guess ** 3 - cube) >= epsilon, for some small epsilon
   # decreasing increment size - slower program
      increasing epsilon - less accurate answer
   3.4 bisection search method
* half interval each iteration and new guess is halfway in between
* guess CONVERGES on the order of log2N steps
* Initial low and high value - low = 0, high = guess, guess = (high + low) / 2.0
  While abs(guess ** 3 - cube) >= epsilon:
if guess ** 3 < cube:
low = guess  #re-define the low boundary
else:
high = guess  #re-define the high boundary
guess = (high + low) / 2.0
num_guesses +=1

Introduction

Lecture 1

Concepts

  • computer - calculations, storage, result memorization, - tell them what to do! 
  • 1. statement of facts  2. order/recipe/ the sequence of steps  
  • flow of control: making decisions 
  • 6 primitives --> types of result -- > instruction flow --> output 
  • expression: primitives + programming languages
  • expression - value - type 

[Revisit]

Programming

  • 35:00
  • give names, reuse names 
  • = assignment (left - variable) 


Remaining Question: 

        usa_gold = 46
        uk_gold = 27
        romania_gold = 1
        total_gold = usa_gold + uk_gold + romania_gold
        print(total_gold)
        romania_gold += 1

    total_gold = usa_gold + uk_gold + romania_gold
    print(total_gold)

My question:

Why do we get 75 for the second print? 


My answer: 

From John's notes:    

  # radius = radius +1 is equal to radius += 1

so, romania_gold + = 1 is equal to romania_gold = romania gold + 1 



MIT 6.0001 Problem Set 1

#1a:
annual_salary = float(input('Enter your annual salary: '))
portion_saved = float(input('Enter the percent of your salary to save, as a decimal:​ '))
total_cost = float(input('Enter the cost of your dream home: '))
current_savings = 0
portion_down_payment = 0.25
down_cost = total_cost * portion_down_payment 
r = 0.04
monthly_salary = annual_salary/12
num_months = 0

while current_savings < down_cost:
    current_savings += monthly_salary * portion_saved + current_savings * r/12
    num_months += 1

num_years = num_months/12  

print('Number of months:', num_months)
print('Number of years:',num_years)

#1b
annual_salary = float(input('Enter your annual salary: '))
portion_saved = float(input('Enter the percent of your salary to save, as a decimal:​ '))
total_cost = float(input('Enter the cost of your dream home: '))
semi_annual_raise = float(input('Enter the semi­annual raise, as a decimal: '))
current_savings = 0
portion_down_payment = 0.25
down_cost = total_cost * portion_down_payment 
r = 0.04
monthly_salary = annual_salary/12
num_months = 0

while current_savings < down_cost:
    current_savings += monthly_salary * portion_saved + current_savings * r/12
    num_months += 1
    if num_months%6 == 0:
        monthly_salary *= (1 + semi_annual_raise)

num_years = num_months/12   
    
print('Number of months:', num_months)
print('Number of years:',num_years)

#1c
def whatrate():
    annual_salary = int(input('Enter the starting salary: '))
    total_cost = 1000000
    semi_annual_raise = 0.07
    current_savings = 0
    portion_down_payment = 0.25
    down_cost = total_cost * portion_down_payment 
    r = 0.04
    monthly_salary = annual_salary/12
    num_months = 36
    high = 10000
    low = 0
    ans = (high + low)/2
    numofsteps = 0
    
    max_savings = 0
    for i in range(1,num_months+1):
        max_savings += monthly_salary + current_savings * r/12
        if i%6 == 0:
            monthly_salary *= (1 + semi_annual_raise)
    if max_savings < down_cost:
        return print('It is not possible to pay the down payment in three years.')

    min_savings = 0
    for i in range(1,num_months+1):
        min_savings += monthly_salary * 0.0001 + current_savings * r/12
        if i%6 == 0:
            monthly_salary *= (1 + semi_annual_raise)
    if min_savings > down_cost:
        return print('You don\'t really need to save, mate.')    
    
    while abs(current_savings - down_cost) > 100:
        current_savings = 0
        monthly_salary = annual_salary/12
        numofsteps += 1
        for i in range(1,num_months+1):
            current_savings += monthly_salary * ans/10000 + current_savings * r/12
            if i%6 == 0:
                monthly_salary *= (1 + semi_annual_raise)
        if current_savings > down_cost:
            high = ans
        else:
            low = ans
        ans = (high + low)/2
    print('Best savings rate:', round(ans/10000,4))
    print('Steps in bisection search:​', numofsteps)

AstraPath Resources +

  • Download Python (https://www.anaconda.com/products/individual#Downloads)
  • Textbook Amazon link (https://http://www.amazon.com/exec/obidos/ASIN/0262529629/ref=nosim/mitopencourse-20)
  • Lecture slides and code (https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/)
  • Problem Set 0 (https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/assignments/ps0.zip)
  • Problem Set 1 (https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/assignments/MIT6_0001F16_ps1.pdf)
  • Problem Set 2 (https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/assignments/ps2.zip)
  • Problem Set 3 (https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/assignments/PS3.zip)
  • Problem Set 4 (https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/assignments/ps4.zip)
  • Problem Set 5 (https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/assignments/pset5.zip)
×
Contact Form