What & How & Why

Computing

Week 1 notes


Computing

Vocabulary

  • Line of Code: A single instruction for the computer to perform.
  • Program: An independent collection of lines of code that serves one or more overall functions.
  • Input: Data that is fed into a program for it to operate upon.
  • Output: What the computer provides in return after running some lines of code.
  • Compile: To translate human-readable computer code into instructions the computer can execute. In the programming flow, this functions as a check on the code the user has written to make sure it makes sense to the computer.
    • 不能保证最后结果的正确
    • 分为静态(编译类)和动态(解释类)
  • Execution: Running some code and having it actually perform its operations.

Programming Languages

  • 语言只是前置,了解 computing 的内在联系才是关键

Console vs. GUI

Console
  • 所有的内容都是 text
GUI
  • an output medium that uses more than just text, like forms, buttons, tabs, and more. More programs are graphical user interfaces.
Computing vs. Programming
  • Computing 指如何 Programming
Code Segments
  • Variables
  • Functions
  • Reserved words
  • Input / Output

Python

  • High level
    • 更远离底层
    • 更少的依赖平台和操作系统
  • Interpreted

Programming

What is Programming?

  • Programming: Writing code through an iterative process of writing lines of code, attempting to execute them, and evaluating the results.

line of code
  • 一行代码代表一个指令
  • 任务可以分解成不同的指令
small chunks
  • code 应该尽量以小单位来组织

简单的 Python

  • print(),function,自带换行
  • python 区分大小写

Compiling vs. Executing

  • Static:
    • the entire program is compiled before it is run
    • prechecking the code for errors
  • Dynamic: the lines of code are compiled and run one at a time.
The Python Interactive Mode
  • 像计算器一样工作
  • 用于快速测试,获取结果
  • intercative mode 下,所有内容都会自动打印

Evaluating Results

  • errors
  • incorrect results

Debuging

  • 解决程序出现的 error 或是 incorrect results 的问题

Debug 的流程

  1. 获取足够多的信息
  2. 定位问题所在
  3. 解决问题

Joyner's Law of Debugging: The time required to fix an error is inversely proportional to the time required to find the error. The longer it takes to find the error, the easier it is to fix once found. (Generally.)

error type
  • error 分为:
    • compilation error : Errors that occur during the computer’s read through of the code.
      • syntax errors: 语法错误
      • Name errors: name 不存在
      • Type errors: 类型不匹配
    • runtime error: Errors that arise when trying to actually execute the code.
      • Divide by zero
      • Null errors: name exists but no value
      • Memory Errors: code that surpasses your memory
python 的错误类型
  • NameError:An error that usually occurs when you use a variable name that doesn't yet exist
  • TypeError: An error that occurs when we try to perform an operation on an object that doesn't make sense with the operation
  • AttributeError:An error occurs when we ask for information about a variable that doesn't make sense, like the happiness of a potato or the GPA of a turnip.
  • SyntaxError:An error that occurs when the line of code we've written can't be read by the computer because it doesn't match the computer's expectation for the programming language's grammar.
Reading Errors in Vocareum

Basic Debugging

  • Print Debugging:打印信息用于跟踪
  • Scope Debugging:缩小跟踪的范围,特别是 variable 在不同时期的状态变化
  • Rubber Duck Debugging:自己解释程序的逻辑,目标等,在解释的过程中找出 bug 的存在。

  • 在 loop 开始和结束的地方添加开始和完成的信息。
  • 通过打印的信息可以确定问题出在第二个 loop。
Scope Debugging in python

  • 首先确认不是次数的问题
  • 其次将累加 loop 的的范围减少为 2 次来查看是否出现了累加的问题
I call this “scope debugging” because the goal is to debug small scopes of your program, rather than trying to debug everything all at once. If you have a 20-line program, for example, you might break it down into four 5-line chunks and say, “Ok, the program has worked through the first five lines, but it breaks in the second five lines…”

没准二分法会更好一些?