Variables and expressions · ตัวแปรและนิพจน์ (Variables and expressions)
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.
Variables hold values
- Variable เก็บค่าเพื่อให้คุณสามารถใช้งานภายหลังได้
- In Python you write
score = 10. The=means "store on the left" - คุณสามารถเปลี่ยน variable ได้โดยการใส่ค่าน้อยใหม่เข้าไปในนั้น
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.
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
Relational operators
- These compare two values and give
TrueorFalse. ==is "equal",!=is "not equal".<,>,<=,>=compare size.
Relational operators
- These compare two values and give
TrueorFalse ==is "equal",!=is "not equal"<,>,<=,>=compare size
print(5 == 5) # True
print(5 != 3) # True
print(7 < 2) # False
Logical operators
andisTrueonly when both sides areTrue.orisTruewhen at least one side isTrue.notflipsTruetoFalseand back.
Logical operators
andisTrueonly when both sides areTrueorisTruewhen at least one side isTruenotflipsTruetoFalseand back
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 uses a left arrow.
MODis the remainder.DISPLAYprints.
In AP CSP pseudocode
- The AP exam uses its own pseudocode, not Python. The ideas are the same
- Assignment ใช้ลูกศรไปทางซ้าย.
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
Common mistakes
- Give a variable a value before you use it.
input()returns text — convert withint(...)for numbers.
ข้อผิดพลาดที่พบบ่อย
- กำหนดค่าให้ตัวแปรก่อนนำไปใช้
input()คืนค่าเป็นข้อความ — แปลงด้วยint(...)สำหรับตัวเลข
Now you try
- Type your code below and press Run to see the output.
- Press Check answer to test it.
ลองดูเลย
- พิมพ์โค้ดของคุณด้านล่างแล้วกด Run เพื่อดูผลลัพธ์
- กด Check answer เพื่อทดสอบ
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. · ใช้ % (MOD) เพื่อพิมพ์เศษเหลือเมื่อ 29 หารด้วย 4 โปรแกรมควรพิมพ์เพียงตัวเลข
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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 %. · คุณบรรจุแอปเปิ้ล 50 ลูกลงในกล่องขนาด 6 เก็บจำนวน กล่องเต็ม ใน boxes โดยใช้ // และเก็บ แอปเปิ้ลที่เหลือ ใน leftover โดยใช้ %
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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). · n คือ 17. เก็บ True/False สำหรับ "n เป็นเลขคี่ไหม?" ใน is_odd (ใช้ %), และสำหรับ "n อยู่ระหว่าง 10 ถึง 20?" ใน in_range (ใช้ >, < และ and)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่