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.
变量保存值
- משתנה מאחסן ערך כדי שתוכל להשתמש בו מאוחר יותר.
- בפיתון כותבים
score = 10. ה-=פירושו "אחסן בצד שמאל". - ניתן לשנות משתנה על ידי אחסון ערך חדש לתוכו.
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.
算术运算符
- האופרטורים הרגילים הם
+,-,*,/. //נותן חלוקה שלמה (ללא שארית);**הוא העלאה בחזקה.%נותן את השארית שנשארה לאחר החלוקה.
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.
מפעילים יחסיים
- אלו משווים שני ערכים ונותנים
TrueאוFalse. ==פירושו "שווה",!=פירושו "לא שווה".<,>,<=,>=משווים גודל.
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.
אופרטורים לוגיים
andמתהפך ל-Trueרק כאשר שני הצדדים הםTrue.orמתהפך ל-Trueכאשר לפחות אחד מהצדדים הואTrue.notהופךTrueל-Falseוחוזר אחורה.
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.
בפסאודוקוד AP CSP
- המבחן ב-AP משתמש בפסאודוקוד משלו, ולא ב-Python. הרעיונות זהים.
- שימוש במתחלק (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 כדי לראות את התוצאה.
- לחץ על בדיקת תשובה כדי לבדוק אותה.
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. · לחץ על הרץ כדי לראות את התוצא כאן.
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. · לחץ על הרץ כדי לראות את התוצא כאן.
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. · לחץ על הרץ כדי לראות את התוצא כאן.