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