المقال الثاني في سلسلة “تعلم Python”. سنتعلم الشروط الحلقات والدوال.
الشروط (if/else)
conditions.py
age = int(input(“أدخل عمرك: “))
if age >= 18:
print(“أنت بالغ”)
elif age >= 13:
print(“أنت مراهق”)
else:
print(“أنت طفل”)
# Comparisons: ==, !=, >, <, >=, <=
# Logical: and, or, not
الحلقات (Loops)
loops.py
# For loop
for i in range(5):
print(f“العدد: {i}”)
for i in range(5):
print(f“العدد: {i}”)
# While loop
count = 0
while count < 5:
print(count)
count += 1
# Loop through list
fruits = [“تفاح”, “موز”, “برتقال”]
for fruit in fruits:
print(fruit)
الدوال (Functions)
functions.py
# Simple function
def greet(name):
return f“مرحباً {name}!”
def greet(name):
return f“مرحباً {name}!”
print(greet(“أحمد”))
# Function with default value
def calculate_area(length, width=10):
return length * width
print(calculate_area(5)) # 50
print(calculate_area(5, 3)) # 15
# Lambda function
square = lambda x: x ** 2
print(square(5)) # 25
0 تعليق