Variables · משתנים
A box with a name
- A variable is a named box that holds a value.
- You put a value in the box, then use the name later.
- The value can be a number or a string.
קופסה עם שם
- משתנה הוא קופסה שמקבלת שם ואוחזת ערך.
- אתה מכניס ערך לקופסה, ולאחר מכן משתמש בשם לעתיד.
- הערך יכול להיות מספר או מחרוזת טקסט.
Make a variable
- Write
name = valueto store a value. - The name is on the left. The value is on the right.
=means "store this value", not "is equal to".
יצירת משתנה
- כתוב
name = valueלאחסון ערך. - ה-שם נמצא בצד שמאל. ה-ערך נמצא בצד ימין.
=פירושו "אחסן ערך זה", ולא "שווה ל".
city = "Paris"
print(city)
Naming rules
- Use letters, digits, and the underscore
_. - A name cannot start with a digit and cannot have spaces.
- Names are case-sensitive:
scoreandScoreare different. - Pick clear names like
ageortotal_score.
כללי חתימה
- השתמש באותיות, ספרות ובקו תחתון
_. - שם אינו יכול להתחיל בספרה ואינו יכול להכיל רווחים.
- שמות הם רגישים לגודל אות:
scoreו-Scoreהם שונים זה מזה. - בחר בשמות ברורים כמו
ageאוtotal_score.
Change a variable
- You can store a new value at any time.
- The new value replaces the old one.
- The name still points to one value — the latest one.
שינוי משתנה
- ניתן לאחסן ערך חדש בכל עת.
- הערך החדש מחליף את הישן.
- השם עדיין מצביע על ערך אחד — זה האחרון.
A variable is a labelled box · משתנה הוא קופסה עם תווית
Step through the program. Each line stores a value in a named box; reassigning count overwrites its box. One name, one current value. · עבור על התוכנית שורה אחר שורה. כל שורה מאחסנת ערך בתיבה בעלת שם; הקצאה מחדש של count תמחק את התיבה שלו. שם אחד, ערך נוכחי אחד.
count = 1
count = 2
print(count)
Constants
- A constant is a value that should not change.
- Python has no special keyword for it.
- By convention, we write constant names in
UPPERCASE.
קבועים
- קבוע הוא ערך שאינו אמור להשתנות.
- ל-Python אין מילת מפתח ייעודית לכך.
- לפי הסכמה, אנו כותבים שמות קבועים באותיות גדולות
UPPERCASE.
PI = 3.14159
print(PI)
Common mistakes
- Use
=to store a value: the name goes on the left, the value on the right. - Use a variable only after you have given it a value, or you get a NameError.
- Names cannot start with a number or contain spaces.
טעויות נפוצות
- השתמש ב-
=לאחסון ערך: השם הולך בצד שמאל, הערך בצד ימין. - השתמש במשתנה רק לאחר שנתת לו ערך, אחרת תקבל NameError.
- שמות לא יכולים להתחיל במספר או להכיל רווחים.
Now you try
- Use a variable for each task below.
- Press Check answer to test your code.
כעת תנסו בעצמכם
- השתמש במשתנה עבור כל משימה להלן.
- לחץ על בדוק תשובה כדי לבדוק את הקוד שלך.
Create a variable called age and store the number 15 in it. · צור משתנה בשם age ואחסן בה את המספר 15.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
The variable color starts as "red". Reassign it so that it becomes "blue". · המשתנה color מתחיל כ-"red". הקצה אותו מחדש כך שהוא יהפוך ל-"blue".
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Constants are written in UPPERCASE by convention. Create a constant MAX_SCORE and set it to 100. · קבועים כתובים באותיות גדולות לפי המקובל. צור קבוע בשם MAX_SCORE והגדר אותו ל-100.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
a is 1 and b is 2. Swap them, so a becomes 2 and b becomes 1, without typing the numbers 1 or 2 again. (Tip: a third variable can hold one value for a moment.) · a הוא 1 ו-b הוא 2. החלף ביניהם, כך ש-a יהפוך ל-2 ו-b יהפוך ל-1, מבלי להכניס שוב את המספרים 1 או 2. (טיפ: משתנה שלישי יכול לאחסן ערך אחד לרגע.)
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.