40923227 cp2020

  • Home
    • Site Map
    • reveal
    • blog
  • About
    • Develop
      • test
  • HomeWork
    • HW1
      • Overview
      • AGP Slots (AGP插槽)
      • Processors(處理器)
      • Video Displays and Video Cards(顯示器與顯示卡)
      • Video Output
      • Install and Configure Expansion Cards(安裝和配置擴充卡)
      • Storage Devices Overview(存儲設備概述)
      • Storage Devices Hard Disk Drives(儲存設備 硬碟驅動器)
      • Storage Devices  Hot Swapping(儲存設備 熱交換)
      • Heat Sources(熱源)
      • heat reduction
      • BIOS Settings(bios設置)
    • HW2
      • 作業2-1
      • 變數 Variables
      • HTML Tutorial HTML教程
    • HW3
      • Check Tic Tac Toe
      • Max Of Three
      • Tic Tac Toe Game
作業2-1 << Previous Next >> HTML Tutorial HTML教程

變數 Variables

  • Variables, expressions, and statements,Values and types


    變量,表達式和語句,值和類型

    A value is one of the basic things a program works with, like a letter or a number. The values we have seen so far are 1, 2, and "Hello, World!"

    值是程序使用的基本內容之一,例如字母或數字。到目前為止,我們看到的值為1、2和“ Hello,World!”。

    These values belong to different types: 2 is an integer, and "Hello, World!" is a string, so called because it contains a "string" of letters. You (and the interpreter) can identify strings because they are enclosed in quotation marks.

    這些值屬於不同的類型:2是整數,“ Hello,World!”是字符串,之所以稱為字符串是因為它包含字母的“字符串”。您(和解釋器)可以識別字符串,因為它們用引號引起來。

    The print statement also works for integers. We use the python command to start the interpreter.

    print語句也適用於整數。我們使用python命令啟動解釋器。

    1
    2
    python
    >>> print(4)

    If you are not sure what type a value has, the interpreter can tell you.

    如果您不確定值的類型,解釋器可以告訴您。

    Not surprisingly, strings belong to the type str and integers belong to the type int. Less obviously, numbers with a decimal point belong to a type called float, because these numbers are represented in a format called floating point

    毫不奇怪,字符串屬於str類型,而整數屬於int類型。不太明顯的是,帶小數點的數字屬於稱為浮點的類型,因為這些數字以稱為浮點的格式表示

    What about values like "17" and "3.2"? They look like numbers, but they are in quotation marks like strings.

    那麼“ 17”和“ 3.2”之類的值呢?它們看起來像數字,但用引號引起來,例如字符串。

    1
    2
    >>> type('17')
    >>> type('3.2')

    They're strings.

    他們是字符串。

    When you type a large integer, you might be tempted to use commas between groups of three digits, as in 1,000,000. This is not a legal integer in Python, but it is legal:

    當您輸入一個大整數時,您可能會想在三位數的組之間使用逗號,例如1,000,000。在Python中這不是合法的整數,但它是合法的:
    1
    >>> print(1,000,000)
    Well, that's not what we expected at all! Python interprets 1,000,000 as a comma-separated sequence of integers, which it prints with spaces between.
    好吧,這根本不是我們所期望的! Python將1,000,000解釋為以逗號分隔的整數序列,並在其之間打印出空格。

    This is the first example we have seen of a semantic error: the code runs without producing an error message, but it doesn't do the "right" thing.

    這是我們看到的第一個語義錯誤示例:代碼運行時未產生錯誤消息,但沒有做“正確的”事情。

    Variables 變數

    One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value.

    編程語言最強大的功能之一就是能夠操縱變量。變量是引用值的名稱。

    An assignment statement creates new variables and gives them values:

    賦值語句創建新變量並為其提供值:

    1
    2
    3
    >>> message = 'And now for something completely different'
    >>> n = 17
    >>> pi = 3.1415926535897931
    This example makes three assignments. The first assigns a string to a new variable named message; the second assigns the integer 17 to n; the third assigns the (approximate) value of π to pi.
    本示例進行了三個分配。第一個將字符串分配給一個名為message的新變量;第二個將整數17分配給n;第三個將π的(近似)值分配給pi。
    To display the value of a variable, you can use a print statement:
    要顯示變量的值,可以使用打印語句:
    1
    2
    3
    4
    >>> print(n)
    17
    >>> print(pi)
    3.141592653589793

    The type of a variable is the type of the value it refers to.

    變量的類型是它所引用的值的類型。

    1
    2
    3
    4
    5
    6
    >>> type(message)
    <class 'str'>
    >>> type(n)
    <class 'int'>
    >>> type(pi)
    <class 'float'>

  • 作業2-1 << Previous Next >> HTML Tutorial HTML教程
  • Copyright © All rights reserved | This template is made with by Colorlib