Variables and expressions
Trang này cần trình duyệt gần đây (hỗ trợ SharedArrayBuffer). Vui lòng cập nhật Chrome, Edge, Firefox hoặc Safari lên phiên bản mới nhất.
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.
English
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.

English
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.
English
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.
English
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.
English
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.
English
Now you try
- Type your code below and press Run to see the output.
- Press Check answer to test it.
Khám phá
Variables hold one value
A variable is a named box; assigning replaces what it holds.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Sử dụng % (MOD) to print the remainder when 29 is divided by 4. Your program should print just the number.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
You pack 50 apples into boxes of 6. Store the number of full boxes in boxes using //, và apples left over in leftover using %.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
n là (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).
Nhấn Chạy để xem kết quả ở đây.