From MATLAB to Python: Getting Started with Kaggle
Programming in Power | البرمجة في الطاقة
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.
Where this course leads | إلى أين يقود هذا المقرر؟
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.
Learning stages | مراحل التعلم
Stage 1 | المرحلة 1
Stage 2 | المرحلة 2
Stage 3 | المرحلة 3
Stage 4 | المرحلة 4
Stage 5 | المرحلة 5
Stage 6 | المرحلة 6
Stage 7 | المرحلة 7
Stage 8 | المرحلة 8
1. What changes—and what does not? | ما الذي يتغير وما الذي لا يتغير؟
MATLAB and Python are tools. Ohm's law, complex power and network equations do not change when the language changes.
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.
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? | ماذا تعني كلمة «مكتبة»؟
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.
Code cell | خلية Code: executable Python. Markdown cell | خلية Markdown: explanation, equations and notes.
Run order matters | ترتيب التشغيل مهم
3A. Create a Kaggle account: 10 steps | إنشاء حساب Kaggle في 10 خطوات
2Open registration | افتح صفحة التسجيل
3Choose Google or email | اختر Google أو البريد الإلكتروني
4Enter the requested details | أدخل البيانات المطلوبة
Ahmad Mohammad. Complete any anti-robot check shown, then press Next or the equivalent continuation button. Field order may vary.Ahmad Mohammad. أكمل اختبار التحقق إن ظهر، ثم اضغط Next أو زر المتابعة المقابل. قد يختلف ترتيب الحقول.5Confirm your email if requested | أكد البريد إذا طلب منك
6Complete your profile | أكمل الملف الشخصي
7Check the account, then open Code | تحقق من الحساب ثم افتح Code
8Create and name the notebook | أنشئ الدفتر وسمه
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.PE2_Lab01_YourName واستبدل YourName باسمك. إذا اختفت الأزرار، جرب في Android قائمة Chrome ⋮ ثم Desktop site، وفي iPhone قائمة الصفحة في Safari ثم Request Desktop Website. أدر الهاتف أفقيا إذا ساعد ذلك.9Run your first program | شغل البرنامج الأول
Power = 24 W. Reading an output printed in this part is different from running your own Kaggle cell.Power = 24 W. قراءة النتيجة المعروضة هنا تختلف عن تشغيل الخلية بنفسك على Kaggle.10Save and reopen | احفظ وأعد الفتح
3B. First Python program: DC power | البرنامج الأول: استطاعة حمل مستمر
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
" and the decimal point . when typing Python. Units are explained in the model; Python does not attach volts or amperes automatically to these numbers." والنقطة العشرية . عند كتابة 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.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 | معالجة المشكلات للطلاب في سوريا
| 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، ولا تكرر التشغيل قبل تحديد السبب. |
4. MATLAB → Python syntax bridge | جسر الصياغة من MATLAB إلى Python
| Concept | المفهوم | MATLAB | Python / 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.
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.
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.
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.
| Operation | العملية | Python | Example | مثال |
|---|---|---|
| Add / جمع | + | P1 + P2 |
| Multiply / ضرب | * | V * I |
| Divide / قسمة | / | V / Z |
| Power / أس | ** | V**2 |
| Compare / مقارنة | > < == | V > 20e3 |
Important MATLAB difference: Python uses ** for exponentiation, not ^.
** لرفع العدد إلى قوة، وليس ^.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.
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 | الأعداد العقدية في هندسة الطاقة
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.
7A. Hand check before trusting code | تحقق يدوي قبل الوثوق بالكود
For R=30 Ω and X=15 Ω:
The impedance angle is tan⁻¹(15/30)=26.565°, therefore with V at 0°, the current angle is approximately −26.565°.
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 = 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²).
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.
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.
# 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.
11. ChatGPT: assistant, not autopilot | ChatGPT: مساعد وليس بديلاً
«اشرح خطأ Python هذا ببساطة، وقل لي ما الذي يجب أن أتحقق منه، لكن لا تكتب الحل المصحح كاملاً.»
Always return to Kaggle, make the correction yourself, run it, and verify the engineering meaning.
Your first-lab checklist | قائمة إنجاز المخبر الأول
Completed | المكتمل: 0/6
Before Lab 01 | قبل المخبر 01
You should now have a Kaggle account, one saved Notebook, and experience running/editing Python cells from the phone.
Quick assessment | اختبار سريع
References | المراجع
- 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.
- S. J. Russell and P. Norvig, Artificial Intelligence: A Modern Approach, 4th Global ed. Harlow, U.K.: Pearson Education Limited, 2022.
- 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.
- S. J. D. Prince, Understanding Deep Learning. Cambridge, MA, USA: The MIT Press, 2023.
- A. Gabash, AI Applications in Electrical Power Systems, Version 1.0, 2026. [Online]. Available: https://aoussgabash.com