当前位置: 代码迷 >> 综合 >> [计算机入门]笔记+闲谈(一)What is Computation
  详细解决方案

[计算机入门]笔记+闲谈(一)What is Computation

热度:58   发布时间:2023-10-28 08:39:50.0

这些话毫无用处

??以前就听说,优秀的学校都是请知识广博经验丰富的教授来讲《计算机导论》这门课的,因为只有拥有足够知识的人,才能给学生们在初学计算机类学科知识的时候建立起良好的知识体系,以有助于在今后的学习中都能有一个清晰的学习思路。
??以我个人为例,《计算机导论》这门课我基本上是逃课+走神中浑浑噩噩的度过的,我至今只记得老师那乏味的念书声的间歇,在黑板上写了如何进行二进制的换算,还记得我想要皮一下所以一直盯着老师的眼睛看,直到四目相对老师唰的一下移开目光,心中抑制不住的哈哈哈哈哈哈哈哈。不过我也因此付出了代价——大学四年我的学习就像是乞丐的衣服,东拼西凑的全是补丁,虽然我承认我不是一个认真学习的学生,不过我始终认为就那样照本宣读的教学方式真的是害人不浅。
??为了弥补我的七零八落的知识体系和学科知识,我决定就先从入门开始学习导论,从b站上找到了“6.0001 Introduction to Computer Science and Programming in Python. Fall 2016”,看到这个我就眼前一亮,这个真不错啊!不止可以学知识,还能学英文,看完了还能装逼,真实一举多得的好视频呢,下面为了我能够持之以恒的看这个视频,我就要逼自己记笔记,怎么说人的潜力都是逼出来的嘛。

这里才是正文

WHAT DOES A COMPUTER DO

  • Fundamentally:
    ?1. performs calculations
    ?a billion calculations per second!
    ?2. remembers results
    ?100s of gigabytes of storage
  • What kinds of calculations?
    ?1. bulit-in to the language(内置语言)
    ?very low level types of calculatios like addition, subtraction, multiplication, and so on
    ?2. ones that you define as the programmer
  • computers only know what you tell them

TYPES OF KNOWLEDGE

  • declarative knowledge(声明性知识) is statements of fact.
    【state-of-the-art:最新式的,使用最先进技术的】
  • imperative knowledge(命令性知识) is a recipe or “how-to”.

IDE: Anaconda
using: Python
purpose: chose a random number

import random
random.randint(16,277)

A NUMERICAL EXAMPLE

  • square root of number x is y such that y*y = x
    ?is a statement of fact
  • recipe for deducing square root of a number x(16)
    ?1) Start with a guess, g
    ?2) If g*g is close enough to x, stop and say g is the answer
    ?3) Otherwise make a new guess by averaging g and x/g
    ?4) Using the new guess, repeat process until close enough
g g*g x/g (g+x/g)/2
3 9 16/3 4.17
4.17 17.36 3.837 4.0035
4.0035 16.0277 3.997 4.000002

WHAT IS A RECIPE

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

COMPUTERS ARE MACHINES

  • how to capture a recipe in a mechanical process

  • fixed program
    ?computer: calculator

  • stored program
    ?computer: machines stores and executes instructions

BASIC MACHINE ARCHITECTURE

在这里插入图片描述
THE BASIC WAY THAT A COMPUTER WORKS

?When you load a sequence of instructions, the program counter starts at the first sequence.
?It starts at the sequence, at the first instruction.
?It gets what the instruction is, and it sends it to the ALU.
?ALU might get some data from memory. It might do some operations. And it might store data back into memory.
?And after it’s done, the ALU is going to go back, and the program counter is going to increase by 1, which means that we’re going to go to the next sequence in the instruction set.
?It just goes linearly, instruction by instruction.
?There might be one particular instruction that does some srt of test.
?The test is going to either return true or false. And depending on the result of that test, you might erither go to the next instruction, or you might set the program counter to go all the way back to the beginning, and so on.
?After you’re done when you finished executing the last instruction, then you might output something.

STORED PROGRAM COMPUTER

  • sequence of instructions stored inside computer
    ?built from predefined set of primitive instructions
    ?1) arithmetic ang logic
    ?2) simple tests
    ?3) moving data

  • special program (interpreter) executes each instruction in order
    ?use tests to change flow of control through sequence
    ?stop when done

BASIC PRIMITIVES

  • turing showed that you can compute anything using 6 primitives(move left, move right, read, write, scan, and do nothing)
  • modern programming languages have more convenient set of primitives
  • can abstract methods to create new primitives
  • anything computable in one language is computable in any other programming language

CREATING RECIPES

  • a programming language provides a set of primitive operations
  • expressions are complex but legal combinations of primitives in a programming language
  • expressions and computations have values and meanings in a programming language

ASPECTS OF LANGUAGES

  • primitive constructs
    ?numbers, strings, simple operators
  • syntax
  • static semantics is which syntactically vaild strings have meaning
  • semantics is the meaning associated with syntactically correct string of symbols with no static semantic errors

WHERE THINGS GO WRONG

  • syntactic errors
    ?common and easily caught
  • static semantic errors
    ?some language check for these before running program
    ?can cause unpredictable behavior
  • no semantic errors but different meaning than what programmer intended
    ?program crashes, stop running
    ?program runs forever
    ?program gives an answer but different than expected

PYTHON PROGRAMS

  • a program is a sequence of definitions and commands
  • commands (statements) instruct interpreter to do something
  • can be typed directly in a shell or stored in a file that is read into the shell and evaluated

OBJECTS

  • programs manipulate data objects
  • objects have a type that defines the kinds of things programs can do to them
  • objects are
    ?scalar(标量对象) (cannont be subdivided)
    ?non-scalar (have internal structure can be accessed)

SCALAR OBJECTS

  • int
  • float
  • bool
  • NoneType
  • can use type() to see the type of an object
>>> type(5)
int 
>>> type(3.0)
float

TYPE CONVERSIONS(CAST)

  • can convert object of one type to another
  • float(3) converts integer 3 to float 3.0
  • int(3.9) truncats float 3.9 to integer 3

PRINTING TO CONSOLE

  • to show output from code to a user, use print command
print(3+2)
5

EXPRESSIONS

  • combine objects and operators to form expressions
  • an expression has a value, which has a type
  • syntax for a simple expression
    ? <object> <operator> <object>

OPERATORS ON ints and floats

  • i + j → the sum
  • i - j → the difference
  • i * j → the product
    for the first three:
    ? if both are ints, result is int
    ? if either or both are floats, result is float
  • i / j → division
    ? result is float
  • i % j → the remainder when i is divided by j
  • i ** j → i to the power of j [双星号可以用于获得字典的值]

BINDING VARIABLES AND VALUES

  • equal sign is an assignment of a value to a variable name
    ? pi = 3.14159
    ? pi is variable, and 3.14159 is value
  • value stored in computer memory
  • an assignment binds name to value
  • retrieve value associated with name or variable by invoking the name by typing pi

ABSTRCTING EXPRESSIONS

  • why give name to values of expression?
  • to reuse names instead of values
  • easier to change code later

CHANGING BINDINGS

  • can re-bind variable names using new assignment statemenrs
  • previous value may still stored in memory but lost the handle for it
  • value for area does not change until you tell the computer to do the calculation again

本节讲的基本上就是计算机的运算机制,运算流程,变量声明。