Making decisions · اتخاذ القرارات
Making a choice
ifruns some code only when a condition is true.- The condition is followed by a colon
:. - The lines that belong to the
ifare indented (4 spaces).
اتخاذ قرار
ifيشغل بعض الأوامر فقط عندما يكون الشرط صحيحاً.- يتبع الشرط نقطتان رأسيتان
:. - الأسطر التابعة لـ
ifتكون مُزاحة (4 مسافات).
Comparisons
- A condition is usually a comparison that is True or False.
==equal,!=not equal,<><=>=.- Note:
==tests equality; a single=stores a value.
المقارنات
- الشرط عادةً ما يكون مقارنة تكون صحيحة (True) أو خاطئة (False).
==يساوي،!=لا يساوي،<><=>=.- ملاحظة:
==يختبر المساواة؛ علامة=مفردة تخزن قيمة.
temperature = 30
if temperature > 25:
print("It is hot.")
if / elif / else
elif("else if") checks another condition if the first was false.elseruns when none of the conditions were true.- Python checks them in order and stops at the first true one.
if / elif / else
elif("otherwise if") يفحص شرطاً آخر إذا كان الأول خاطئاً.elseيشغل عندما لا تكون أي من الشروط صحيحة.- يفحص Python الشروط بترتيبها ويتوقف عند أول واحد صحيح.
hour = 14
if hour < 12:
print("Morning")
elif hour < 18:
print("Afternoon")
else:
print("Evening")
The remainder
a % bis what is left over whenais divided byb.year % 4 == 0means the year divides by 4 exactly — nothing left over.8 % 3is2, because 8 is two 3s with 2 left over.
الباقي
a % bهو ما يتبقى عندما يُقسَمaعلىb.year % 4 == 0يعني أن السنة تقبل القسمة على 4 تماماً — لا يوجد شيء متبقٍ.8 % 3هو2، لأن 8 تساوي اثنين من 3 مع بقاء 2.
Combine conditions
andis true only when both sides are true.oris true when at least one side is true.notflips true to false and false to true.
دمج الشروط
andتكون صحيحة فقط عندما يكون كلا الطرفين صحيحين.orتكون صحيحة عندما يكون على الأقل طرف واحد صحيحاً.notيعكس الصحيح إلى خاطئ والخاطئ إلى صحيح.
day = "Sat"
if day == "Sat" or day == "Sun":
print("Weekend!")
In Cambridge pseudocode
- Python
if/elif/elsematches the exam'sIF … ELSE … ENDIFandCASE.
في الرمز الوهمي لكامبريدج
- Python
if/elif/elseيطابقIF … ELSE … ENDIFوCASEفي الامتحان.
IF Score >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
CASE OF Grade
"A": OUTPUT "Top"
"B": OUTPUT "Good"
OTHERWISE OUTPUT "Keep trying"
ENDCASE
Common mistakes
- Compare with
==; a single=assigns. - Write
elif, notelse if, and end eachif/elif/elseline with:. - The indented block below
ifis what runs when the condition is true.
أخطاء شائعة
- قارنها بـ
==؛ علامة=مفردة تقوم بالتعيين. - اكتب
elif، وليسelse if، وانهِ كل سطرif/elif/elseبـ:. - كتلة الإزاحة أدناه
ifهي ما يتم تنفيذه عندما يكون الشرط صحيحاً.
Now you try
- When you Run, type the input in the box under the code and press Enter.
- The checks type the input for you. Read it, then decide what to print.
- Match the output text exactly.
الآن جرب بنفسك
- عند التشغيل، اكتب الإدخال في المربع أسفل الكود واضغط إدخال.
- فحوصات التحقق تكتب الإدخال لك. اقرأه، ثم حدد ما يجب طباعته.
- طابق النص الناتج بالضبط.
if / elif / else
Conditions are checked top-down; only the first true branch runs. · تُتحقق الشروط من الأعلى إلى الأسفل؛ يتم تنفيذ الفرع الحقيقي الأول فقط.
Read a score with int(input()). Print Pass if it is 50 or more, otherwise print Fail. For input 72, print Pass. · اقرأ درجة باستخدام int(input()). اطبع Pass إذا كانت 50 أو أكثر، وإلا اطبع Fail. للإدخال 72، اطبع Pass.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Read a score. Print A if it is 80+, B if it is 60+, otherwise C. Use if / elif / else. For input 75, print B. · اقرأ درجة. اطبع A إذا كانت 80+، وB إذا كانت 60+، وإلا C. استخدم if / elif / else. للإدخال 75، اطبع B.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Read an age. Print Yes if the age is from 13 to 19 (a teenager), otherwise No. Use and. For input 16, print Yes. · اقرأ عمراً. اطبع Yes إذا كان العمر بين 13 و19 (مراهق)، وإلا اطبع No. استخدم and. للإدخال 16، اطبع Yes.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Read a year with int(input()). A year divides by 4 when the remainder is 0: year % 4 == 0. Print Leap if it divides by 4, otherwise Common. For the input 2024, print Leap. · اقرأ سنة باستخدام int(input()). السنة تقبل القسمة على 4 عندما يكون الباقي 0: year % 4 == 0. اطبع Leap إذا كانت تقبل القسمة على 4، وإلا اطبع Common. بالنسبة للإدخال 2024، اطبع Leap.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Dividing by 4 is not the whole rule. A year that also divides by 100 is not a leap year, unless it divides by 400 too. 1900 is the trap: it divides by 4 and by 100, so print Common. Print Leap or Common. · القسمة على 4 ليست القاعدة الكاملة. السنة التي تقبل القسمة أيضاً على 100 ليست سنة كبيسة، إلا إذا كانت تقبل القسمة على 400 أيضاً. 1900 هي الفخ: فهي تقبل القسمة على 4 وعلى 100، لذا اطبع Common. اطبع Leap أو Common.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.