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.
الثوابت
- الثابت هو قيمة يجب ألا تتغير.
- بايثون ليس لديه كلمة محجوزة خاصة لذلك.
- بالعادة، نكتب أسماء الثوابت بحروف كبيرة
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. · اضغط تشغيل لرؤية المخرجات هنا.