Variables and expressions
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
Variables hold values
- Variable เก็บค่าเพื่อให้คุณสามารถใช้งานภายหลังได้
- In Python you write
score = 10. The=means "store on the left" - คุณสามารถเปลี่ยน variable ได้โดยการใส่ค่าน้อยใหม่เข้าไปในนั้น
English
score = 10
score = score + 5
print(score)
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
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
Logical operators
andisTrueonly when both sides areTrueorisTruewhen at least one side isTruenotflipsTruetoFalseand back
English
print(True and False) # False
print(True or False) # True
print(not True) # False
In AP CSP pseudocode
- The AP exam uses its own pseudocode, not Python. The ideas are the same
- Assignment ใช้ลูกศรไปทางซ้าย.
MODคือค่าเหลือเศษ.DISPLAYแสดงผล.
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
ข้อผิดพลาดที่พบบ่อย
- กำหนดค่าให้ตัวแปรก่อนนำไปใช้
input()คืนค่าเป็นข้อความ — แปลงด้วยint(...)สำหรับตัวเลข
ลองดูเลย
- พิมพ์โค้ดของคุณด้านล่างแล้วกด Run เพื่อดูผลลัพธ์
- กด Check answer เพื่อทดสอบ
สำรวจ
Variables hold one value
A variable is a named box; assigning replaces what it holds.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Use % (MOD) to print the remainder when 29 is divided by 4. Your program should print just the number.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
You pack 50 apples into boxes of 6. Store the number of full boxes in boxes using // และ apples left over in leftover using %.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
n เป็น 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).
คลิก Run เพื่อดูผลลัพธ์ที่นี่