Python MIT OCW Lec1 : What is Computation?

김재만·2023년 9월 18일
0

Python MIT OCW

목록 보기
1/9

배울 내용

    1. Course Info
    1. What is computation?
    1. python basics
    1. mathmatical operations
    1. python variables and types

0. Course info

Topics

  1. Learning how to program
    • represent knowlege with data structures
    • iteration and recursion as computational metaphors
  2. How do you write good code
    • abstraction of procedures and data types
    • organize and modularize systems using object classes and methods
  3. Computer science
    • different classes of algorithms, searching and sorting
    • complexity of algorithms

1. What is computation?

What does a computer do?

  1. Calculation
    : built-in
    : ones that you define

  2. remembers results

  3. only know what you tell them

Types of knowlege

  • Declarative knowledge : fact
  • Imperative knowledge : recipe or "how-to"

What is a recipe?

  1. Sequence of simple steps
  2. Flow of control
  3. A means of determining when to stop

=> 1 + 2 + 3 = Algorithm

Stored Program Computer

  • Sequence of instructions stroed inside computer
  • Special program (interpreter) executes each instruction in order

Basic Primitives

  • In computing, language primitives are the simplest elements available in a programming language. A primitive is the smallest 'unit of processing' available to a programmer of a given machine, or can be an atomic element of an expression in a language. (From Wiki)
  • Turing showed that you can compute anything using 6 primitives (move left/right, read, write, scan, do nothing)
  • anything computable in one language is computable in any other programming language

Aspects of languages

  1. Primitive constructs

  2. Syntax
    : Symbol 이 배치되는 구조나 형식

  3. (Static) Semantics
    : Syntatically valid strings that have meaning
    : Syntax의 틀에 따라 작성된 언어를 이해하는 방법이나, 의미
    (https://jidoc.tistory.com/895)
    : 예를 들자면

    	3.2 * 5 // syntatically valid
    	3 + "hi" // static semantic error
  4. Programming languages have only one meaning but may not be what programmer intended

Where things go wrong

  • Syntatic errors
    : common and easily caught

  • static semantic errors
    : cause unpredictable behavior

  • no semantic errors but different meaning than what programmer intended
    : program crashes, stops running
    : program runs forever
    : program gives an answer but different than expected

2. Python Basics

Objects

  • programs manipulate data objects (everything is object)
  • objects have type
  • scalar (cannot be subdivided) / non-scalar (have internal structure)

Scalar Objects

  • int
  • float
  • bool (True / False)
  • NoneType : special and has one value, None
  • Can you type() to see teh type of an object

Type conversion

  • can convert object of one type to another
  • float(3) : integer 3 to float 3.0
  • int(3.9) truncates (끝을 자른다) float 3.9 to integer 3

3. Mathmatical Operations

Operations on ints and floats

  • i + j / i - j / i * j
    : if both are ints, result is int
    : if either or both are floats, result is float

  • i / j
    : result is float

  • i % j
    : remainder

  • i ** j
    : i to the power of j (i^j)

4. Python variables and types

  • type 은 위에서 서술.

Binding Variables and Values

  • Equal sign is an assignment

    	pi = 3.14159

    : pi is variable, 3.14159 is value

    pi_approx = 22/7  
  • Value stored in computer memory

  • Assignment binds name to value

Changing Bindings

  • can re-bind variable names using new assignment stastements
pi = 3.14
radius = 2.2
area = pi * (radius**2)
radius = radius + 1

# value of area is not changed

Abstracting Expressions

  • Why give names to values of expression?
    : to reuse names instead of values
    : easier to change code later

번외 : Programming vs Math

  • Math : solve for x
  • Programming : you need to tell the computer exactly how to solve for problem
profile
Hardware Engineer가 되자

0개의 댓글