Variables and expressions
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
Variables hold values
- A variable stores a value so you can use it later.
- In Python you write
score = 10. The=means "store on the left". - You can change a variable by storing a new value into it.
日本語
変数は値を保持します
- 変数は後で使用できるよう値を格納します。
- Pythonでは
score = 10と記述します。=は「左側に格納する」という意味です。 - 変数に新しい値を代入することで、その値を変更できます。
score = 10
score = score + 5
print(score)
English
Arithmetic operators
- The usual operators are
+,-,*,/. //gives whole-number division (no remainder);**is power.%gives the remainder left after dividing.
日本語
演算子
- 通常の演算子は
+、-、*、/です。 //は整数除算(余りなし)を返し、**は累乗です。%は除算後の余りを返します。
print(17 // 5) # whole 5s that fit = 3
print(17 % 5) # what is left over = 2
print(2 ** 3) # 2 to the power 3 = 8
English
Relational operators
- These compare two values and give
TrueorFalse. ==is "equal",!=is "not equal".<,>,<=,>=compare size.
日本語
関係演算子
- これらは2つの値を比較し、
TrueまたはFalseを返します。 ==は「等しい」、!=は「等しくない」です。<、>、<=、>=は大小関係进行比较。
print(5 == 5) # True
print(5 != 3) # True
print(7 < 2) # False
English
Logical operators
andisTrueonly when both sides areTrue.orisTruewhen at least one side isTrue.notflipsTruetoFalseand back.
日本語
論理演算子
andがTrueとなるのは、両方の側がTrueの場合のみである。orがTrueとなるのは、少なくとも片方の側がTrueの場合である。notはTrueをFalseに変換し、再び元に戻します。
print(True and False) # False
print(True or False) # True
print(not True) # False
English
In AP CSP pseudocode
- The AP exam uses its own pseudocode, not Python. The ideas are the same.
- Assignment uses a left arrow.
MODis the remainder.DISPLAYprints.
日本語
AP CSP擬似コード
- AP試験ではPythonではなく独自の擬似コードを使用します。考え方は同じです。
- アサインメントは左向きの矢印を使用します。
MODは余りです。DISPLAYは出力します。
Python AP CSP pseudocode
score = 10 score ← 10
17 % 5 17 MOD 5
print(score) DISPLAY(score)
a == b a = b
a and b a AND b
English
Common mistakes
- Give a variable a value before you use it.
input()returns text — convert withint(...)for numbers.
日本語
よくあるミス
- 変数に値を割り当てる前に使用しないでください。
input()はテキストを返す — 数値に変換するにはint(...)を使用します。
English
Now you try
- Type your code below and press Run to see the output.
- Press Check answer to test it.
日本語
あなたも試してみよう
- 以下にコードを入力して実行ボタンを押すと、出力が表示されます。
- Check answer を押して確認する。
Explore · 探索
Variables hold one value
A variable is a named box; assigning replaces what it holds.
Use % (MOD) to print the remainder when 29 is divided by 4. Your program should print just the number.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
You pack 50 apples into boxes of 6. Store the number of full boxes in boxes using //, and the apples left over in leftover using %.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
n is 17. Store True/False for "is n odd?" in is_odd (use %), and for "is n between 10 and 20?" in in_range (use >, <, and and).
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。