Variables, constants & data types · 変数、定数、数据类型
A variable is a named box
- A variable is a name that stores a value.
- You put a value in with
=— this is called assignment. - Use a clear name so the program is easy to read.
変数は名前のついた箱
- 変数とは値を格納するための名前です。
=を使って値を入れる——これを代入と呼びます。- 読みやすいように明確な名前を使用します。
age = 15
price = 2.50
name = "Ada"
print(age, price, name)
Variables as named boxes · 変数は名付けられたボックス
Each = puts a value into a named box you can use later. · 各=は、後で使用できる名付けられたボックスに値を入れることができます。
The five data types
- IGCSE uses five basic data types:
- integer (whole number) → Python
int; real (decimal) →float. - char (one letter), string (text) →
str; Boolean (True/False) →bool.
5つのデータ型
- IGCSEでは5つの基本データ型を用います:
- 整数(whole number)→ Pythonの
int;実数(real、小数)→float。 - char(単一文字)、文字列(string、テキスト)→
str;ブール(Boolean、True/False)→bool。
score = 42 # integer
height = 1.75 # real
grade = "A" # char
town = "Cairo" # string
passed = True # Boolean
Constants stay the same
- A constant is a value that never changes while the program runs.
- Python has no special keyword, so we write constants in
CAPITALS. - This warns other readers: do not change me.
定数は変わらない
- 定数とは、プログラム実行中に値が変わらないものです。
- Pythonには特別なキーワードがないため、定数は
CAPITALSで書きます。 - これは他の読者への警告です:私を変えないでください。
PI = 3.14159
VAT_RATE = 0.20
print(PI)
Changing between types
input()gives a string, so convert it when you need a number.int(x)makes a whole number;float(x)a decimal;str(x)makes text.
型の変更
input()は文字列を返すため、数値が必要な際は変換する必要があります。int(x)は整数を、float(x)は小数を、str(x)はテキストを作ります。
n = int("5")
print(n + 1) # 6
print("Total: " + str(n)) # Total: 5
In the exam: DECLARE and CONSTANT
- Pseudocode names a type with
DECLARE, and fixes a value withCONSTANT.
試験向け:DECLAREとCONSTANT
- 擬似コードでは类型为
DECLAREで指定し、値をCONSTANTで固定します。
DECLARE Age : INTEGER
Age ← 15
CONSTANT Pi ← 3.14
Common mistakes
- Give a variable a value before you use it.
- A constant is a value you do not change — name it clearly.
- Keep the right type: numbers for maths, strings for text.
よくあるミス
- 変数に値を割り当てる前に使用しないでください。
- 定数は変更しない値であり、明確に名付けます。
- 適切な型を維持します:計算には数値、テキストには文字列。
Now you try
- Make variables and do a little maths with them.
- Press Check answer to test your code.
あなたも試してみよう
- 変数を作り、少しの計算を行ってみましょう。
- 回答を確認 を押してコードを試してください。
The variables width and height are set for you. Work out the rectangle's area (width × height) and store it in a variable called area. · 変数widthとheightが設定されています。長方形の面積(幅 × 高さ)を計算し、areaという名の変数に保存してください。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
price is a real number and quantity is an integer. Work out the total cost (price × quantity). · priceは実数でquantityは整数です。totalのコスト(価格 × 数量)を計算してください。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Read a whole number with input(), convert it with int(...), then print the number plus 3. For input 7, the output should be 10. · input()で整数を読み込み、int(...)で変換し、次に数字に3を加えたものを出力してください。入力7に対しては、出力が10になるはずです。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。