Skip to content

3.1 Variables and Assignment 变量与赋值

To find specific solutions to generalizable problems, programmers represent and organize data in multiple ways.

  • 为了找到可泛化问题的具体解决方案,程序员以多种方式表示和组织数据。

核心要点 Core Points

  1. A variable is an abstraction inside a program that can hold a value. Each variable has associated data storage that represents one value at a time, but that value can be a list or other collection that in turn contains multiple values.

    • 变量是程序内部可以保存值的抽象。每个变量都有相关的数据存储,一次表示一个值,但该值可以是列表或其他包含多个值的集合。
  2. Using meaningful variable names helps with the readability of program code and understanding of what values are represented by the variables.

    • 使用有意义的变量名有助于程序代码的可读性和理解变量表示的值。
  3. Some programming languages provide types to represent data, which are referenced using variables. These types include numbers, Booleans, lists, and strings.

    • 一些编程语言提供类型来表示数据,这些类型通过变量引用。这些类型包括数字、布尔值、列表和字符串。
  4. Some values are better suited to representation using one type of datum rather than another.

    • 某些值更适合使用一种数据类型而不是另一种来表示。
  5. The assignment operator allows a program to change the value represented by a variable.

    • 赋值运算符允许程序更改变量表示的值。
  6. The exam reference sheet provides the "←" operator to use for assignment. For example, Text: a ← expression Block: a ← expression evaluates expression and then assigns a copy of the result to the variable a.

    • 考试参考表提供了用于赋值的"←"运算符。例如,文本:a ← expression 块:a ← expression 计算expression然后将结果的副本赋给变量a
  7. The value stored in a variable will be the most recent value assigned. For example: a ← 1, b ← a, a ← 2, display (b) still displays 1.

    • 存储在变量中的值将是最新分配的值。例如:a ← 1b ← aa ← 2display (b) 仍然显示1

学生活动 Student Activities

  1. Represent a value with a variable.

    • 用变量表示一个值。
  2. Determine the value of a variable as a result of an assignment.

    • 确定变量作为赋值结果的值。

基于 VitePress 构建的 AP CSP 学习平台