MyMisPal
→ دروس Python
الدرس 7 من 13

القوائم (Lists)

PYTHON
courses = ["SQL", "Python", "ERP"]
print(courses[0], courses[-1], len(courses))
courses.append("Networks")
print(courses)

الترقيم يبدأ من 0، و-1 آخر عنصر. append تضيف في الآخر، وremove تحذف قيمة، وlen تعطيك العدد.

PYTHON
grades = [88, 92, 75, 64, 99]
print(max(grades), min(grades), sum(grades) / len(grades))
print(sorted(grades))
print(grades[1:3])
PYTHON
for c in ["SQL", "Python", "ERP"]:
    print("-", c)

List comprehension طريقة مختصرة تبني فيها قائمة جديدة من قائمة موجودة مع شرط:

PYTHON
grades = [88, 92, 75, 64, 99]
passed = [g for g in grades if g >= 70]
curved = [g + 5 for g in grades]
print(passed)
print(curved)

تمارين

تمرين 1

عندك sales = [1200, 450, 980, 3100, 760]. اطبع المجموع ثم أعلى قيمة، كل وحدة في سطر.

تمرين 2

من نفس القائمة، اطبع قائمة جديدة فيها المبيعات اللي أكبر من 900 فقط.

لقيت خطأ في هذي الصفحة، أو عندك فكرة تحسّن الموقع؟