الدرس السابع: الشروط المتقدمة (Advanced Conditions) والوظائف (Functions) في Python

الدرس السابع: الشروط المتقدمة (Advanced Conditions) والوظائف (Functions) في Python
الدرس السابع: الشروط المتقدمة (Advanced Conditions) والوظائف (Functions) في Python

 

الدرس السابع: الشروط المتقدمة (Advanced Conditions) والوظائف (Functions) في Python

مقدمة:

الشروط والوظائف هما أساس البرمجة الاحترافية. باستخدام الشروط المتقدمة، نقدر نخلي البرامج تتخذ قرارات دقيقة. أما الوظائف، فهي أدوات أساسية لتنظيم الكود، تقليل التكرار، وزيادة كفاءته. في الدرس ده، هنتعلم كيفية كتابة شروط معقدة، وتصميم وظائف متعددة الاستخدامات.


أولاً: الشروط المتقدمة

1. مراجعة الشروط الأساسية

تعلمنا سابقًا إن الشروط بتستخدم لإجراء اختبارات منطقية باستخدام كلمات مثل if وelif وelse. دلوقتي هنشوف كيفية دمج الشروط للحصول على قرارات أكثر تعقيدًا.

2. الشروط المركبة باستخدام and وor

  • and: يتحقق إذا كانت كل الشروط صحيحة.
  • or: يتحقق إذا كان شرط واحد على الأقل صحيح.

مثال:

age = 25
income = 5000
if age > 18 and income >= 3000:
print("You are eligible for the loan.")
else:
print("You are not eligible for the loan.")

3. استخدام in وnot in للتحقق من القيم

  • in: يتحقق إذا كانت القيمة موجودة في قائمة.
  • not in: يتحقق إذا كانت القيمة غير موجودة.

مثال:


fruits = ["apple", "banana", "cherry"]
if "apple" in fruits:
print("Apple is available.")
if "mango" not in fruits:
print("Mango is not available.")

4. الشروط المتداخلة

نقدر نضع شرط داخل شرط آخر للحصول على تحكم أفضل.

مثال:

age = 20
has_license = True
if age >= 18:
if has_license:
print("You can drive.")
else:
print("You need a license to drive.")
else:
print("You are too young to drive.")

ثانيًا: الوظائف (Functions)

1. ما هي الوظائف؟

الوظيفة هي مجموعة من الأوامر اللي بنجمعها تحت اسم معين ونقدر نستدعيها بدل ما نكرر الكود.

2. إنشاء وظيفة بسيطة

لإنشاء وظيفة بنستخدم الكلمة المفتاحية def:

مثال:


def greet():
print("Hello, welcome to Python!")
greet() # استدعاء الوظيفة

3. تمرير معطيات (Parameters) إلى الوظيفة

نقدر نضيف مدخلات للوظيفة للحصول على مرونة أكتر.

مثال:


def greet(name):
print(f"Hello, {name}!")
greet("Mustafa")

4. إرجاع القيم باستخدام return

لوظيفة ترجع نتيجة معينة، بنستخدم return.

مثال:


def add(a, b):
return a + b
result = add(10, 5)
print("The sum is:", result)

5. القيم الافتراضية (Default Values)

نقدر نحدد قيمة افتراضية للمعطيات.

مثال:


def greet(name="Guest"):
print(f"Hello, {name}!")
greet() # الناتج: Hello, Guest!
greet("Ali") # الناتج: Hello, Ali!

ثالثًا: أمثلة عملية على الشروط والوظائف

1. برنامج بسيط للتحقق من صلاحية كلمة مرور:


def check_password(password):
if len(password) < 8:
return "Password is too short."
elif not any(char.isdigit() for char in password):
return "Password must contain at least one number."
elif not any(char.isupper() for char in password):
return "Password must contain at least one uppercase letter."
else:
return "Password is strong."
password = input("Enter your password: ")
print(check_password(password))

2. برنامج لتحديد الفصول بناءً على الشهور:


def get_season(month):
if month in ["December", "January", "February"]:
return "Winter"
elif month in ["March", "April", "May"]:
return "Spring"
elif month in ["June", "July", "August"]:
return "Summer"
elif month in ["September", "October", "November"]:
return "Autumn"
else:
return "Invalid month"
month = input("Enter the month: ")
print("The season is:", get_season(month))

3. برنامج آلة حاسبة:


def calculator(a, b, operation):
if operation == "+":
return a + b
elif operation == "-":
return a - b
elif operation == "*":
return a * b
elif operation == "/":
return a / b
else:
return "Invalid operation!"
a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
operation = input("Enter the operation (+, -, *, /): ")
print("The result is:", calculator(a, b, operation))

خاتمة:

في هذا الدرس، تعرفنا على الشروط المتقدمة وكيفية استخدامها، بالإضافة إلى الوظائف ودورها في تنظيم الكود وجعله أكثر كفاءة. في الدرس القادم، هنستعرض البرمجة الكائنية (Object-Oriented Programming) واللي بتعتبر خطوة أساسية في البرمجة الاحترافية.


تعليقات