Variables and names
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A variable is a name that refers to a value. Step through a four-line program, score.py, and watch what = really does: Python works out the right side first, makes an object in memory, then ties the name on the left to it. Two names can share one object, a name can move on to a new one, and a name with the wrong capital letter does not exist at all.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01In Python, what does x = 7 mean?
= is assignment, not a comparison: Python works out the right side, 7, and ties the name x to it. Comparing uses ==.
02After these lines, what does print(a, b) show? a = 3 b = a a = a + 1
b = a ties b to the same 3. Then a + 1 makes a new value, 4, and only a moves on to it. b still refers to 3, so it prints 4 3.
03In score = score + 5 , which part does Python run first?
The right side of = always runs first, using the value score refers to now. Only then is the name score tied to the result.
04Which of these is a valid variable name?
Names may use letters, digits and underscores but cannot start with a digit (2nd_place), contain a space (my total), or be a keyword (for). total_2 is fine.
05A program sets score = 15 and later runs print(Score) . What happens?
Names are case-sensitive, so Score is a different name from score, and nothing ever made Score. Python stops with a NameError (and since 3.10 suggests: Did you mean: 'score'?).
المسرد
- Variable
- A name that refers to a value. In Python the name points at an object; it does not hold the value inside itself.
- Assignment (=)
- The statement name = value. Python works out the right side first, then ties the name on the left to the result.
- Object
- Python's word for one value in memory, such as the whole number 10. Every object has a type, such as int.
- Reassignment
- Assigning to a name that already exists, which moves the name on to a new value. Other names are not affected.
- Identifier
- The technical word for a name: letters, digits and underscores, not starting with a digit.
- Keyword
- A word Python reserves for its own grammar, such as if, for and while. It cannot be used as a name.
- Case-sensitive
- Capital and small letters count as different, so score, Score and SCORE are three different names.
- NameError
- The error Python raises when code uses a name that has not been assigned.
الملاحظات والمصادر
- Python calls every value an object. A variable does not hold its value like a box; it is a name tied to an object, and = changes which object a name is tied to.
- Whole numbers (int) and strings never change once made, so score + 5 always makes a new object. Two names can share any object, but sharing only shows when an object can change, such as a list.
- The is operator asks whether two names refer to the very same object; == asks whether two values are equal.
- On macOS and Linux the command is often python3 rather than python; on Windows the py launcher also works. The $ is the terminal's own prompt: you do not type it.
- The real NameError message is one line; the figure wraps it. Python also shows the failing line with ^^^^^ under Score, and the ... stands for the full path to the file. The "Did you mean" hint appeared in Python 3.10.
- Python style (PEP 8) writes variable names in lower case with words joined by underscores, such as total_score.
- Python reference: Assignment statements
- Python reference: Naming and binding
- Python reference: Objects, values and types
- Python reference: Identifiers and keywords
- Python library: keyword (the list of keywords)
- Python library: Built-in exceptions (NameError)
- What's new in Python 3.10: better NameError messages
- Python tutorial: An informal introduction to Python
- PEP 8: Naming conventions
اختبار يتاح بعد الخطوة الأخيرة.