Numbers & operators · 数と演算子
Two kinds of number
- An integer (
int) is a whole number, like7or-3. - A float is a number with a decimal point, like
2.5. - Division with
/always gives a float, even4 / 2→2.0.
2種類の数値
- 整数 (
int) はwhole numberで、7や-3のようなものです。 - 浮動小数点数 (float) は小数点を含む数値で、
2.5のようなものです。 /による割り算は常に float を返します。4 / 2→2.0のように整数同士の割り算でも例外ではありません。
Add, subtract, multiply, divide
+adds,-subtracts,*multiplies,/divides.- You can use them with variables or with plain numbers.
- Spaces around operators are optional but make code easier to read.
足し算、引き算、掛け算、割り算
+は足し算、-は引き算、*は掛け算、/は割り算を行います。- 変数や単純な数値と一緒に使えます。
- 演算子の周りにスペースを入れるのは任意ですが、コードが読みやすくなります。
print(7 + 2)
print(7 - 2)
print(7 * 2)
print(7 / 2)
Whole-number division, remainder, powers
//divides and keeps only the whole part (floor division).%gives the remainder (called modulo).**raises to a power:2 ** 3is 2×2×2.
整数除法、剰余、べき乗
//は割り算を行い、整数部分のみを返します(床関数除法)。%は剰余(モジュロ演算)を返します。**はべき乗を計算します:2 ** 3は 2×2×2 です。
print(7 // 2)
print(7 % 2)
print(2 ** 5)
Order of operations
- Python does
**first, then*///%, then+-. - This is the same order you use in maths.
- Use parentheses
(...)to change the order.
演算子の優先順位
- Python はまず
**を処理し、次に*///%を処理し、最後に+-を処理します。 - これは数学で使う順序と同じです。
- 順序を変えるには括弧
(...)を使います。
print(2 + 3 * 4)
print((2 + 3) * 4)
Common mistakes
/always gives a float (4 / 2is2.0); use//when you want a whole number.- Power is
**, not^. "3" + "4"joins text into"34"— it does not add to7.
よくあるミス
/は常に float を返します(4 / 2は2.0です)。整数が欲しい場合は//を使ってください。- べき乗は
**で、^ではありません。 "3" + "4"はテキストを"34"に結合します——7に加算するものではありません。
Now you try
- Store each answer in the variable named in the task.
- Press Check answer to test your code.
あなたも試してみよう
- 各答えをタスクで指定された変数名で格納してください。
- 回答を確認 を押してコードを試してください。
Order of operations · 演算の優先順位
Python evaluates × and ÷ before + and −, left to right. · Python は左から右へ × と ÷ を + と − より先に評価します。
Set length to 8 and width to 5. Then store their product in a variable called area. · length を 8 に、width を 5 に設定してください。その後、それらの積を area という変数に格納してください。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
You share 17 sweets among 5 children. Store how many each child gets in each (use //), and how many are left over in left (use %). · 17 個のキャンディを 5 人の子供たちに分けましょう。各子供が何個受け取るかをeach に格納し(// を使用)、残りをleft に格納してください(% を使用)。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Store 3 ** 2 + 1 in result. Then use parentheses to store (3 + 1) ** 2 in grouped. · 3 ** 2 + 1 をresult に格納してください。その後、括弧を使って(3 + 1) ** 2 をgrouped に格納してください。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
celsius is 37. Store the temperature in Fahrenheit in fahrenheit, using the formula celsius × 9 ÷ 5 + 32. · celsius は 37 です。ファレンハイトでの温度をfahrenheit に格納してください。公式 celsius × 9 ÷ 5 + 32 を使用します。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。