PART 02 | الجزء الثاني

From MATLAB to Python: Getting Started with Kaggle

من MATLAB إلى Python: البداية مع Kaggle

Programming in Power | البرمجة في الطاقة

10-Question Quiz | الاختبار

Starting point | نقطة البداية

Students already know the programming foundations from Programming I using MATLAB. We do not restart programming; we transfer the same computational thinking to Python and a cloud notebook.

يفترض أن الطالب تعلم في «البرمجة في الطاقة 1» أساسيات البرمجة باستخدام MATLAB. لذلك لا نبدأ البرمجة من الصفر، بل ننقل التفكير البرمجي نفسه إلى Python وبيئة Notebook سحابية.

Where this course leads | إلى أين يقود هذا المقرر؟

This course builds the Python and data foundation for Artificial Intelligence, followed by Applications of AI in Power Systems. Today we learn to run and verify simple engineering code; AI agents come later.
يبني هذا المقرر أساس Python والتعامل مع البيانات لمادة الذكاء الاصطناعي، ثم مادة تطبيقات الذكاء الاصطناعي في أنظمة الطاقة. نتعلم اليوم تشغيل كود هندسي بسيط والتحقق منه، وتأتي دراسة وكلاء الذكاء الاصطناعي في مرحلة لاحقة.

Learning outcomes | مخرجات التعلم

By the end, the student can explain Python/Kaggle, create and save a Notebook on a phone, distinguish Code and Markdown cells, run/edit cells, use variables/lists/NumPy arrays, represent complex numbers, and make a first engineering plot.

بنهاية الجزء يستطيع الطالب تعريف Python وKaggle، إنشاء Notebook وحفظه من الهاتف، التمييز بين خلايا Code وMarkdown، تشغيل الخلايا وتعديلها، استخدام المتغيرات والقوائم ومصفوفات NumPy، تمثيل الأعداد العقدية، وإنشاء أول رسم هندسي.

Learning stages | مراحل التعلم

Stage 1 | المرحلة 1

MATLAB → Python and the three-course pathway
الانتقال من MATLAB إلى Python والمسار بين المقررات الثلاثة

Stage 2 | المرحلة 2

Language, library, notebook and execution order
اللغة والمكتبة والدفتر وترتيب التنفيذ

Stage 3 | المرحلة 3

Account registration and mobile notebook setup
إنشاء الحساب وتجهيز دفتر البرمجة على الهاتف

Stage 4 | المرحلة 4

First DC power calculation, line by line
أول حساب للاستطاعة المستمرة مع شرح كل سطر

Stage 5 | المرحلة 5

MATLAB comparison, variables, units and indexing
المقارنة مع MATLAB والمتغيرات والوحدات والفهرسة

Stage 6 | المرحلة 6

Numerical arrays and the complex-current example
المصفوفات العددية ومثال التيار العقدي

Stage 7 | المرحلة 7

Plot and interpret the physical trend
الرسم وتفسير الاتجاه الفيزيائي

Stage 8 | المرحلة 8

Save, reopen and the 10-question assessment
الحفظ وإعادة الفتح واختبار من 10 أسئلة

This part can span several classes and remains open to development throughout the semester. Follow these stages at the pace agreed with the teacher.
يمكن أن يمتد هذا الجزء لعدة محاضرات ويبقى قابلا للتطوير خلال الفصل الدراسي. تابع هذه المراحل وفق وتيرة التعلم المتفق عليها مع المدرس.

1. What changes—and what does not? | ما الذي يتغير وما الذي لا يتغير؟

Engineering problem → equations → algorithm → code → result → engineering interpretation

MATLAB and Python are tools. Ohm's law, complex power and network equations do not change when the language changes.

MATLAB وPython أداتان. قانون أوم والاستطاعة العقدية ومعادلات الشبكة لا تتغير بتغير لغة البرمجة. الذي يتغير أساساً هو طريقة كتابة التعليمات والمكتبات وبيئة التنفيذ.

2. What is Python? | ما هي Python؟

Python is a general-purpose programming language. For numerical engineering work we will mainly use NumPy for numerical arrays and mathematics, Matplotlib for plotting, and later Pandas for tabular/time-series data.

Python لغة برمجة عامة. في التطبيقات الهندسية العددية سنستخدم بصورة أساسية NumPy للمصفوفات والحسابات الرياضية، وMatplotlib للرسم، ولاحقاً Pandas للبيانات الجدولية والسلاسل الزمنية.
import numpy as np
import matplotlib.pyplot as plt
Explain each line | شرح كل سطر
Line | التعليمةExplanation | الشرح
import numpy as np
Load NumPy and call it np.
استدع NumPy باسم مختصر هو np.
import matplotlib.pyplot as plt
Load the plotting module with the short name plt.
استدع وحدة الرسم بالاسم المختصر plt.

2A. What does “library” mean? | ماذا تعني كلمة «مكتبة»؟

A library is a collection of ready-made programming tools. Think of an engineering toolbox: instead of writing every numerical method yourself, you call a suitable tool. Python is the language; NumPy is a numerical library; Matplotlib is a plotting library; Kaggle is where we run the code.
المكتبة هي مجموعة أدوات برمجية جاهزة، مثل صندوق أدوات المهندس. بدلا من كتابة كل عملية عددية بنفسك، تستعمل أداة مناسبة جاهزة. Python هي اللغة، وNumPy مكتبة حسابات عددية، وMatplotlib مكتبة رسوم، أما Kaggle فهي منصة تشغيل الكود.
import numpy as np: load NumPy and give it the short name np. np.array(...) calls the array tool inside NumPy. The dot means “use a tool belonging to this library”.
import numpy as np: استدعاء NumPy وإعطاؤها الاسم المختصر np. التعليمة np.array(...) تستدعي أداة تكوين مصفوفة داخل المكتبة. النقطة تعني استخدام أداة تابعة لهذه المكتبة.
import matplotlib.pyplot as plt: load the plotting module and use plt as its short name. These aliases are conventions, not new programming languages.
import matplotlib.pyplot as plt: استدعاء وحدة الرسم وإعطاؤها الاسم المختصر plt. هذه الاختصارات أسماء متعارف عليها وليست لغات برمجة جديدة.

3. What is Kaggle Notebook? | ما هو Kaggle Notebook؟

A Notebook combines executable code, output, text and figures in one document. Kaggle runs the code in the cloud, so the phone acts mainly as the interface.

الـNotebook يجمع الكود القابل للتنفيذ والنتائج والنصوص والأشكال في وثيقة واحدة. ينفذ Kaggle الكود على بيئة سحابية، ولذلك يعمل الهاتف أساساً كواجهة للكتابة والتشغيل ومشاهدة النتائج.

Code cell | خلية Code: executable Python.   Markdown cell | خلية Markdown: explanation, equations and notes.

Run order matters | ترتيب التشغيل مهم

A Code cell is one editable block of instructions; output appears underneath. A Markdown cell explains the work and does not execute Python. Cells share memory during a running session. Run definitions before calculations. After restarting, run the required cells from top to bottom.
خلية Code كتلة قابلة للتعديل تحتوي التعليمات، وتظهر نتائجها تحتها. خلية Markdown تشرح العمل ولا تنفذ Python. تتشارك الخلايا الذاكرة ضمن الجلسة الجارية. شغل تعريف المتغيرات قبل الحسابات، وبعد إعادة التشغيل نفذ الخلايا المطلوبة من الأعلى إلى الأسفل.

3A. Create a Kaggle account: 10 steps | إنشاء حساب Kaggle في 10 خطوات

A mobile guide for students in Syria. Start with your usual connection without assuming a VPN is necessary. We have not verified registration or SMS delivery from a Syrian network; availability and additional checks can differ. If blocked, use the troubleshooting section below.
دليل من الهاتف لطلاب سوريا. ابدأ باتصالك المعتاد دون افتراض ضرورة VPN. لم نتحقق من إنشاء الحساب أو وصول SMS من شبكة سورية؛ وقد تختلف الإتاحة والتحققات الإضافية. عند التعثر، راجع قسم المشكلات أدناه.
The illustrations are simplified teaching diagrams, not screenshots of the current Kaggle interface. Follow the labels on the live site; positions and wording may change.
الأشكال التالية رسوم تعليمية مبسطة، وليست لقطات شاشة للواجهة الحالية لـKaggle. اتبع أسماء الأزرار في الموقع الفعلي؛ فقد تتغير مواقعها وصياغتها.

1Prepare your phone | جهز هاتفك

Use Chrome on Android or Safari on iPhone, with an internet connection and an email inbox you can open. We use the browser; you do not need to install Python or a Kaggle app for this activity.
استخدم Chrome على Android أو Safari على iPhone، مع اتصال بالإنترنت وبريد إلكتروني تستطيع فتح رسائله. نعمل من المتصفح؛ لا تحتاج إلى تثبيت Python أو تطبيق Kaggle لهذا النشاط.

2Open registration | افتح صفحة التسجيل

Open Kaggle registration. If the page shows Sign In, choose Register. If you already have an account, sign in and continue at step 7.
افتح صفحة التسجيل في Kaggle. إذا ظهرت Sign In فاختر Register. إذا كان لديك حساب بالفعل، سجل الدخول وتابع من الخطوة 7.

3Choose Google or email | اختر Google أو البريد الإلكتروني

Register with Google: select your own Google account and read the permission screen before continuing. Register with Email: create a separate Kaggle password using your own email. Choose one route, rather than creating two accounts.
Register with Google: اختر حساب Google الخاص بك واقرأ شاشة الموافقة قبل المتابعة. Register with Email: أنشئ كلمة مرور خاصة بـKaggle باستخدام بريدك. اختر مسارا واحدا، ولا تنشئ حسابين.
Choose Google or emailkaggle.com · Choose Google or emailCreate an accountRegister with GoogleRegister with Email
Teaching illustration · Choose Google or email
رسم توضيحي تعليمي · اختر Google أو البريد الإلكتروني

4Enter the requested details | أدخل البيانات المطلوبة

For email registration, enter your real Email Address and a strong Password. Enter Full Name when requested, for example Ahmad Mohammad. Complete any anti-robot check shown, then press Next or the equivalent continuation button. Field order may vary.
عند التسجيل بالبريد اكتب عنوانك الحقيقي في Email Address وكلمة مرور قوية في Password. أدخل Full Name عندما يطلب، مثل Ahmad Mohammad. أكمل اختبار التحقق إن ظهر، ثم اضغط Next أو زر المتابعة المقابل. قد يختلف ترتيب الحقول.
Enter the requested detailskaggle.com · Enter the requested detailsEmail AddressPassword••••••••••Next →
Teaching illustration · Enter the requested details
رسم توضيحي تعليمي · أدخل البيانات المطلوبة

5Confirm your email if requested | أكد البريد إذا طلب منك

Open your email app, find the Kaggle message, and use its verification code or link as instructed. Return to the same registration tab. If the message is missing, check Spam and use Resend email if offered. Do not share the code.
افتح تطبيق البريد وابحث عن رسالة Kaggle، واستخدم رمز التحقق أو الرابط بحسب الرسالة. عد إلى علامة تبويب التسجيل نفسها. إذا لم تصل الرسالة، افحص Spam واستخدم Resend email إن كان متاحا. لا تشارك الرمز.
Confirm your email if requestedkaggle.com · Confirm your email if requestedVerify your emailOpen your inbox → read the codeEnter the code from your emailCheck Spam • Resend email
Teaching illustration · Confirm your email if requested
رسم توضيحي تعليمي · أكد البريد إذا طلب منك

6Complete your profile | أكمل الملف الشخصي

If Complete registration appears, enter your name, check the username and account link, and complete the displayed agreements. A username must be available; your displayed name and username need not be identical. Newsletter subscription is optional when offered.
إذا ظهرت Complete registration، أدخل اسمك وراجع اسم المستخدم ورابط الحساب وأكمل الموافقات الظاهرة. يجب أن يكون اسم المستخدم متاحا؛ وليس شرطا أن يطابق الاسم المعروض. الاشتراك بالنشرة البريدية اختياري عندما يعرض.
Complete your profilekaggle.com · Complete your profileComplete registrationFull NameAhmad MohammadUsername: choose your ownNewsletter: optional
Teaching illustration · Complete your profile
رسم توضيحي تعليمي · أكمل الملف الشخصي

7Check the account, then open Code | تحقق من الحساب ثم افتح Code

Open your profile from the account menu to confirm sign-in. Then open Kaggle Code. Creating an account and being able to run a notebook are separate checks.
افتح ملفك الشخصي من قائمة الحساب للتأكد من تسجيل الدخول، ثم افتح Kaggle Code. إنشاء الحساب وإمكانية تشغيل الدفتر مرحلتان مختلفتان للتحقق.

8Create and name the notebook | أنشئ الدفتر وسمه

Choose New Notebook, use Python and the default CPU; this example needs no GPU. Name it PE2_Lab01_YourName, replacing YourName with your name. If controls are hidden, on Android try Chrome ⋮ → Desktop site; on iPhone use Safari’s page menu → Request Desktop Website. Rotate the phone horizontally if helpful.
اختر New Notebook، واستخدم Python والمعالج CPU الافتراضي؛ هذا المثال لا يحتاج GPU. سم الدفتر PE2_Lab01_YourName واستبدل YourName باسمك. إذا اختفت الأزرار، جرب في Android قائمة Chrome ⋮ ثم Desktop site، وفي iPhone قائمة الصفحة في Safari ثم Request Desktop Website. أدر الهاتف أفقيا إذا ساعد ذلك.
Create and name the notebookkaggle.com · Create and name the notebookCode+ New NotebookChoose Python • CPUChrome ⋮ → Desktop site
Teaching illustration · Create and name the notebook
رسم توضيحي تعليمي · أنشئ الدفتر وسمه

9Run your first program | شغل البرنامج الأول

Scroll to the first-power example below, copy its four lines into a Code cell, then press ▶ / Run. Wait for the session to start. The expected output is Power = 24 W. Reading an output printed in this part is different from running your own Kaggle cell.
انتقل إلى مثال الاستطاعة الأول أدناه، وانسخ أسطره الأربعة إلى خلية Code ثم اضغط ▶ أو Run. انتظر بدء جلسة التشغيل. النتيجة المتوقعة Power = 24 W. قراءة النتيجة المعروضة هنا تختلف عن تشغيل الخلية بنفسك على Kaggle.
Run your first programkaggle.com · Run your first programvoltage = 12current = 2power = voltage * currentprint("Power =", power, "W")Power = 24 W
Teaching illustration · Run your first program
رسم توضيحي تعليمي · شغل البرنامج الأول

10Save and reopen | احفظ وأعد الفتح

Use Save Version. Choose Quick Save if available, or follow the save options displayed; Save & Run All also reruns cells. Wait until saving completes. Reopen the notebook from your work/profile and check the code. Rerun it if saved outputs are not displayed. Keep coursework private unless the teacher asks you to share.
استخدم Save Version. اختر Quick Save إن كان متاحا، أو اتبع خيارات الحفظ المعروضة؛ خيار Save & Run All يعيد أيضا تشغيل الخلايا. انتظر اكتمال الحفظ. افتح الدفتر مجددا من أعمالك أو ملفك وتحقق من الكود، وأعد تشغيله إذا لم تظهر النتائج المحفوظة. اجعل عملك خاصا ما لم يطلب المدرس مشاركته.
Save and reopenkaggle.com · Save and reopenPE2_Lab01_YourNameSave VersionQuick Save, if availableReopen → check your work
Teaching illustration · Save and reopen
رسم توضيحي تعليمي · احفظ وأعد الفتح

3B. First Python program: DC power | البرنامج الأول: استطاعة حمل مستمر

Model: a DC load has voltage 12 V and current 2 A. Predict the power by hand, then run the same calculation on Kaggle.
النموذج: حمل مستمر جهده 12 V ويمر فيه تيار 2 A. احسب الاستطاعة يدويا أولا، ثم نفذ الحساب نفسه على Kaggle.
P = U × I = 12 × 2 = 24 W
voltage = 12
current = 2
power = voltage * current
print("Power =", power, "W")
Line | التعليمةExplanation | الشرح
voltage = 12
Store the DC voltage, 12 V, in voltage.
خزن الجهد المستمر 12 V في المتغير voltage.
current = 2
Store the current, 2 A, in current.
خزن التيار 2 A في المتغير current.
power = voltage * current
Multiply voltage by current and store 24 in power.
اضرب الجهد بالتيار وخزن القيمة 24 في power.
print("Power =", power, "W")
Print the label, value and unit; spaces are inserted between items.
اطبع النص والقيمة والوحدة؛ تفصل مسافات بين العناصر.

Expected output | النتيجة المتوقعة

Power = 24 W
Use English letters, digits 0–9, straight quotes " and the decimal point . when typing Python. Units are explained in the model; Python does not attach volts or amperes automatically to these numbers.
استخدم الأحرف الإنجليزية والأرقام 0–9 وعلامة الاقتباس المستقيمة " والنقطة العشرية . عند كتابة Python. الوحدات محددة في النموذج؛ ولا يربط Python الأعداد تلقائيا بالفولت أو الأمبير.
= stores a value in a variable; it does not ask whether two values are equal. * means multiplication. Text inside quotes is printed as text. Commas separate the items passed to print.
الرمز = يسند قيمة إلى متغير، ولا يختبر تساوي قيمتين. الرمز * يعني الضرب. النص بين علامتي اقتباس يطبع كنص، وتفصل الفواصل بين العناصر المرسلة إلى print.
Try it: change current to 3, predict 36 W, and run again. Then return it to 2 and rerun. Add a Markdown cell containing your name and the hand calculation.
جرب: غير current إلى 3، وتوقع 36 W ثم شغل مجددا. أعده إلى 2 وشغل الخلية مرة أخرى. أضف خلية Markdown تحتوي اسمك والحساب اليدوي.

3C. Troubleshooting in Syria | معالجة المشكلات للطلاب في سوريا

Email confirmation, phone verification and identity verification are different processes. A working profile alone does not prove the notebook can execute. Use the 24 W result to check the complete learning workflow.
تأكيد البريد والتحقق بالهاتف والتحقق من الهوية عمليات مختلفة. ظهور الملف الشخصي لا يثبت وحده إمكانية تنفيذ الدفتر. استخدم نتيجة 24 W للتحقق من اكتمال مسار التطبيق.
Problem | المشكلةAction | التصرف
The site does not load
الموقع لا يفتح
Refresh once, check your connection, try another available network, and record the exact error.
حدث الصفحة مرة، وافحص اتصالك وجرب شبكة أخرى متاحة وسجل رسالة الخطأ كما هي.
Email message is missing
رسالة البريد لم تصل
Check the address and Spam; request another message only after waiting.
راجع عنوان البريد ومجلد Spam، وانتظر قبل طلب رسالة جديدة.
New Notebook is hidden
زر New Notebook غير ظاهر
Try Desktop site and landscape orientation; check that you are signed in.
جرب وضع سطح المكتب والوضع الأفقي، وتأكد من تسجيل الدخول.
Phone Verification appears
ظهر طلب تحقق بالهاتف
Use your real number only if Syria is supported in the country selector. Country code +963 replaces the leading local 0. Do not repeat +963 if selected separately.
استخدم رقمك الحقيقي إذا كانت سوريا مدعومة بقائمة الدول. الرمز +963 يحل محل الصفر المحلي الأول. لا تكرر +963 إذا اخترته منفصلا.
SMS does not arrive
رمز SMS لا يصل
Avoid repeated rapid requests. Record the step and ask the teacher or Kaggle support. Delivery to Syrian numbers is not guaranteed here.
لا تكرر الطلب بسرعة. سجل المرحلة وأبلغ المدرس أو دعم Kaggle. لا يضمن هذا الدليل وصول الرمز للأرقام السورية.
Identity or region check fails
فشل التحقق من الهوية أو المنطقة
Keep the error text with private data removed and report it. Do not submit another person’s identity or create repeated accounts.
احتفظ بنص الخطأ بعد إخفاء البيانات الخاصة وأبلغ عنه. لا تستخدم هوية شخص آخر ولا تنشئ حسابات متكررة.
The cell does not execute
الخلية لا تنفذ
Wait for the session, look for an error or verification request, confirm the cell is Code, and run it again only after identifying the cause.
انتظر بدء الجلسة، واقرأ الخطأ أو طلب التحقق، وتأكد أن الخلية Code، ولا تكرر التشغيل قبل تحديد السبب.
If access fails, continue the hand calculation and code reading, then report the blocking step. Do not mark the programming task complete until you actually run it. Hide your email, phone, password and verification codes in screenshots.
إذا تعذر الوصول، تابع الحساب اليدوي وقراءة الكود ثم أبلغ عن المرحلة المتعثرة. لا تعتبر المهمة البرمجية مكتملة قبل تنفيذ الكود فعليا. أخف بريدك ورقم هاتفك وكلمة المرور ورموز التحقق من لقطات الشاشة.

4. MATLAB → Python syntax bridge | جسر الصياغة من MATLAB إلى Python

Concept | المفهومMATLABPython / NumPy
Vector | متجهx = 1:5;x=np.arange(1,6)
Complex | عقديZ=30+1j*15;Z=30+1j*15
Magnitude | المطالabs(Z)abs(Z)
Angle | الزاويةangle(Z)np.angle(Z)
Display | إظهارdisp(x)print(x)
Comment | تعليق% text# text

Python indentation matters in blocks such as loops and functions; semicolons are normally unnecessary.

المسافات البادئة مهمة في Python داخل الحلقات والدوال، بينما لا نحتاج عادة إلى الفاصلة المنقوطة في نهاية السطر.

5. Variables, types and units | المتغيرات والأنواع والوحدات

V = 20e3          # float
R = 30            # integer
name = "Load A"   # string
connected = True  # boolean
print(type(V), type(name))
Explain each line | شرح كل سطر
Line | التعليمةExplanation | الشرح
V = 20e3 # float
20e3 means 20 × 10³ = 20000 V.
20e3 تعني 20 × 10³ = 20000 V.
R = 30 # integer
Set resistance to 30 Ω.
اجعل المقاومة 30 Ω.
name = "Load A" # string
Store text; quotation marks identify a string.
خزن نصا؛ علامتا الاقتباس تعرفان سلسلة نصية.
connected = True # boolean
Store a Boolean value; True must start with a capital T.
خزن قيمة منطقية؛ يجب كتابة True بحرف T كبير.
print(type(V), type(name))
Show the types: float for V, str for name.
اعرض النوعين: float للمتغير V وstr للاسم name.

Engineering rule: choose a consistent unit system. Converting kV→V and MW→W explicitly prevents many errors.

قاعدة هندسية: استخدم نظام وحدات متسقاً. التحويل الصريح من kV إلى V ومن MW إلى W يمنع كثيراً من الأخطاء العددية.

6. Lists and NumPy arrays | القوائم ومصفوفات NumPy

import numpy as np
P_list = [5, 10, 20, 15]
P = np.array(P_list)
print(P)
print(P.mean())
print(P.max())
Explain each line | شرح كل سطر
Line | التعليمةExplanation | الشرح
import numpy as np
Load NumPy and call it np.
استدع NumPy باسم مختصر هو np.
P_list = [5, 10, 20, 15]
Create a Python list of four load powers; assume kW.
أنشئ قائمة Python بأربع استطاعات أحمال؛ نفترض الوحدة kW.
P = np.array(P_list)
Convert the list into a NumPy numerical array.
حول القائمة إلى مصفوفة NumPy عددية.
print(P)
Display the four values.
اعرض القيم الأربع.
print(P.mean())
Calculate the arithmetic mean: 12.5 kW.
احسب المتوسط الحسابي: 12.5 kW.
print(P.max())
Find the largest value: 20 kW.
جد القيمة الكبرى: 20 kW.

A NumPy array supports efficient element-wise numerical operations and will be central in later load and network calculations.

تسمح مصفوفة NumPy بإجراء العمليات العددية عنصرًا بعنصر بكفاءة، وستكون أساسية لاحقاً في حسابات الأحمال والشبكات.

6A. Python operators engineers will use | معاملات Python التي سيستخدمها المهندس

MATLAB experience makes most operators familiar, but students should explicitly distinguish arithmetic, comparison and element-wise numerical operations before later power-system calculations.

تجعل خبرة MATLAB معظم المعاملات مألوفة، لكن يجب أن يميّز الطالب بوضوح بين العمليات الحسابية والمقارنات والعمليات العددية على عناصر المصفوفة قبل الانتقال لاحقاً إلى حسابات نظم القدرة.
Operation | العمليةPythonExample | مثال
Add / جمع+P1 + P2
Multiply / ضرب*V * I
Divide / قسمة/V / Z
Power / أس**V**2
Compare / مقارنة> < ==V > 20e3

Important MATLAB difference: Python uses ** for exponentiation, not ^.

فرق مهم عن MATLAB: يستخدم Python الرمز ** لرفع العدد إلى قوة، وليس ^.

6B. Indexing: MATLAB versus Python | الفهرسة: MATLAB مقابل Python

MATLAB indexing starts at 1, while Python indexing starts at 0. This small difference causes many mistakes when students first move between the two languages.

تبدأ فهرسة MATLAB من 1، بينما تبدأ فهرسة Python من 0. هذا الفرق الصغير سبب شائع للأخطاء عند الانتقال لأول مرة بين اللغتين.
import numpy as np
P = np.array([5, 10, 20, 15])
print(P[0])   # first value = 5
print(P[2])   # third value = 20
print(P[-1])  # last value = 15
Explain each line | شرح كل سطر
Line | التعليمةExplanation | الشرح
import numpy as np
Load NumPy and call it np.
استدع NumPy باسم مختصر هو np.
P = np.array([5, 10, 20, 15])
Build a numerical array from the four values.
كون مصفوفة عددية من القيم الأربع.
print(P[0]) # first value = 5
Index 0 selects the first value, 5.
الفهرس 0 يختار القيمة الأولى 5.
print(P[2]) # third value = 20
Index 2 selects the third value, 20.
الفهرس 2 يختار القيمة الثالثة 20.
print(P[-1]) # last value = 15
Index −1 selects the last value, 15.
الفهرس −1 يختار القيمة الأخيرة 15.
في المثال السابق تمثل P[0] العنصر الأول، وتمثل P[-1] العنصر الأخير. سنستخدم ذلك كثيراً عند قراءة بيانات الأحمال.

7. Complex numbers for power engineering | الأعداد العقدية في هندسة الطاقة

This is a single-phase AC example. V = 20 kV is the RMS phasor voltage across the impedance at reference angle 0°. It is not a three-phase line-to-line voltage.
هذا مثال تيار متناوب أحادي الطور. V = 20 kV تمثل فازور الجهد الفعال على طرفي الممانعة عند زاوية مرجعية 0°، ولا تمثل جهد الخط بين طورين في شبكة ثلاثية الطور.
Z=R+jX     I=V/Z
import numpy as np
V = 20e3
R = 30
X = 15
Z = R + 1j*X
I = V/Z

print("Z =", Z)
print("|I| =", abs(I))
print("angle(I) =", np.angle(I, deg=True), "deg")
Explain each line | شرح كل سطر
Line | التعليمةExplanation | الشرح
import numpy as np
Load NumPy and call it np.
استدع NumPy باسم مختصر هو np.
V = 20e3
20e3 means 20 × 10³ = 20000 V.
20e3 تعني 20 × 10³ = 20000 V.
R = 30
Set resistance to 30 Ω.
اجعل المقاومة 30 Ω.
X = 15
Set inductive reactance to 15 Ω.
اجعل المفاعلة الحثية 15 Ω.
Z = R + 1j*X
1j is the imaginary unit; Z becomes 30 + j15 Ω.
1j هي الوحدة التخيلية؛ تصبح الممانعة 30 + j15 Ω.
I = V/Z
Divide the voltage phasor by impedance to obtain current.
اقسم فازور الجهد على الممانعة للحصول على التيار.
print("Z =", Z)
Display the complex impedance.
اعرض الممانعة العقدية.
print("|I| =", abs(I))
abs computes current magnitude, about 596.3 A.
abs تحسب مطال التيار، نحو 596.3 A.
print("angle(I) =", np.angle(I, deg=True), "deg")
Calculate and print the current angle in degrees; deg=True selects degrees.
احسب واطبع زاوية التيار بالدرجات؛ deg=True تختار الدرجات.

Expected magnitude: about 596.3 A. The negative current angle is consistent with an inductive impedance.

المطال المتوقع للتيار يقارب 596.3 A. وإشارة زاوية التيار السالبة منطقية لأن الممانعة حثية، فيتأخر التيار عن الجهد.

7A. Hand check before trusting code | تحقق يدوي قبل الوثوق بالكود

For R=30 Ω and X=15 Ω:

|Z| = √(R²+X²) = √(30²+15²) = 33.54 Ω
|I| = V/|Z| = 20000/33.54 ≈ 596.3 A

The impedance angle is tan⁻¹(15/30)=26.565°, therefore with V at 0°, the current angle is approximately −26.565°.

عند R=30 Ω وX=15 Ω يكون مطال الممانعة 33.54 Ω، ومنه مطال التيار نحو 596.3 A. زاوية الممانعة 26.565°، لذلك عندما يكون الجهد مرجعاً عند 0° تكون زاوية التيار نحو −26.565°. هذه المقارنة اليدوية وسيلة أساسية لاكتشاف أخطاء الكود.
📱 MINI CHALLENGE

Predict → Run → Explain | توقّع ← شغّل ← فسّر

Without changing V, test three cases: (R,X)=(30,5), (30,15), (30,20). Before pressing Run, write your expected ordering of current magnitudes. Then verify it numerically.

دون تغيير V اختبر الحالات الثلاث: (R,X)=(30,5)، (30,15)، (30,20). قبل الضغط على Run اكتب ترتيباً متوقعاً لمقادير التيار، ثم تحقق عددياً من توقعك.
V = 20e3
cases = [(30,5), (30,15), (30,20)]
for R, X in cases:
    I = V/(R + 1j*X)
    print(R, X, abs(I))
Explain each line | شرح كل سطر
Line | التعليمةExplanation | الشرح
V = 20e3
20e3 means 20 × 10³ = 20000 V.
20e3 تعني 20 × 10³ = 20000 V.
cases = [(30,5), (30,15), (30,20)]
A list of three (R, X) pairs.
قائمة تحتوي ثلاثة أزواج (R, X).
for R, X in cases:
For each pair, assign R and X; the colon starts the loop body.
لكل زوج اسند قيمتي R وX؛ النقطتان تبدأان جسم الحلقة.
I = V/(R + 1j*X)
Indented: calculate a current for each impedance.
داخل الحلقة مع مسافة بادئة: احسب تيارا لكل ممانعة.
print(R, X, abs(I))
Indented: show R, X and the current magnitude for that case.
داخل الحلقة: اعرض R وX ومطال التيار للحالة.

The goal is not memorizing the loop; it is connecting parameter change to physical response.

الهدف ليس حفظ الحلقة، بل ربط تغير المعامل بالاستجابة الفيزيائية للنظام.

8. First plot with Matplotlib | أول رسم باستخدام Matplotlib

import numpy as np
import matplotlib.pyplot as plt
V = 20e3
R = 30
X = np.arange(5, 21)
I = V / (R + 1j*X)

plt.plot(X, np.abs(I), marker="o")
plt.xlabel("Reactance X (ohm)")
plt.ylabel("Current magnitude |I| (A)")
plt.title("Load current versus reactance")
plt.grid(True)
plt.show()
Explain each line | شرح كل سطر
Line | التعليمةExplanation | الشرح
import numpy as np
Load NumPy and call it np.
استدع NumPy باسم مختصر هو np.
import matplotlib.pyplot as plt
Load the plotting module with the short name plt.
استدع وحدة الرسم بالاسم المختصر plt.
V = 20e3
20e3 means 20 × 10³ = 20000 V.
20e3 تعني 20 × 10³ = 20000 V.
R = 30
Set resistance to 30 Ω.
اجعل المقاومة 30 Ω.
X = np.arange(5, 21)
Create 5, 6, …, 20; the upper bound 21 is excluded.
أنشئ القيم 5، 6، …، 20؛ الحد الأعلى 21 غير مشمول.
I = V / (R + 1j*X)
NumPy calculates one complex current for each X value.
تحسب NumPy تيارا عقديا لكل قيمة X.
plt.plot(X, np.abs(I), marker="o")
Plot X horizontally and current magnitude vertically; o adds circular markers.
ارسم X أفقيا ومطال التيار شاقوليا؛ o يضيف علامات دائرية.
plt.xlabel("Reactance X (ohm)")
Label the horizontal axis and its unit.
سم المحور الأفقي ووحدته.
plt.ylabel("Current magnitude |I| (A)")
Label the vertical axis in amperes.
سم المحور الشاقولي بالأمبير.
plt.title("Load current versus reactance")
Give the plot a meaningful title.
أعط الرسم عنوانا ذا معنى.
plt.grid(True)
Show grid lines for easier reading.
أظهر خطوط الشبكة لتسهيل القراءة.
plt.show()
Display the figure.
اعرض الشكل.

Do not treat the graph as decoration: read the axes and units, describe the trend, then connect it to |I|=V/√(R²+X²).

الرسم ليس للزينة: اقرأ المحاور والوحدات، صف اتجاه المنحني، ثم اربطه بالعلاقة |I|=V/√(R²+X²).

8A. How to read an engineering graph | كيف نقرأ الرسم الهندسي؟

Every engineering plot should answer four questions: What is on the x-axis? What is on the y-axis? What are the units? What physical relationship explains the trend?

يجب أن يجيب كل رسم هندسي عن أربعة أسئلة: ماذا يمثل المحور الأفقي؟ ماذا يمثل المحور الشاقولي؟ ما الوحدات؟ وما العلاقة الفيزيائية التي تفسر اتجاه المنحني؟

Here, increasing X increases |Z| and therefore decreases |I| for fixed V and R. The graph is a visual representation of the equation, not an independent result.

هنا تؤدي زيادة X إلى زيادة |Z| وبالتالي انخفاض |I| عند ثبات V وR. لذلك فالرسم تمثيل بصري للمعادلة وليس نتيجة منفصلة عنها.

9. Errors are part of programming | الأخطاء جزء من البرمجة

Syntax error: invalid Python form. Name error: variable not defined. Logic/engineering error: code runs but the model is wrong.

Syntax error: صياغة Python غير صحيحة. Name error: استخدام متغير غير معرف. Logic/engineering error: يعمل الكود لكن النموذج أو العلاقة الهندسية خاطئة؛ وهذا النوع أخطر لأنه قد يعطي رقماً يبدو صحيحاً.
# Runs, but physically wrong:
Z = R + X
I = V * Z
Explain each line | شرح كل سطر
Line | التعليمةExplanation | الشرح
Z = R + X
Deliberately wrong: this loses the imaginary component of impedance.
خطأ مقصود: يفقد الجزء التخيلي للممانعة.
I = V * Z
Deliberately wrong: Ohm’s law requires division by Z.
خطأ مقصود: قانون أوم يتطلب القسمة على Z.

10. Save and reopen | الحفظ وإعادة الفتح

Rename the Notebook clearly and verify that it appears in your Kaggle work. Reopen it before leaving the session. This becomes your digital lab notebook.

سمِّ الـNotebook بوضوح وتحقق من ظهوره ضمن أعمالك في Kaggle، ثم أعد فتحه قبل نهاية الجلسة. سيصبح هذا الملف دفتر المخبر الرقمي للطالب.

11. ChatGPT: assistant, not autopilot | ChatGPT: مساعد وليس بديلاً

“Explain this Python error in simple terms. Tell me what to check, but do not write the full corrected solution.”
«اشرح خطأ Python هذا ببساطة، وقل لي ما الذي يجب أن أتحقق منه، لكن لا تكتب الحل المصحح كاملاً.»

Always return to Kaggle, make the correction yourself, run it, and verify the engineering meaning.

عد دائماً إلى Kaggle، نفّذ التصحيح بنفسك، شغّل الكود، ثم تحقق من المعنى الهندسي للنتيجة.

Your first-lab checklist | قائمة إنجاز المخبر الأول

Check each item only after doing it. Progress is stored on this browser when local storage is available; it is not sent to the teacher.
ضع علامة على كل مهمة بعد تنفيذها فقط. يحفظ التقدم في هذا المتصفح عندما تتاح مساحة التخزين المحلية؛ ولا يرسل إلى المدرس.

Completed | المكتمل: 0/6

Keep a screenshot of the output and your account link for the lab. If blocked, report: the step number, exact error, phone/browser, and whether you used your usual connection or a VPN. Do not include passwords or verification codes.
احتفظ بصورة النتيجة ورابط حسابك للمخبر. إذا تعثرت، أبلغ عن رقم الخطوة ونص الخطأ والهاتف والمتصفح وهل استخدمت اتصالك المعتاد أو VPN. لا ترسل كلمات المرور أو رموز التحقق.

Before Lab 01 | قبل المخبر 01

You should now have a Kaggle account, one saved Notebook, and experience running/editing Python cells from the phone.

يجب أن يكون لديك الآن حساب Kaggle وNotebook محفوظ، وأن تكون قد شغلت وعدّلت خلايا Python فعلياً من الهاتف.

Open Lab 01 | فتح المخبر 01 →

Quick assessment | اختبار سريع

1. A Kaggle Notebook can contain:
يمكن أن يحتوي Kaggle Notebook على:
2. Python comment begins with:
يبدأ التعليق في Python بالرمز:
3. np.arange(1,6) ends at:
تنتهي np.arange(1,6) عند:
4. Python complex unit is:
الوحدة التخيلية في Python هي:
5. NumPy is mainly used here for:
نستخدم NumPy هنا أساساً من أجل:
6. Matplotlib is used for:
تستخدم Matplotlib من أجل:
7. A Markdown cell is mainly for:
تستخدم خلية Markdown أساساً من أجل:
8. If X rises with V,R fixed, |I|:
إذا زادت X مع ثبات V وR فإن |I|:
9. A code cell is executed using:
يتم تنفيذ خلية الكود باستخدام:
10. After AI suggests a fix, you should:
بعد اقتراح الذكاء الاصطناعي للتصحيح يجب:
Score | النتيجة: 0/10

References | المراجع

  1. A. Gabash, Flexible Optimal Operations of Energy Supply Networks with Renewable Energy Generation and Battery Storage. Saarbrücken, Germany: Südwestdeutscher Verlag für Hochschulschriften, 2014.
  2. S. J. Russell and P. Norvig, Artificial Intelligence: A Modern Approach, 4th Global ed. Harlow, U.K.: Pearson Education Limited, 2022.
  3. A. Gabash, “Energy Market Transition and Climate Change: A Review of TSOs–DSOs C+++ Framework from 1800 to Present,” Energies, vol. 16, no. 17, Art. no. 6139, 2023, doi: 10.3390/en16176139.
  4. S. J. D. Prince, Understanding Deep Learning. Cambridge, MA, USA: The MIT Press, 2023.
  5. A. Gabash, AI Applications in Electrical Power Systems, Version 1.0, 2026. [Online]. Available: https://aoussgabash.com