logo

دروس بايثون | لغة البرمجة بايثون

بايثون هي لغة برمجة مستخدمة على نطاق واسع وتقدم العديد من الميزات والمزايا الفريدة مقارنة بلغات مثل جافا و سي ++. يشرح برنامج Python التعليمي لدينا بشكل كامل أساسيات Python والمفاهيم المتقدمة، بدءًا من التثبيت، عبارات شرطية والحلقات وهياكل البيانات المضمنة والبرمجة الموجهة للكائنات والمولدات ومعالجة الاستثناءات وPython RegEx والعديد من المفاهيم الأخرى. تم تصميم هذا البرنامج التعليمي للمبتدئين والمهنيين العاملين.

في أواخر الثمانينات، جويدو فان روسوم حلمت بتطوير بايثون. النسخة الأولى من تم إصدار بايثون 0.9.0 في عام 1991 . منذ صدورها، بدأت بايثون تكتسب شعبية. وفقًا للتقارير، أصبحت لغة بايثون الآن لغة البرمجة الأكثر شعبية بين المطورين بسبب متطلباتها العالية في مجال التكنولوجيا.

ما هي بايثون

بايثون هي لغة برمجة ذات أغراض عامة ومكتوبة ديناميكيًا وعالية المستوى ومترجمة ومفسرة ومجمعة للقمامة وموجهة للكائنات بحتة وتدعم البرمجة الإجرائية والموجهة للكائنات والوظيفية.

مميزات لغة بايثون:

    سهل الاستخدام والقراءة -بناء جملة بايثون واضح وسهل القراءة، مما يجعلها لغة مثالية لكل من المبرمجين المبتدئين وذوي الخبرة. هذه البساطة يمكن أن تؤدي إلى تطوير أسرع وتقليل فرص الأخطاء.تمت كتابته ديناميكيًا- يتم تحديد أنواع البيانات للمتغيرات أثناء وقت التشغيل. لا نحتاج إلى تحديد نوع بيانات المتغير أثناء كتابة الأكواد.مستوى عال- لغة عالية المستوى تعني كودًا يمكن للإنسان قراءته.تم تجميعها وتفسيرها- يتم تجميع كود بايثون أولاً في كود بايت، ثم يتم تفسيره سطرًا تلو الآخر. عندما نقوم بتنزيل Python في نموذج نظامنا ORG نقوم بتنزيل التطبيق الافتراضي لـ Python المعروف باسم CPython. يعتبر CPython ملتزمًا ومفسرًا على حد سواء.تم جمع القمامة- تتم إدارة تخصيص الذاكرة وإلغاء التخصيص تلقائيًا. لا يحتاج المبرمجون على وجه التحديد إلى إدارة الذاكرة.بحتة وجوه المنحى- يشير إلى كل شيء ككائن، بما في ذلك الأرقام والسلاسل.التوافق عبر الأنظمة الأساسية- يمكن تثبيت Python بسهولة على أنظمة التشغيل Windows وmacOS وتوزيعات Linux المختلفة، مما يسمح للمطورين بإنشاء برامج تعمل عبر أنظمة تشغيل مختلفة.مكتبة قياسية غنية- تأتي بايثون مع العديد من المكتبات القياسية التي توفر وحدات ووظائف جاهزة للاستخدام لمختلف المهام، بدءًا من تطوير الشبكة و معالجة البيانات ل التعلم الالي و الشبكات .مفتوح المصدر- بايثون هي لغة برمجة مفتوحة المصدر ومجانية. يتم استخدامه في العديد من القطاعات والتخصصات نتيجة لذلك.

بايثون لديها الكثير الأصول على شبكة الإنترنت , مشاريع مفتوحة المصدر ، و مجتمع نابض بالحياة . إن تعلم اللغة والعمل معًا في المشاريع والمساهمة في نظام Python البيئي، كلها أمور أصبحت سهلة للغاية بالنسبة للمطورين.

نظرًا لإطارها اللغوي المباشر، تعد لغة بايثون أسهل في الفهم وكتابة التعليمات البرمجية. وهذا يجعلها لغة برمجة رائعة للمبتدئين. بالإضافة إلى ذلك، فهو يساعد المبرمجين المتمرسين في كتابة تعليمات برمجية واضحة وخالية من الأخطاء.

لدى Python العديد من مكتبات الطرف الثالث التي يمكن استخدامها لتسهيل وظائفها. تغطي هذه المكتبات العديد من المجالات، على سبيل المثال، تطوير الويب، والحوسبة العلمية، وتحليل البيانات، والمزيد.

جافا مقابل بايثون

تعد Python خيارًا ممتازًا للتطوير السريع ومهام البرمجة النصية. بينما تؤكد Java على نظام الكتابة القوي والبرمجة الموجهة للكائنات.

وفيما يلي بعض البرامج الأساسية التي توضح الاختلافات الرئيسية بينهما.

طباعة 'مرحبا بالعالم'

رمز بايثون:

 print('Hello World)' 

في بايثون، هو سطر واحد من التعليمات البرمجية. يتطلب بناء جملة بسيطًا لطباعة 'Hello World'

كود جافا:

 public class HelloWorld { public static void main(String[] args) { System.out.println('Hello, World!'); } } 

في Java، نحتاج إلى الإعلان عن الفئات وهياكل الطرق وأشياء أخرى كثيرة.

في حين أن كلا البرنامجين يعطيان نفس النتيجة، يمكننا ملاحظة اختلاف بناء الجملة في بيان الطباعة.

كيفية العثور على الأشياء المخفية على أندرويد
  • من السهل تعلم وكتابة التعليمات البرمجية في بايثون. أثناء وجودك في Java، يتطلب الأمر المزيد من التعليمات البرمجية لأداء مهام معينة.
  • تتم كتابة لغة Python ديناميكيًا، مما يعني أننا لا نحتاج إلى الإعلان عن المتغير، بينما تتم كتابة لغة Java إحصائيًا، مما يعني أننا نحتاج إلى الإعلان عن نوع المتغير.
  • تعتبر Python مناسبة لمختلف المجالات مثل علوم البيانات والتعلم الآلي وتطوير الويب والمزيد. في حين أن Java مناسبة لتطوير الويب وتطوير تطبيقات الهاتف المحمول (Android) والمزيد.

بناء جملة بايثون الأساسي

لا يوجد استخدام للأقواس المتعرجة أو الفواصل المنقوطة في لغة البرمجة بايثون. وهي لغة شبيهة باللغة الإنجليزية. لكن بايثون تستخدم المسافة البادئة لتحديد كتلة من التعليمات البرمجية. المسافة البادئة ليست سوى إضافة مسافة بيضاء قبل البيان عند الحاجة إليه.

على سبيل المثال -

 def func(): statement 1 statement 2 ………………… ………………… statement N 

في المثال أعلاه، تنتمي العبارات التي لها نفس المستوى إلى اليمين إلى الدالة. بشكل عام، يمكننا استخدام أربع مسافات بيضاء لتحديد المسافة البادئة.

بدلاً من الفاصلة المنقوطة كما هو مستخدم في اللغات الأخرى، تنهي بايثون بياناتها بحرف NewLine.

بايثون هي لغة حساسة لحالة الأحرف، مما يعني أنه يتم التعامل مع الأحرف الكبيرة والصغيرة بشكل مختلف. على سبيل المثال، يعد 'name' و'Name' متغيرين مختلفين في لغة Python.

في بايثون، يمكن إضافة التعليقات باستخدام الرمز '#'. أي نص مكتوب بعد الرمز '#' يعتبر تعليقًا ويتم تجاهله من قبل المترجم الفوري. هذه الخدعة مفيدة لإضافة ملاحظات إلى الكود أو تعطيل كتلة الكود مؤقتًا. كما أنه يساعد في فهم الكود بشكل أفضل من قبل بعض المطورين الآخرين.

'لو' و'خلافًا لذلك' و'من أجل' و'بينما' و'محاولة' و'باستثناء' و'أخيرًا' هي بعض الكلمات الرئيسية المحجوزة في بايثون والتي لا يمكن استخدامها كأسماء للمتغيرات. تستخدم هذه المصطلحات في اللغة لأسباب معينة ولها معاني ثابتة. إذا كنت تستخدم هذه الكلمات الأساسية، فقد يتضمن الكود الخاص بك أخطاء، أو قد يرفضها المترجم كمتغيرات جديدة محتملة.

تاريخ بايثون

تم إنشاء بايثون على يد جويدو فان روسوم . في أواخر الثمانينيات، بدأ المبرمج الهولندي غيدو فان روسوم، العمل على لغة بايثون أثناء وجوده في Centrum Wiskunde & Informatica (CWI) في هولندا. لقد أراد إنشاء خليفة لـ لغة برمجة ABC سيكون ذلك سهل القراءة وفعالاً.

في فبراير 1991، تم إصدار أول نسخة عامة من بايثون، الإصدار 0.9.0. كان هذا بمثابة الميلاد الرسمي لـ بايثون كمشروع مفتوح المصدر . سميت اللغة بهذا الاسم نسبة إلى المسلسل الكوميدي البريطاني ' مونتي بايثون الطائر السيرك '.

لقد مر تطوير بايثون بعدة مراحل. في يناير 1994، تم إصدار Python 1.0 كلغة برمجة مستقرة وقابلة للاستخدام. تضمن هذا الإصدار العديد من الميزات التي لا تزال موجودة في بايثون اليوم.

من التسعينيات إلى العقد الأول من القرن الحادي والعشرين اكتسبت بايثون شعبية بسبب بساطتها وسهولة قراءتها وتعدد استخداماتها. في أكتوبر 2000، تم إصدار بايثون 2.0 . قدم Python 2.0 فهم القوائم وجمع البيانات المهملة ودعم Unicode.

في ديسمبر 2008، تم إصدار بايثون 3.0. قدم Python 3.0 العديد من التغييرات غير المتوافقة مع الإصدارات السابقة لتحسين إمكانية قراءة التعليمات البرمجية وقابلية الصيانة.

خلال العقد الأول من القرن الحادي والعشرين، زادت شعبية بايثون، خاصة في مجالات مثل التعلم الآلي وتطوير الويب. إن نظامها البيئي الغني من المكتبات والأطر جعلها مفضلة لدى المطورين.

ال تأسست مؤسسة برمجيات بايثون (PSF) في عام 2001 لتعزيز وحماية وتطوير لغة برمجة بايثون ومجتمعها.

لماذا تعلم بايثون؟

توفر بايثون العديد من الميزات المفيدة للمبرمج. هذه الميزات تجعلها اللغة الأكثر شعبية واستخدامًا على نطاق واسع. لقد أدرجنا أدناه بعض الميزات الأساسية لبيثون.

    سهل الاستخدام والتعلم:تتميز لغة Python ببناء جملة بسيط وسهل الفهم، على عكس اللغات التقليدية مثل C وC++ وJava وما إلى ذلك، مما يجعل من السهل على المبتدئين التعلم.اللغة التعبيرية:فهو يسمح للمبرمجين بالتعبير عن المفاهيم المعقدة في بضعة أسطر فقط من التعليمات البرمجية أو يقلل من وقت المطور.اللغة المترجمة:لا تحتاج لغة بايثون إلى تجميع، مما يسمح بالتطوير والاختبار السريع. ويستخدم مترجم بدلا من المترجم.
  • لغة كائنية التوجه : وهو يدعم البرمجة الموجهة للكائنات، مما يجعل كتابة التعليمات البرمجية المعيارية قابلة لإعادة الاستخدام وسهلة.
  • مفتوح المصدر لغة: بايثون مفتوحة المصدر ومجانية الاستخدام والتوزيع والتعديل.توسع:يمكن توسيع لغة Python بوحدات مكتوبة بلغة C أو C++ أو لغات أخرى.تعلم المكتبة القياسية:تحتوي مكتبة بايثون القياسية على العديد من الوحدات والوظائف التي يمكن استخدامها لمهام مختلفة، مثل معالجة السلسلة وبرمجة الويب والمزيد.دعم برمجة واجهة المستخدم الرسومية:توفر بايثون العديد من أطر واجهة المستخدم الرسومية، مثل تكنتر وPyQt، مما يسمح للمطورين بإنشاء تطبيقات سطح المكتب بسهولة.مدمج:يمكن لـ Python أن تتكامل بسهولة مع اللغات والتقنيات الأخرى، مثل C/C++، وJava، و. شبكة.قابل للتضمين:يمكن تضمين كود Python في تطبيقات أخرى كلغة برمجة نصية.تخصيص الذاكرة الديناميكية:تقوم لغة Python تلقائيًا بإدارة تخصيص الذاكرة، مما يسهل على المطورين كتابة برامج معقدة دون القلق بشأن إدارة الذاكرة.مجموعة واسعة من المكتبات والأطر:تمتلك بايثون مجموعة كبيرة من المكتبات والأطر، مثل NumPy وPandas وDjango وFlask، والتي يمكن استخدامها لحل مجموعة واسعة من المشكلات.براعه:بايثون هي لغة عالمية في مجالات مختلفة مثل تطوير الويب، والتعلم الآلي، وعلوم البيانات، والذكاء الاصطناعي، وتطوير الويب، والمزيد.إقبال كبير:مع تزايد الطلب على الأتمتة والتحول الرقمي، تتزايد الحاجة إلى مطوري لغة بايثون. تبحث العديد من الصناعات عن مطوري Python المهرة للمساعدة في بناء البنية التحتية الرقمية الخاصة بهم.زيادة الإنتاجية:تمتلك لغة Python بنية بسيطة ومكتبات قوية يمكنها مساعدة المطورين على كتابة التعليمات البرمجية بشكل أسرع وأكثر كفاءة. يمكن أن يؤدي ذلك إلى زيادة الإنتاجية وتوفير الوقت للمطورين والمؤسسات.البيانات الضخمة والتعلم الآلي:أصبحت لغة بايثون هي اللغة المفضلة للبيانات الضخمة والتعلم الآلي. أصبحت لغة Python شائعة بين علماء البيانات ومهندسي التعلم الآلي من خلال مكتبات مثل NumPy وPandas وScikit-learn وTensorFlow والمزيد.

أين يتم استخدام بايثون؟

بايثون هي لغة برمجة شعبية ذات أغراض عامة، ويتم استخدامها في كل المجالات التقنية تقريبًا. المجالات المختلفة لاستخدام بايثون موضحة أدناه.

    علم البيانات:يعد علم البيانات مجالًا واسعًا، وتعد لغة بايثون لغة مهمة لهذا المجال بسبب بساطتها وسهولة استخدامها وتوافر مكتبات قوية لتحليل البيانات وتصورها مثل NumPy وPandas وMatplotlib.تطبيقات سطح المكتب:باي كيوت و تكنتر هي مكتبات مفيدة يمكن استخدامها في واجهة المستخدم الرسومية - تطبيقات سطح المكتب المستندة إلى واجهة المستخدم الرسومية. هناك لغات أفضل لهذا المجال، ولكن يمكن استخدامها مع لغات أخرى لعمل التطبيقات.التطبيقات المعتمدة على وحدة التحكم:تُستخدم Python أيضًا بشكل شائع لإنشاء تطبيقات تعتمد على سطر الأوامر أو وحدة التحكم نظرًا لسهولة استخدامها ودعمها للميزات المتقدمة مثل إعادة توجيه الإدخال / الإخراج والأنابيب.تطبيقات الهاتف الجوال:على الرغم من أن لغة Python لا تُستخدم بشكل شائع لإنشاء تطبيقات الهاتف المحمول، إلا أنه لا يزال من الممكن دمجها مع أطر عمل مثل Kivy أو BeeWare لإنشاء تطبيقات الهاتف المحمول عبر الأنظمة الأساسية.تطوير البرمجيات:تعتبر لغة بايثون من أفضل لغات صناعة البرمجيات. تتوافق Python بسهولة مع كل من البرامج الصغيرة الحجم إلى البرامج الكبيرة الحجم.
  • الذكاء الاصطناعي : الذكاء الاصطناعي هو تقنية ناشئة، وتعتبر بايثون لغة مثالية للذكاء الاصطناعي والتعلم الآلي بسبب توفر مكتبات قوية مثل TensorFlow وKeras وPyTorch.
  • تطبيقات الويب:تُستخدم لغة Python بشكل شائع في تطوير الويب على الواجهة الخلفية باستخدام أطر عمل مثل Django وFlask وعلى الواجهة الأمامية باستخدام أدوات مثل جافا سكريبت HTML و CSS .تطبيقات المؤسسة:يمكن استخدام Python لتطوير تطبيقات المؤسسات واسعة النطاق بميزات مثل الحوسبة الموزعة والشبكات والمعالجة المتوازية.تطبيقات CAD ثلاثية الأبعاد:يمكن استخدام لغة Python لتطبيقات التصميم بمساعدة الكمبيوتر (CAD) ثلاثية الأبعاد من خلال مكتبات مثل Blender.التعلم الالي:تُستخدم لغة Python على نطاق واسع في التعلم الآلي نظرًا لبساطتها وسهولة استخدامها وتوافر مكتبات قوية للتعلم الآلي.تطبيقات الرؤية الحاسوبية أو معالجة الصور:يمكن استخدام لغة بايثون في تطبيقات رؤية الكمبيوتر ومعالجة الصور من خلال مكتبات قوية مثل OpenCV وScikit-image.التعرف على الكلام:يمكن استخدام Python لتطبيقات التعرف على الكلام من خلال مكتبات مثل SpeechRecognition وPyAudio.الحوسبة العلمية:توفر مكتبات مثل NumPy وSciPy وPandas إمكانات حوسبة رقمية متقدمة لمهام مثل تحليل البيانات والتعلم الآلي والمزيد.تعليم:إن تركيب لغة بايثون سهل التعلم وتوافر العديد من الموارد يجعلها لغة مثالية لتدريس البرمجة للمبتدئين.اختبارات:تُستخدم لغة Python لكتابة الاختبارات الآلية، وتوفير أطر عمل مثل اختبارات الوحدة وpytest التي تساعد في كتابة حالات الاختبار وإنشاء التقارير.الألعاب:لدى بايثون مكتبات مثل Pygame، والتي توفر منصة لتطوير الألعاب باستخدام بايثون.إنترنت الأشياء:تُستخدم Python في إنترنت الأشياء لتطوير البرامج النصية والتطبيقات لأجهزة مثل Raspberry Pi وArduino وغيرها.الشبكات:تُستخدم Python في الشبكات لتطوير البرامج النصية والتطبيقات لأتمتة الشبكة ومراقبتها وإدارتها.
  • ديف أوبس : يتم استخدام Python على نطاق واسع في DevOps للأتمتة والبرمجة النصية لإدارة البنية التحتية وإدارة التكوين وعمليات النشر.
  • تمويل:لدى بايثون مكتبات مثل Pandas وScikit-learn وStatsmodels للنمذجة والتحليل المالي.الصوت والموسيقى:لدى بايثون مكتبات مثل Pyaudio، والذي يستخدم لمعالجة الصوت والتوليف والتحليل، وMusic21، والذي يستخدم لتحليل الموسيقى وتوليدها.كتابة البرامج النصية:تُستخدم لغة Python لكتابة البرامج النصية المساعدة لأتمتة المهام مثل عمليات الملفات، وتجميع الويب، وأطر عمل ومكتبات Python الشائعة.

    تمتلك بايثون مجموعة واسعة من المكتبات والأطر المستخدمة على نطاق واسع في مجالات مختلفة مثل التعلم الآلي، والذكاء الاصطناعي، وتطبيقات الويب، وما إلى ذلك. ونحدد بعض الأطر والمكتبات الشائعة في بايثون على النحو التالي.

    وظيفة طباعة بايثون ().

    تُستخدم وظيفة Python print () لعرض الإخراج إلى وحدة التحكم أو المحطة الطرفية. يسمح لنا بعرض النص والمتغيرات والبيانات الأخرى بتنسيق يمكن قراءته بواسطة الإنسان.

    بناء الجملة:

    طباعة (كائن (كائنات)، sep = فاصل، نهاية = نهاية، ملف = ملف، تدفق = تدفق)

    يأخذ وسيطة واحدة أو أكثر مفصولة بفاصلة (،) ويضيف 'سطرًا جديدًا' في النهاية افتراضيًا.

    حدود:

    • الكائن (الكائنات) - سيتم أولاً تحويل العدد الذي تريد عرضه من البيانات إلى سلسلة وطباعته على وحدة التحكم.
    • sep - يفصل الكائنات بفاصل تم تمريره، القيمة الافتراضية = ' '.
    • end - ينهي السطر بحرف السطر الجديد
    • file - كائن ملف بأسلوب الكتابة، القيمة الافتراضية = sys.stdout

    مثال:

     # Displaying a string print('Hello, World!') # Displaying multiple values name = 'Aman' age = 21 print('Name:', name, 'Age:', age) # Printing variables and literals x = 5 y = 7 print('x =', x, 'y =', y, 'Sum =', x + y) # Printing with formatting percentage = 85.75 print('Score: {:.2f}%'.format(percentage)) 

    انتاج:

     Hello, World! Name: Aman Age: 21 X = 5 y = 7 Sum = 12 Score: 85.75% 

    في هذا المثال، يتم استخدام عبارة print لطباعة قيم السلسلة والأعداد الصحيحة والعائمة بتنسيق يمكن قراءته بواسطة الإنسان.

    يمكن استخدام عبارة الطباعة لتصحيح الأخطاء وتسجيلها وتوفير المعلومات للمستخدم.

    عبارات بايثون الشرطية

    تساعدنا البيانات الشرطية على تنفيذ كتلة معينة لشرط معين. في هذا البرنامج التعليمي، سوف نتعلم كيفية استخدام التعبير الشرطي لتنفيذ مجموعة مختلفة من العبارات. توفر بايثون كلمات رئيسية if و else لإعداد الشروط المنطقية. ال إليف يتم استخدام الكلمة الأساسية أيضًا كبيان شرطي.

    رمز المثال لبيان if..else

     x = 10 y = 5 if x > y: print('x is greater than y') else: print('y is greater than or equal to x') 

    انتاج:

     x is greater than y 

    في الكود أعلاه، لدينا متغيرين، x وy، مع 10 و5 على التوالي. ثم استخدمنا عبارة if..else للتحقق مما إذا كانت x أكبر من y أو العكس. إذا كان الشرط الأول صحيحا، فسيتم طباعة العبارة 'x أكبر من y'. إذا كان الشرط الأول خاطئًا، فسيتم طباعة العبارة 'y أكبر من أو يساوي x' بدلاً من ذلك.

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

    سنتعرف على هذا بمزيد من التفصيل في المقالة الإضافية لبرنامج Python التعليمي.

    حلقات بايثون

    في بعض الأحيان قد نحتاج إلى تغيير تدفق البرنامج. قد يلزم تكرار تنفيذ تعليمات برمجية معينة عدة مرات. ولهذا الغرض، توفر لغات البرمجة حلقات متنوعة قادرة على تكرار بعض التعليمات البرمجية المحددة عدة مرات. خذ بعين الاعتبار البرنامج التعليمي التالي لفهم العبارات بالتفصيل.

    بايثون للحلقة

     fruits = ['apple', 'banana', 'cherry'] for x in fruits: print(x, end=' ') 

    انتاج:

     apple banana cherry 

    بايثون أثناء الحلقة

     i = 1 while i<5: print(i, end=" " ) i +="1" < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 </pre> <p>In the above example code, we have demonstrated using two types of loops in Python - For loop and While loop.</p> <p>The For loop is used to iterate over a sequence of items, such as a list, tuple, or string. In the example, we defined a list of fruits and used a for loop to print each fruit, but it can also be used to print a range of numbers.</p> <p>The While loop repeats a code block if the specified condition is true. In the example, we have initialized a variable i to 1 and used a while loop to print the value of i until it becomes greater than or equal to 6. The i += 1 statement is used to increment the value of i in each iteration.</p> <p>We will learn about them in the tutorial in detail.</p> <h2>Python Data Structures</h2> <p> <strong>Python offers four built-in data structures:</strong>  <strong>lists</strong>  ,  <strong>tuples</strong>  ,  <strong>sets</strong>  , and  <strong>dictionaries</strong>  that allow us to store data in an efficient way. Below are the commonly used data structures in Python, along with example code:</p> <h3>1. Lists </h3> <ul> <li>Lists are <strong>ordered collections</strong> of data elements of different data types.</li> <li>Lists are <strong>mutable</strong> meaning a list can be modified anytime.</li> <li>Elements can be <strong>accessed using indices</strong> .</li> <li>They are defined using square bracket &apos; <strong>[]</strong> &apos;.</li> </ul> <p> <strong>Example:</strong> </p> <pre> # Create a list fruits = [&apos;apple&apos;, &apos;banana&apos;, &apos;cherry&apos;] print(&apos;fuirts[1] =&apos;, fruits[1]) # Modify list fruits.append(&apos;orange&apos;) print(&apos;fruits =&apos;, fruits) num_list = [1, 2, 3, 4, 5] # Calculate sum sum_nums = sum(num_list) print(&apos;sum_nums =&apos;, sum_nums) </pre> <p> <strong>Output:</strong> </p> <pre> fuirts[1] = banana fruits = [&apos;apple&apos;, &apos;banana&apos;, &apos;cherry&apos;, &apos;orange&apos;] sum_nums = 15 </pre> <h3>2. Tuples </h3> <ul> <li>Tuples are also <strong>ordered collections</strong> of data elements of different data types, similar to Lists.</li> <li>Elements can be <strong>accessed using indices</strong> .</li> <li>Tuples are <strong>immutable</strong> meaning Tuples can&apos;t be modified once created.</li> <li>They are defined using open bracket &apos; <strong>()</strong> &apos;.</li> </ul> <p> <strong>Example:</strong> </p> <pre> # Create a tuple point = (3, 4) x, y = point print(&apos;(x, y) =&apos;, x, y) # Create another tuple tuple_ = (&apos;apple&apos;, &apos;banana&apos;, &apos;cherry&apos;, &apos;orange&apos;) print(&apos;Tuple =&apos;, tuple_) </pre> <p> <strong>Output:</strong> </p> <pre> (x, y) = 3 4 Tuple = (&apos;apple&apos;, &apos;banana&apos;, &apos;cherry&apos;, &apos;orange&apos;) </pre> <h3>3. Sets </h3> <ul> <li>Sets are <strong>unordered</strong> collections of immutable data elements of different data types.</li> <li>Sets are <strong>mutable</strong> .</li> <li>Elements can&apos;t be accessed using indices.</li> <li>Sets <strong>do not contain duplicate elements</strong> .</li> <li>They are defined using curly braces &apos; <strong>{}</strong> &apos;</li> </ul> <p> <strong>Example:</strong> </p> <pre> # Create a set set1 = {1, 2, 2, 1, 3, 4} print(&apos;set1 =&apos;, set1) # Create another set set2 = {&apos;apple&apos;, &apos;banana&apos;, &apos;cherry&apos;, &apos;apple&apos;, &apos;orange&apos;} print(&apos;set2 =&apos;, set2) </pre> <p> <strong>Output:</strong> </p> <pre> set1 = {1, 2, 3, 4} set2 = {&apos;apple&apos;, &apos;cherry&apos;, &apos;orange&apos;, &apos;banana&apos;} </pre> <h3>4. Dictionaries </h3> <ul> <li>Dictionary are <strong>key-value pairs</strong> that allow you to associate values with unique keys.</li> <li>They are defined using curly braces &apos; <strong>{}</strong> &apos; with key-value pairs <strong>separated by colons &apos;:&apos;</strong> .</li> <li>Dictionaries are <strong>mutable</strong> .</li> <li>Elements can be accessed using keys.</li> </ul> <p> <strong>Example:</strong> </p> <pre> # Create a dictionary person = {&apos;name&apos;: &apos;Umesh&apos;, &apos;age&apos;: 25, &apos;city&apos;: &apos;Noida&apos;} print(&apos;person =&apos;, person) print(person[&apos;name&apos;]) # Modify Dictionary person[&apos;age&apos;] = 27 print(&apos;person =&apos;, person) </pre> <p> <strong>Output:</strong> </p> <pre> person = {&apos;name&apos;: &apos;Umesh&apos;, &apos;age&apos;: 25, &apos;city&apos;: &apos;Noida&apos;} Umesh person = {&apos;name&apos;: &apos;Umesh&apos;, &apos;age&apos;: 27, &apos;city&apos;: &apos;Noida&apos;} </pre> <p>These are just a few examples of Python&apos;s built-in data structures. Each data structure has its own characteristics and use cases.</p> <h2>Python Functional Programming</h2> <p>This section of the Python tutorial defines some important tools related to functional programming, such as lambda and recursive functions. These functions are very efficient in accomplishing complex tasks. We define a few important functions, such as reduce, map, and filter. Python provides the functools module that includes various functional programming tools. Visit the following tutorial to learn more about functional programming.</p> <p>Recent versions of Python have introduced features that make functional programming more concise and expressive. For example, the &apos;walrus operator&apos;:= allows for inline variable assignment in expressions, which can be useful when working with nested function calls or list comprehensions.</p> <h2>Python Function</h2> <ol class="points"> <li>  <strong>Lambda Function</strong>  - A lambda function is a small, <strong>anonymous function</strong> that can take any number of arguments but can only have one expression. Lambda functions are often used in functional programming to create functions &apos;on the fly&apos; without defining a named function.</li> <li>  <strong>Recursive Function</strong>  - A recursive function is a function that calls itself to solve a problem. Recursive functions are often used in functional programming to perform complex computations or to traverse complex data structures.</li> <li> <a href="/python-map-function"> <strong>Map Function</strong> </a> - The map() function applies a given function to each item of an iterable and returns a new iterable with the results. The input iterable can be a list, tuple, or other.</li> <li> <a href="/python-filter-function"> <strong>Filter Function</strong> </a> - The filter() function returns an iterator from an iterable for which the function passed as the first argument returns True. It filters out the items from an iterable that do not meet the given condition.</li> <li> <a href="/reduce-python"> <strong>Reduce Function</strong> </a> - The reduce() function applies a function of two arguments cumulatively to the items of an iterable from left to right to reduce it to a single value.</li> <li>  <strong>functools Module</strong>  - The functools module in Python provides higher-order functions that operate on other functions, such as partial() and reduce().</li> <li>  <strong>Currying Function</strong>  - A currying function is a function that takes multiple arguments and returns a sequence of functions that each take a single argument.</li> <li>  <strong>Memoization Function</strong>  - Memoization is a technique used in functional programming to cache the results of expensive function calls and return the cached Result when the same inputs occur again.</li> <li>  <strong>Threading Function</strong>  - Threading is a technique used in functional programming to run multiple tasks simultaneously to make the code more efficient and faster.</li> </ol> <h2>Python Modules</h2> <p> Python modules are the program files that contain Python code or functions. Python has two types of modules - User-defined modules and built-in modules. A module the user defines, or our Python code saved with .py extension, is treated as a user-define module.</p> <p>Built-in modules are predefined modules of Python. To use the functionality of the modules, we need to import them into our current working program.</p> <p>Python modules are essential to the language&apos;s ecosystem since they offer reusable code and functionality that can be imported into any Python program. Here are a few examples of several Python modules, along with a brief description of each:</p> <p>  <strong>Math</strong>  : Gives users access to mathematical constants and pi and trigonometric functions.</p> <p>  <strong>Datetime</strong>  : Provides classes for a simpler way of manipulating dates, times, and periods.</p> <p> <a href="/python-os-module"> <strong>OS</strong> </a> : Enables interaction with the base operating system, including administration of processes and file system activities.</p> <p> <a href="/python-random-module"> <strong>Random</strong> </a> : The random function offers tools for generating random integers and picking random items from a list.</p> <p>  <strong>JSON</strong>  : JSON is a data structure that can be encoded and decoded and is frequently used in online APIs and data exchange. This module allows dealing with JSON. <br>  <strong>Re</strong>  : Supports regular expressions, a potent text-search and text-manipulation tool.</p> <p>  <strong>Collections</strong>  : Provides alternative data structures such as sorted dictionaries, default dictionaries, and named tuples.</p> <p>  <strong>NumPy</strong>  : NumPy is a core toolkit for scientific computing that supports numerical operations on arrays and matrices.</p> <p>  <strong>Pandas</strong>  : It provides high-level data structures and operations for dealing with time series and other structured data types.</p> <p>  <strong>Requests</strong>  : Offers a simple user interface for web APIs and performs HTTP requests.</p> <h2>Python File I/O</h2> <p>Files are used to store data in a computer disk. In this tutorial, we explain the built-in file object of Python. We can open a file using Python script and perform various operations such as writing, reading, and appending. There are various ways of opening a file. We are explained with the relevant example. We will also learn to perform read/write operations on binary files.</p> <p> <strong>Python&apos;s file input/output (I/O) system</strong> offers programs to communicate with files stored on a disc. Python&apos;s built-in methods for the file object let us carry out actions like reading, writing, and adding data to files.</p> <p>The <strong>open()</strong> method in Python makes a file object when working with files. The name of the file to be opened and the mode in which the file is to be opened are the two parameters required by this function. The mode can be used according to work that needs to be done with the file, such as &apos; <strong>r</strong> &apos; for reading, &apos; <strong>w</strong> &apos; for writing, or &apos; <strong>a</strong> &apos; for attaching.</p> <p>After successfully creating an object, different methods can be used according to our work. If we want to write in the file, we can use the write() functions, and if you want to read and write both, then we can use the append() function and, in cases where we only want to read the content of the file we can use read() function. Binary files containing data in a binary rather than a text format may also be worked with using Python. Binary files are written in a manner that humans cannot directly understand. The <strong>rb</strong> and <strong>wb</strong> modes can read and write binary data in binary files.</p> <h2>Python Exceptions</h2> <p>An exception can be defined as an unusual condition in a program resulting in an interruption in the flow of the program.</p> <p>Whenever an exception occurs, the program stops the execution, and thus the other code is not executed. Therefore, an exception is the run-time errors that are unable to handle to Python script. An exception is a Python object that represents an error.</p> <p>  <strong>Python Exceptions</strong>  are an important aspect of error handling in Python programming. When a program encounters an unexpected situation or error, it may raise an exception, which can interrupt the normal flow of the program.</p> <p>In Python, exceptions are represented as objects containing information about the error, including its type and message. The most common type of Exception in Python is the Exception class, a base class for all other built-in exceptions.</p> <p>To handle exceptions in Python, we use the <strong>try</strong> and <strong>except</strong> statements. The <strong>try</strong> statement is used to enclose the code that may raise an exception, while the <strong>except</strong> statement is used to define a block of code that should be executed when an exception occurs.</p> <p> <strong>For example, consider the following code:</strong> </p> <pre> try: x = int ( input (&apos;Enter a number: &apos;)) y = 10 / x print (&apos;Result:&apos;, y) except ZeroDivisionError: print (&apos;Error: Division by zero&apos;) except ValueError: print (&apos;Error: Invalid input&apos;) </pre> <p> <strong>Output:</strong> </p> <pre> Enter a number: 0 Error: Division by zero </pre> <p>In this code, we use the try statement to attempt to perform a division operation. If either of these operations raises an exception, the matching except block is executed.</p> <p>Python also provides many built-in exceptions that can be raised in similar situations. Some common built-in exceptions include <strong>IndexError, TypeError</strong> , and <strong>NameError</strong> . Also, we can define our custom exceptions by creating a new class that inherits from the Exception class.</p> <h2>Python CSV</h2> <p>A CSV stands for &apos;comma separated values&apos;, which is defined as a simple file format that uses specific structuring to arrange tabular data. It stores tabular data such as spreadsheets or databases in plain text and has a common format for data interchange. A CSV file opens into the Excel sheet, and the rows and columns data define the standard format.</p> <p>We can use the CSV.reader function to read a CSV file. This function returns a reader object that we can use to repeat over the rows in the CSV file. Each row is returned as a list of values, where each value corresponds to a column in the CSV file.</p> <p> <strong>For example, consider the following code:</strong> </p> <pre> import csv with open(&apos;data.csv&apos;, &apos;r&apos;) as file: reader = csv.reader(file) for row in reader: print(row) </pre> <p>Here, we open the file data.csv in read mode and create a <strong>csv.reader</strong> object using the <strong>csv.reader()</strong> function. We then iterate over the rows in the CSV file using a for loop and print each row to the console.</p> <p>We can use the  <strong>CSV.writer()</strong>  function to write data to a CSV file. It returns a writer object we can use to write rows to the CSV file. We can write rows by calling the <strong>writer ()</strong> method on the writer object.</p> <p> <strong>For example, consider the following code:</strong> </p> <pre> import csv data = [ [&apos;Name&apos;, &apos;Age&apos;, &apos;Country&apos;], [&apos;Alice&apos;, &apos;25&apos;, &apos;USA&apos;], [&apos;Bob&apos;, &apos;30&apos;, &apos;Canada&apos;], [&apos;Charlie&apos;, &apos;35&apos;, &apos;Australia&apos;] ] with open(&apos;data.csv&apos;, &apos;w&apos;) as file: writer = csv.writer(file) for row in data: writer.writerow(row) </pre> <p>In this program, we create a list of lists called data, where each inner list represents a row of data. We then open the file data.csv in write mode and create a <strong>CSV.writer</strong> object using the CSV.writer function. We then iterate over the rows in data using a for loop and write each row to the CSV file using the writer method.</p> <h2>Python Sending Mail</h2> <p>We can send or read a mail using the Python script. Python&apos;s standard library modules are useful for handling various protocols such as PoP3 and IMAP . Python provides the <a href="/python-sending-email-using-smtp">smtplib</a> module for sending emails using SMTP (Simple Mail Transfer Protocol). We will learn how to send mail with the popular email service SMTP from a Python script.</p> <h3>Python Magic Methods</h3> <p>The Python magic method is the special method that adds &apos;magic&apos; to a class. It starts and ends with double underscores, for example,  <strong>_init_</strong>  or  <strong>_str_</strong>  .</p> <p>The built-in classes define many magic methods. The <strong>dir()</strong> function can be used to see the number of magic methods inherited by a class. It has two prefixes and suffix underscores in the method name.</p> <ul> <li>Python magic methods are also known as <strong>dunder methods</strong> , short for &apos; double underscore &apos; methods because their names start and end with a double underscore.</li> <li>  <strong>Magic methods</strong>  are automatically invoked by the Python interpreter in certain situations, such as when an object is created, compared to another object, or printed.</li> <li>Magic methods can be used to customize the behavior of classes, such as defining how objects are compared, converted to strings, or accessed as containers.</li> <li>Some commonly used magic methods include  <strong>init</strong>  for initializing an object, str for converting an object to a string, <strong>eq</strong> for comparing two objects for equality, and  <strong>getitem</strong>  and <strong>setitem</strong> for accessing items in a container object.</li> </ul> <p>For example, the <strong>str</strong> magic method can define how an object should be represented as a string. Here&apos;s an example</p> <pre> class Person: def __init__(self, name, age): self.name = name self.age = age def __str__(self): return f&apos;{self.name} ({self.age})&apos; person = Person(&apos;Vikas&apos;, 22) print(person) </pre> <p> <strong>Output:</strong> </p> <pre> Vikas (22) </pre> <p>In this example, the str method is defined to return a formatted string representation of the Person object with the person&apos;s name and age.</p> <p>Another commonly used magic method is <strong>eq</strong> , which defines how objects should be compared for equality. Here&apos;s an example:</p> <pre> class Point: def __init__(self, x, y): self.x = x self.y = y def __eq__(self, other): return self.x == other.x and self.y == other.y point1 = Point(2, 3) point2 = Point(3, 4) point3 = Point(2, 3) print(point1 == point2) print(point1 == point3) </pre> <p> <strong>Output:</strong> </p> <pre> False True </pre> <p>In this example, the <strong>eq</strong> method is defined to return True if two Point objects have the same x and y coordinates and False otherwise.</p> <h2>Python Oops Concepts</h2> <p>Everything in Python is treated as an object, including integer values, floats, functions, classes, and none. Apart from that, Python supports all oriented concepts. Below is a brief introduction to the Oops concepts of Python.</p> <ul> <li> <a href="/classes-objects-python"> <strong>Classes and Objects</strong> </a> - Python classes are the blueprints of the Object. An object is a collection of data and methods that act on the data.</li> <li> <a href="/python-inheritance"> <strong>Inheritance</strong> </a> - An inheritance is a technique where one class inherits the properties of other classes.</li> <li> <a href="/python-constructor"> <strong>Constructor</strong> </a> - Python provides a special method __init__() which is known as a constructor. This method is automatically called when an object is instantiated.</li> <tr><td>Data Member</td> - A variable that holds data associated with a class and its objects. <li>  <strong>Polymorphism</strong>  - Polymorphism is a concept where an object can take many forms. In Python, polymorphism can be achieved through method overloading and method overriding.</li> </tr><tr><td>Method Overloading</td> - In Python, method overloading is achieved through default arguments, where a method can be defined with multiple parameters. The default values are used if some parameters are not passed while calling the method. <li>  <strong>Method Overriding</strong>  - Method overriding is a concept where a subclass implements a method already defined in its superclass.</li> <li>  <strong>Encapsulation</strong>  - Encapsulation is wrapping data and methods into a single unit. In Python, encapsulation is achieved through access modifiers, such as public, private, and protected. However, Python does not strictly enforce access modifiers, and the naming convention indicates the access level.</li> <li>  <strong>Data Abstraction</strong>  : A technique to hide the complexity of data and show only essential features to the user. It provides an interface to interact with the data. Data abstraction reduces complexity and makes code more modular, allowing developers to focus on the program&apos;s essential features.</li> </tr></ul> <p>To read the Oops concept in detail, visit the following resources.</p> <ul> <li> Python Oops Concepts - In Python, the object-oriented paradigm is to design the program using classes and objects. The object is related to real-word entities such as book, house, pencil, etc. and the class defines its properties and behaviours.</li> <li> <a href="/classes-objects-python">Python Objects and classes</a> - In Python, objects are instances of classes and classes are blueprints that defines structure and behaviour of data.</li> <li> <a href="/python-constructor">Python Constructor</a> - A constructor is a special method in a class that is used to initialize the object&apos;s attributes when the object is created.</li> <li> <a href="/python-inheritance">Python Inheritance</a> - Inheritance is a mechanism in which new class (subclass or child class) inherits the properties and behaviours of an existing class (super class or parent class).</li> <li> Python Polymorphism - Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling different classes to be used interchangeably through a common interface.</li> </ul> <h2>Python Advance Topics</h2> <p>Python includes many advances and useful concepts that help the programmer solve complex tasks. These concepts are given below.</p> <h3> Python Iterator </h3> <p>An iterator is simply an object that can be iterated upon. It returns one Object at a time. It can be implemented using the two special methods,  <strong>__iter__()</strong>  and __next__().</p> <p>Iterators in Python are objects that allow iteration over a collection of data. They process each collection element individually without loading the entire collection into memory.</p> <p>For example, let&apos;s create an iterator that returns the squares of numbers up to a given limit:</p> <pre> def __init__(self, limit): self.limit = limit self.n = 0 def __iter__(self): return self def __next__(self): if self.n <= 2 self.limit: square="self.n" ** self.n +="1" return else: raise stopiteration numbers="Squares(5)" for n in numbers: print(n) < pre> <p> <strong>Output:</strong> </p> <pre> 0 1 4 9 16 25 </pre> <p>In this example, we have created a class Squares that acts as an iterator by implementing the __iter__() and __next__() methods. The __iter__() method returns the Object itself, and the __next__() method returns the next square of the number until the limit is reached.</p> <p>To learn more about the iterators, visit our Python Iterators tutorial.</p> <h3> Python Generators </h3> <p>  <strong>Python generators</strong>  produce a sequence of values <strong>using a yield statement</strong> rather than a return since they are functions that return iterators. Generators terminate the function&apos;s execution while keeping the local state. It picks up right where it left off when it is restarted. Because we don&apos;t have to implement the iterator protocol thanks to this feature, writing iterators is made simpler. Here is an illustration of a straightforward generator function that produces squares of numbers:</p> <pre> # Generator Function def square_numbers(n): for i in range(n): yield i**2 # Create a generator object generator = square_numbers(5) # Print the values generated by the generator for num in generator: print(num) </pre> <p> <strong>Output:</strong> </p> <pre> 0 1 4 9 16 </pre> <h2>Python Modifiers</h2> <p>  <strong>Python Decorators</strong>  are functions used to modify the behaviour of another function. They allow adding functionality to an existing function without modifying its code directly. Decorators are defined using the <strong>@</strong> symbol followed by the name of the decorator function. They can be used for logging, timing, caching, etc.</p> <p>Here&apos;s an example of a decorator function that adds timing functionality to another function:</p> <pre> import time from math import factorial # Decorator to calculate time taken by # the function def time_it(func): def wrapper(*args, **kwargs): start = time.time() result = func(*args, **kwargs) end = time.time() print(f&apos;{func.__name__} took {end-start:.5f} seconds to run.&apos;) return result return wrapper @time_it def my_function(n): time.sleep(2) print(f&apos;Factorial of {n} = {factorial(n)}&apos;) my_function(25) </pre> <p> <strong>Output:</strong> </p> <pre> </pre> <p>In the above example, the time_it decorator function takes another function as an argument and returns a wrapper function. The wrapper function calculates the time to execute the original function and prints it to the console. The @time_it decorator is used to apply the time_it function to the my_function function. When my_function is called, the decorator is executed, and the timing functionality is added.</p> <h2>Python MySQL</h2> <p>Python MySQL is a powerful relational database management system. We must set up the environment and establish a connection to use MySQL with Python. We can create a new database and tables using SQL commands in Python.</p> <ul> <li>  <strong>Environment Setup</strong>  : Installing and configuring MySQL Connector/Python to use Python with MySQL.</li> <li>  <strong>Database Connection</strong>  : Establishing a connection between Python and MySQL database using MySQL Connector/Python.</li> <li>  <strong>Creating New Database</strong>  : Creating a new database in MySQL using Python.</li> <li>  <strong>Creating Tables</strong>  : Creating tables in the MySQL database with Python using SQL commands.</li> <li>  <strong>Insert Operation</strong>  : Insert data into MySQL tables using Python and SQL commands.</li> <li>  <strong>Read Operation</strong>  : Reading data from MySQL tables using Python and SQL commands.</li> <li>  <strong>Update Operation</strong>  : Updating data in MySQL tables using Python and SQL commands.</li> <li>  <strong>Join Operation</strong>  : Joining two or more tables in MySQL using Python and SQL commands.</li> <li>  <strong>Performing Transactions</strong>  : Performing a group of SQL queries as a single unit of work in MySQL using Python.</li> </ul> <p>Other relative points include handling errors, creating indexes, and using stored procedures and functions in MySQL with Python.</p> <h2>Python MongoDB</h2> <p> Python MongoDB is a popular NoSQL database that stores data in JSON-like documents. It is schemaless and provides high scalability and flexibility for data storage. We can use MongoDB with Python using the PyMongo library, which provides a simple and intuitive interface for interacting with MongoDB.</p> <p>Here are some common tasks when working with MongoDB in Python:</p> <ol class="points"> <li>  <strong>Environment Setup</strong>  : Install and configure MongoDB and PyMongo library on your system.</li> <li>  <strong>Database Connection</strong>  : Connect to a MongoDB server using the MongoClient class from PyMongo.</li> <li>  <strong>Creating a new database</strong>  : Use the MongoClient Object to create a new database.</li> <li>  <strong>Creating collections</strong>  : Create collections within a database to store documents.</li> <li>  <strong>Inserting documents</strong>  : Insert new documents into a collection using the insert_one() or insert_many() methods.</li> <li>  <strong>Querying documents</strong>  : Retrieve documents from a collection using various query methods like find_one(), find(), etc.</li> <li>  <strong>Updating documents</strong>  : Modify existing documents in a collection using update_one() or update_many() methods.</li> <li>  <strong>Deleting documents</strong>  : Remove documents from a collection using the delete_one() or delete_many() methods.</li> <li>  <strong>Aggregation</strong>  : Perform aggregation operations like grouping, counting, etc., using the aggregation pipeline framework.</li> <tr><td>Indexing:</td> Improve query performance by creating indexes on fields in collections. </tr></ol> <p>There are many more advanced topics in MongoDB, such as data sharding, replication, and more, but these tasks cover the basics of working with MongoDB in Python.</p> <h2> Python SQLite </h2> <p>Relational databases are built and maintained using Python SQLite, a compact, serverless, self-contained database engine. Its mobility and simplicity make it a popular option for local or small-scale applications. Python has a built-in module for connecting to SQLite databases called SQLite3, enabling developers to work with SQLite databases without difficulties.</p> <p>Various API methods are available through the SQLite3 library that may be used to run SQL queries, insert , select , update , and remove data, as well as get data from tables. Additionally, it allows transactions, allowing programmers to undo changes in case of a problem. Python SQLite is a fantastic option for creating programs that need an embedded database system, including desktop, mobile, and modest-sized web programs. SQLite has become popular among developers for lightweight apps with database functionality thanks to its ease of use, portability, and smooth connection with Python.</p> <h2> Python CGI </h2> <p>  <strong>Python CGI</strong>  is a technology for running scripts through web servers to produce dynamic online content. It offers a communication channel and a dynamic content generation interface for external CGI scripts and the web server. Python CGI scripts may create HTML web pages, handle form input, and communicate with databases. Python CGI enables the server to carry out Python scripts and provide the results to the client, offering a quick and effective approach to creating dynamic online applications.</p> <p>Python CGI scripts may be used for many things, including creating dynamic web pages, processing forms, and interacting with databases. Since Python, a potent and popular programming language, can be utilized to create scripts, it enables a more customized and flexible approach to web creation. Scalable, safe, and maintainable online applications may be created with Python CGI. Python CGI is a handy tool for web developers building dynamic and interactive online applications.</p> <h2> Asynchronous Programming in Python </h2> <p> <strong>Asynchronous programming</strong> is a paradigm for computer programming that enables independent and concurrent operation of activities. It is frequently used in applications like web servers, database software, and network programming, where several tasks or requests must be handled concurrently.</p> <p>Python has asyncio, Twisted, and Tornado among its libraries and frameworks for asynchronous programming. Asyncio, one of these, offers a simple interface for asynchronous programming and is the official asynchronous programming library in Python.</p> <p>Coroutines are functions that may be halted and restarted at specific locations in the code and are utilized by asyncio. This enables numerous coroutines to operate simultaneously without interfering with one another. For constructing and maintaining coroutines, the library offers several classes and methods, including <strong>asyncio.gather(),</strong> <strong>asyncio.wait(),</strong> and <strong>asyncio.create_task().</strong> </p> <p>Event loops, which are in charge of planning and operating coroutines, are another feature of asyncio. By cycling between coroutines in a non-blocking way, the event loop controls the execution of coroutines and ensures that no coroutine blocks another. Additionally, it supports timers and scheduling callbacks, which may be helpful when activities must be completed at specified times or intervals.</p> <h2> Python Concurrency </h2> <p>The term &apos; <strong>concurrency</strong> &apos; describes a program&apos;s capacity to carry out several tasks at once, enhancing the program&apos;s efficiency. Python offers several modules and concurrency-related methods, including asynchronous programming, multiprocessing, and multithreading. While multiprocessing involves running many processes simultaneously on a system, multithreading involves running numerous threads concurrently inside a single process.</p> <p>The <strong>threading module</strong> in Python enables programmers to build multithreading. It offers classes and operations for establishing and controlling threads. Conversely, the multiprocessing module allows developers to design and control processes. Python&apos;s asyncio module provides asynchronous programming support, allowing developers to write non-blocking code that can handle multiple tasks concurrently. Using these techniques, developers can write highperformance, scalable programs that can handle multiple tasks concurrently.</p> <p>Python&apos;s threading module enables the concurrent execution of several threads within a single process, which is helpful for I/O-bound activities.</p> <p>For CPU-intensive operations like image processing or data analysis, multiprocessing modules make it possible to execute numerous processes concurrently across multiple CPU cores.</p> <p>The asyncio module supports asynchronous I/O and permits the creation of single-threaded concurrent code using coroutines for high-concurrency network applications.</p> <p>With libraries like Dask , <a href="/pyspark-tutorial">PySpark</a> , and MPI, Python may also be used for parallel computing. These libraries allow workloads to be distributed across numerous nodes or clusters for better performance.</p> <h2> Web Scrapping using Python </h2> <p>The process of web scraping is used to retrieve data from websites automatically. Various tools and libraries extract data from HTML and other online formats. Python is among the most widely used programming languages for web scraping because of its ease of use, adaptability, and variety of libraries.</p> <p>We must take a few steps to accomplish web scraping using Python. We must first decide which website to scrape and what information to gather. Then, we can submit a request to the website and receive the HTML content using Python&apos;s requests package. Once we have the HTML text, we can extract the needed data using a variety of parsing packages, like <strong>Beautiful Soup and lxml</strong> .</p> <p>We can employ several strategies, like slowing requests, employing user agents, and using proxies, to prevent overburdening the website&apos;s server. It is also crucial to abide by the terms of service for the website and respect its robots.txt file.</p> <p>Data mining, lead creation, pricing tracking, and many more uses are possible for web scraping. However, as unauthorized web scraping may be against the law and unethical, it is essential to utilize it professionally and ethically.</p> <h2>Natural Language Processing (NLP) using Python</h2> <p>A branch of artificial intelligence (AI) called &apos;natural language processing&apos; (NLP) studies how computers and human language interact. Thanks to NLP, computers can now understand, interpret, and produce human language. Due to its simplicity, versatility, and strong libraries like NLTK (Natural Language Toolkit) and spaCy, Python is a well-known programming language for NLP.</p> <p> <strong>For NLP tasks, including tokenization, stemming, lemmatization, part-of-speech tagging, named entity identification, sentiment analysis, and others, NLTK provides a complete library.</strong> It has a variety of corpora (big, organized text collections) for developing and evaluating NLP models. Another well-liked library for NLP tasks is spaCy , which offers quick and effective processing of enormous amounts of text. It enables simple modification and expansion and comes with pre-trained models for various NLP workloads.</p> <p>NLP may be used in Python for various practical purposes, including chatbots, sentiment analysis, text categorization, machine translation, and more. NLP is used, for instance, by chatbots to comprehend and reply to user inquiries in a natural language style. Sentiment analysis, which may be helpful for brand monitoring, customer feedback analysis, and other purposes, employs NLP to categorize text sentiment (positive, negative, or neutral). Text documents are categorized using natural language processing (NLP) into pre-established categories for spam detection, news categorization, and other purposes.</p> <p>Python is a strong and useful tool when analyzing and processing human language. Developers may carry out various NLP activities and create useful apps that can communicate with consumers in natural language with libraries like NLTK and spaCy.</p> <h2>Conclusion:</h2> <p>In this tutorial, we&apos;ve looked at some of Python&apos;s most important features and ideas, including variables, data types, loops, functions, modules, and more. More complex subjects, including web scraping, natural language processing, parallelism, and database connection, have also been discussed. You will have a strong basis to continue learning about Python and its applications using the information you have learned from this lesson.</p> <p>Remember that practicing and developing code is the best method to learn Python. You may find many resources at javaTpoint to support your further learning, including documentation, tutorials, online groups, and more. You can master Python and use it to create wonderful things if you work hard and persist.</p> <h2>Prerequisite</h2> <p>Before learning Python, you must have the basic knowledge of programming concepts.</p> <h2>Audience</h2> <p>Our Python tutorial is designed to help beginners and professionals.</p> <h2>Problem</h2> <p>We assure that you will not find any problem in this Python tutorial. But if there is any mistake, please post the problem in contact form.</p> <hr></=></pre></5:>

    في المثال أعلاه، أوضحنا استخدام نوعين من الحلقات في بايثون - حلقة For وحلقة while.

    يتم استخدام حلقة For للتكرار عبر سلسلة من العناصر، مثل قائمة أو صف أو سلسلة. في المثال، قمنا بتحديد قائمة من الفواكه واستخدمنا حلقة for لطباعة كل فاكهة، ولكن يمكن استخدامها أيضًا لطباعة نطاق من الأرقام.

    تكرر حلقة while مقطع التعليمات البرمجية إذا كان الشرط المحدد صحيحًا. في المثال، قمنا بتهيئة متغير i إلى 1 واستخدمنا حلقة while لطباعة قيمة i حتى تصبح أكبر من أو تساوي 6. يتم استخدام عبارة i += 1 لزيادة قيمة i في كل تكرار .

    سنتعرف عليهم في البرنامج التعليمي بالتفصيل.

    هياكل بيانات بايثون

    تقدم بايثون أربعة هياكل بيانات مدمجة: القوائم , الصفوف , مجموعات ، و القواميس التي تسمح لنا بتخزين البيانات بطريقة فعالة. فيما يلي هياكل البيانات شائعة الاستخدام في بايثون، بالإضافة إلى رمز المثال:

    1. القوائم

    • القوائم هي مجموعات مرتبة عناصر البيانات من أنواع البيانات المختلفة.
    • القوائم هي متقلب وهذا يعني أن القائمة يمكن تعديلها في أي وقت.
    • يمكن أن تكون العناصر الوصول إليها باستخدام المؤشرات .
    • يتم تعريفها باستخدام قوس مربع ' [] '.

    مثال:

     # Create a list fruits = [&apos;apple&apos;, &apos;banana&apos;, &apos;cherry&apos;] print(&apos;fuirts[1] =&apos;, fruits[1]) # Modify list fruits.append(&apos;orange&apos;) print(&apos;fruits =&apos;, fruits) num_list = [1, 2, 3, 4, 5] # Calculate sum sum_nums = sum(num_list) print(&apos;sum_nums =&apos;, sum_nums) 

    انتاج:

     fuirts[1] = banana fruits = [&apos;apple&apos;, &apos;banana&apos;, &apos;cherry&apos;, &apos;orange&apos;] sum_nums = 15 

    2. الصفوف

    • الصفوف هي أيضا مجموعات مرتبة لعناصر البيانات من أنواع البيانات المختلفة، المشابهة للقوائم.
    • يمكن أن تكون العناصر الوصول إليها باستخدام المؤشرات .
    • الصفوف هي غير قابل للتغيير مما يعني أنه لا يمكن تعديل الصفوف بمجرد إنشائها.
    • يتم تعريفها باستخدام قوس مفتوح ' () '.

    مثال:

     # Create a tuple point = (3, 4) x, y = point print(&apos;(x, y) =&apos;, x, y) # Create another tuple tuple_ = (&apos;apple&apos;, &apos;banana&apos;, &apos;cherry&apos;, &apos;orange&apos;) print(&apos;Tuple =&apos;, tuple_) 

    انتاج:

     (x, y) = 3 4 Tuple = (&apos;apple&apos;, &apos;banana&apos;, &apos;cherry&apos;, &apos;orange&apos;) 

    3 مجموعات

    • مجموعات هي غير مرتبة مجموعات من عناصر البيانات غير القابلة للتغيير من أنواع البيانات المختلفة.
    • مجموعات هي متقلب .
    • لا يمكن الوصول إلى العناصر باستخدام المؤشرات.
    • مجموعات لا تحتوي على عناصر مكررة .
    • ويتم تعريفها باستخدام الأقواس المتعرجة ' {} '

    مثال:

     # Create a set set1 = {1, 2, 2, 1, 3, 4} print(&apos;set1 =&apos;, set1) # Create another set set2 = {&apos;apple&apos;, &apos;banana&apos;, &apos;cherry&apos;, &apos;apple&apos;, &apos;orange&apos;} print(&apos;set2 =&apos;, set2) 

    انتاج:

     set1 = {1, 2, 3, 4} set2 = {&apos;apple&apos;, &apos;cherry&apos;, &apos;orange&apos;, &apos;banana&apos;} 

    4. القواميس

    • القاموس هي أزواج القيمة الرئيسية التي تسمح لك بربط القيم بمفاتيح فريدة.
    • ويتم تعريفها باستخدام الأقواس المتعرجة ' {} ' مع أزواج القيمة الرئيسية مفصولة بنقطتين ':' .
    • القواميس هي متقلب .
    • يمكن الوصول إلى العناصر باستخدام المفاتيح.

    مثال:

     # Create a dictionary person = {&apos;name&apos;: &apos;Umesh&apos;, &apos;age&apos;: 25, &apos;city&apos;: &apos;Noida&apos;} print(&apos;person =&apos;, person) print(person[&apos;name&apos;]) # Modify Dictionary person[&apos;age&apos;] = 27 print(&apos;person =&apos;, person) 

    انتاج:

     person = {&apos;name&apos;: &apos;Umesh&apos;, &apos;age&apos;: 25, &apos;city&apos;: &apos;Noida&apos;} Umesh person = {&apos;name&apos;: &apos;Umesh&apos;, &apos;age&apos;: 27, &apos;city&apos;: &apos;Noida&apos;} 

    هذه مجرد أمثلة قليلة على هياكل البيانات المضمنة في بايثون. كل بنية بيانات لها خصائصها وحالات الاستخدام الخاصة بها.

    البرمجة الوظيفية بايثون

    يحدد هذا القسم من برنامج Python التعليمي بعض الأدوات المهمة المتعلقة بالبرمجة الوظيفية، مثل lambda والوظائف العودية. هذه الوظائف فعالة للغاية في إنجاز المهام المعقدة. نحدد بعض الوظائف المهمة، مثل التصغير والخريطة والتصفية. توفر Python وحدة functools التي تتضمن أدوات برمجة وظيفية متنوعة. قم بزيارة البرنامج التعليمي التالي لمعرفة المزيد حول البرمجة الوظيفية.

    قدمت الإصدارات الحديثة من Python ميزات تجعل البرمجة الوظيفية أكثر إيجازًا وتعبيرًا. على سبيل المثال، يسمح 'عامل تشغيل Walrus':= بتعيين متغير مضمّن في التعبيرات، وهو ما يمكن أن يكون مفيدًا عند العمل مع استدعاءات الوظائف المتداخلة أو استيعاب القائمة.

    وظيفة بايثون

    1. وظيفة لامدا - دالة لامدا صغيرة، وظيفة مجهولة يمكن أن يأخذ أي عدد من الوسائط ولكن يمكن أن يحتوي على تعبير واحد فقط. غالبًا ما تُستخدم وظائف Lambda في البرمجة الوظيفية لإنشاء وظائف 'سريعة' دون تحديد وظيفة مسماة.
    2. وظيفة العودية - الدالة العودية هي دالة تستدعي نفسها لحل مشكلة ما. غالبًا ما تُستخدم الوظائف العودية في البرمجة الوظيفية لإجراء عمليات حسابية معقدة أو لاجتياز هياكل البيانات المعقدة.
    3. وظيفة الخريطة - تطبق الدالة Map() دالة معينة على كل عنصر من العناصر القابلة للتكرار وتقوم بإرجاع عنصر قابل للتكرار جديد مع النتائج. يمكن أن يكون الإدخال القابل للتكرار عبارة عن قائمة أو صف أو غير ذلك.
    4. وظيفة التصفية - تقوم الدالة filter() بإرجاع مكرر من كائن متكرر تم تمرير الدالة له كوسيطة أولى ترجع True. يقوم بتصفية العناصر من العنصر التكراري الذي لا يستوفي الشرط المحدد.
    5. تقليل الوظيفة - تطبق الدالة التخفيض () دالة مكونة من وسيطتين بشكل تراكمي على عناصر العنصر القابل للتكرار من اليسار إلى اليمين لتقليله إلى قيمة واحدة.
    6. وحدة أدوات الوظائف - توفر وحدة functools في بايثون وظائف ذات ترتيب أعلى تعمل على وظائف أخرى، مثل جزئي () وتقليل ().
    7. وظيفة الكاري - دالة الكاري هي دالة تأخذ وسائط متعددة وترجع سلسلة من الوظائف التي تأخذ كل منها وسيطة واحدة.
    8. وظيفة التذكير - الحفظ هو أسلوب يستخدم في البرمجة الوظيفية للتخزين المؤقت لنتائج استدعاءات الوظائف باهظة الثمن وإرجاع النتيجة المخزنة مؤقتًا عند حدوث نفس المدخلات مرة أخرى.
    9. وظيفة الخيوط - الترابط هو أسلوب يستخدم في البرمجة الوظيفية لتشغيل مهام متعددة في وقت واحد لجعل التعليمات البرمجية أكثر كفاءة وأسرع.

    وحدات بايثون

    وحدات بايثون هي ملفات البرنامج التي تحتوي على تعليمات برمجية أو وظائف بايثون. لدى بايثون نوعان من الوحدات - الوحدات المعرفة من قبل المستخدم والوحدات المضمنة. يتم التعامل مع الوحدة النمطية التي يحددها المستخدم، أو كود Python المحفوظ بامتداد .py، كوحدة تعريفية من قبل المستخدم.

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

    تعد وحدات Python ضرورية للنظام البيئي للغة لأنها توفر تعليمات برمجية قابلة لإعادة الاستخدام ووظائف يمكن استيرادها إلى أي برنامج Python. فيما يلي بعض الأمثلة على العديد من وحدات بايثون، بالإضافة إلى وصف موجز لكل منها:

    الرياضيات : يمنح المستخدمين إمكانية الوصول إلى الثوابت الرياضية والدوال المثلثية.

    التاريخ والوقت : يوفر دروسًا لطريقة أبسط لمعالجة التواريخ والأوقات والفترات.

    أنت : يتيح التفاعل مع نظام التشغيل الأساسي، بما في ذلك إدارة العمليات وأنشطة نظام الملفات.

    عشوائي : توفر الوظيفة العشوائية أدوات لإنشاء أعداد صحيحة عشوائية واختيار عناصر عشوائية من القائمة.

    JSON : JSON عبارة عن بنية بيانات يمكن تشفيرها وفك تشفيرها، ويتم استخدامها بشكل متكرر في واجهات برمجة التطبيقات عبر الإنترنت وتبادل البيانات. تسمح هذه الوحدة بالتعامل مع JSON.
    يكرر : يدعم التعبيرات العادية، وهي أداة فعالة للبحث عن النص ومعالجة النص.

    المجموعات : يوفر هياكل بيانات بديلة مثل القواميس المصنفة والقواميس الافتراضية والصفوف المسماة.

    NumPy : NumPy عبارة عن مجموعة أدوات أساسية للحوسبة العلمية تدعم العمليات العددية على المصفوفات والمصفوفات.

    الباندا : يوفر هياكل وعمليات بيانات عالية المستوى للتعامل مع السلاسل الزمنية وأنواع البيانات المنظمة الأخرى.

    الطلبات : يوفر واجهة مستخدم بسيطة لواجهات برمجة تطبيقات الويب وينفذ طلبات HTTP.

    إدخال/إخراج ملف بايثون

    تُستخدم الملفات لتخزين البيانات في قرص الكمبيوتر. في هذا البرنامج التعليمي، نشرح كائن الملف المدمج في بايثون. يمكننا فتح ملف باستخدام برنامج Python النصي وإجراء عمليات مختلفة مثل الكتابة والقراءة والإلحاق. هناك طرق مختلفة لفتح الملف. لقد تم شرحنا بالمثال ذي الصلة. سوف نتعلم أيضًا إجراء عمليات القراءة/الكتابة على الملفات الثنائية.

    نظام إدخال/إخراج الملفات (I/O) في بايثون يقدم برامج للتواصل مع الملفات المخزنة على القرص. تتيح لنا أساليب Python المضمنة لكائن الملف تنفيذ إجراءات مثل القراءة والكتابة وإضافة البيانات إلى الملفات.

    ال يفتح() الطريقة في Python تصنع كائن ملف عند العمل مع الملفات. اسم الملف الذي سيتم فتحه والوضع الذي سيتم فتح الملف فيه هما المعلمتان اللتان تتطلبهما هذه الوظيفة. يمكن استخدام الوضع وفقًا للعمل الذي يجب القيام به مع الملف، مثل ' ص ' للقراءة، ' في 'للكتابة أو' أ 'للإرفاق.

    بعد إنشاء الكائن بنجاح، يمكن استخدام طرق مختلفة وفقًا لعملنا. إذا أردنا الكتابة في الملف، فيمكننا استخدام وظائف write()، وإذا كنت تريد القراءة والكتابة معًا، فيمكننا استخدام وظيفة append()، وفي الحالات التي نريد فيها قراءة محتوى الملف فقط الملف يمكننا استخدام وظيفة القراءة (). يمكن أيضًا التعامل مع الملفات الثنائية التي تحتوي على بيانات بتنسيق ثنائي بدلاً من التنسيق النصي باستخدام Python. تتم كتابة الملفات الثنائية بطريقة لا يستطيع البشر فهمها بشكل مباشر. ال ر.ب و wb يمكن للأوضاع قراءة وكتابة البيانات الثنائية في الملفات الثنائية.

    استثناءات بايثون

    يمكن تعريف الاستثناء بأنه حالة غير عادية في البرنامج تؤدي إلى انقطاع في تدفق البرنامج.

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

    استثناءات بايثون جانب مهم من معالجة الأخطاء في برمجة بايثون. عندما يواجه برنامج ما موقفًا أو خطأً غير متوقع، فقد يؤدي ذلك إلى ظهور استثناء، مما قد يقطع التدفق الطبيعي للبرنامج.

    في بايثون، يتم تمثيل الاستثناءات ككائنات تحتوي على معلومات حول الخطأ، بما في ذلك نوعه ورسالته. النوع الأكثر شيوعًا من الاستثناءات في بايثون هو فئة الاستثناء، وهي فئة أساسية لجميع الاستثناءات المضمنة الأخرى.

    للتعامل مع الاستثناءات في بايثون، نستخدم يحاول و يستثني صياغات. ال يحاول يتم استخدام العبارة لإحاطة التعليمات البرمجية التي قد تثير استثناءً، في حين أن يستثني يتم استخدام العبارة لتحديد كتلة من التعليمات البرمجية التي يجب تنفيذها عند حدوث استثناء.

    تحويل سلسلة إلى عدد صحيح في Java

    على سبيل المثال، خذ بعين الاعتبار الكود التالي:

     try: x = int ( input (&apos;Enter a number: &apos;)) y = 10 / x print (&apos;Result:&apos;, y) except ZeroDivisionError: print (&apos;Error: Division by zero&apos;) except ValueError: print (&apos;Error: Invalid input&apos;) 

    انتاج:

     Enter a number: 0 Error: Division by zero 

    في هذا الكود، نستخدم عبارة المحاولة لمحاولة إجراء عملية القسمة. إذا أثارت أي من هذه العمليات استثناءً، فسيتم تنفيذ كتلة المطابقة باستثناء.

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

    بايثون CSV

    يرمز ملف CSV إلى 'القيم المفصولة بفواصل'، والتي يتم تعريفها على أنها تنسيق ملف بسيط يستخدم بنية محددة لترتيب البيانات الجدولية. يقوم بتخزين البيانات الجدولية مثل جداول البيانات أو قواعد البيانات بنص عادي وله تنسيق شائع لتبادل البيانات. يتم فتح ملف CSV في ورقة Excel، وتحدد بيانات الصفوف والأعمدة التنسيق القياسي.

    يمكننا استخدام وظيفة CSV.reader لقراءة ملف CSV. تقوم هذه الوظيفة بإرجاع كائن قارئ يمكننا استخدامه لتكرار الصفوف في ملف CSV. يتم إرجاع كل صف كقائمة من القيم، حيث تتوافق كل قيمة مع عمود في ملف CSV.

    على سبيل المثال، خذ بعين الاعتبار الكود التالي:

     import csv with open(&apos;data.csv&apos;, &apos;r&apos;) as file: reader = csv.reader(file) for row in reader: print(row) 

    هنا، نفتح الملف data.csv في وضع القراءة ونقوم بإنشاء ملف csv.reader كائن باستخدام csv.reader() وظيفة. نقوم بعد ذلك بالتكرار فوق الصفوف الموجودة في ملف CSV باستخدام حلقة for وطباعة كل صف إلى وحدة التحكم.

    يمكننا استخدام CSV.writer() وظيفة لكتابة البيانات إلى ملف CSV. يقوم بإرجاع كائن كاتب يمكننا استخدامه لكتابة الصفوف في ملف CSV. يمكننا كتابة الصفوف عن طريق استدعاء كاتب () الطريقة على كائن الكاتب.

    على سبيل المثال، خذ بعين الاعتبار الكود التالي:

     import csv data = [ [&apos;Name&apos;, &apos;Age&apos;, &apos;Country&apos;], [&apos;Alice&apos;, &apos;25&apos;, &apos;USA&apos;], [&apos;Bob&apos;, &apos;30&apos;, &apos;Canada&apos;], [&apos;Charlie&apos;, &apos;35&apos;, &apos;Australia&apos;] ] with open(&apos;data.csv&apos;, &apos;w&apos;) as file: writer = csv.writer(file) for row in data: writer.writerow(row) 

    نقوم في هذا البرنامج بإنشاء قائمة من القوائم تسمى البيانات، حيث تمثل كل قائمة داخلية صفًا من البيانات. نقوم بعد ذلك بفتح الملف data.csv في وضع الكتابة وإنشاء ملف CSV.كاتب كائن باستخدام وظيفة CSV.writer. نقوم بعد ذلك بالتكرار فوق الصفوف في البيانات باستخدام حلقة for ونكتب كل صف في ملف CSV باستخدام طريقة الكاتب.

    بايثون إرسال البريد

    يمكننا إرسال أو قراءة البريد باستخدام برنامج Python النصي. تعد وحدات مكتبة Python القياسية مفيدة للتعامل مع البروتوكولات المختلفة مثل PoP3 وIMAP. توفر بايثون com.smtplib وحدة لإرسال رسائل البريد الإلكتروني باستخدام SMTP (بروتوكول نقل البريد البسيط). سوف نتعلم كيفية إرسال البريد باستخدام خدمة البريد الإلكتروني الشائعة SMTP من برنامج Python النصي.

    طرق بايثون السحرية

    طريقة بايثون السحرية هي طريقة خاصة تضيف 'سحرًا' إلى الفصل الدراسي. يبدأ وينتهي بشرطات سفلية مزدوجة، على سبيل المثال، _حار_ أو _str_ .

    تحدد الفئات المضمنة العديد من الأساليب السحرية. ال أنت() يمكن استخدام الوظيفة لمعرفة عدد الأساليب السحرية الموروثة بواسطة الفصل. يحتوي على بادئتين وشرطات سفلية لاحقة في اسم الطريقة.

    • تُعرف أيضًا أساليب بايثون السحرية باسم أساليب أقل ، اختصار لطرق 'الشرطة السفلية المزدوجة' لأن أسمائها تبدأ وتنتهي بشرطة سفلية مزدوجة.
    • الأساليب السحرية يتم استدعاؤها تلقائيًا بواسطة مترجم Python في مواقف معينة، مثل عند إنشاء كائن أو مقارنته بكائن آخر أو طباعته.
    • يمكن استخدام الأساليب السحرية لتخصيص سلوك الفئات، مثل تحديد كيفية مقارنة الكائنات، أو تحويلها إلى سلاسل، أو الوصول إليها كحاويات.
    • تتضمن بعض الأساليب السحرية شائعة الاستخدام حرارة لتهيئة كائن، str لتحويل كائن إلى سلسلة، مكافئ لمقارنة كائنين للمساواة، و بعنوان و setitem للوصول إلى العناصر الموجودة في كائن الحاوية.

    على سبيل المثال، شارع يمكن للطريقة السحرية تحديد كيفية تمثيل الكائن كسلسلة. هنا مثال

     class Person: def __init__(self, name, age): self.name = name self.age = age def __str__(self): return f&apos;{self.name} ({self.age})&apos; person = Person(&apos;Vikas&apos;, 22) print(person) 

    انتاج:

     Vikas (22) 

    في هذا المثال، تم تعريف الأسلوب str لإرجاع تمثيل سلسلة منسقة لكائن الشخص مع اسم الشخص وعمره.

    طريقة سحرية أخرى شائعة الاستخدام هي مكافئ ، الذي يحدد كيفية مقارنة الكائنات لتحقيق المساواة. هنا مثال:

     class Point: def __init__(self, x, y): self.x = x self.y = y def __eq__(self, other): return self.x == other.x and self.y == other.y point1 = Point(2, 3) point2 = Point(3, 4) point3 = Point(2, 3) print(point1 == point2) print(point1 == point3) 

    انتاج:

     False True 

    في هذا المثال، مكافئ يتم تعريف الطريقة لإرجاع True إذا كان هناك كائنان نقطيان لهما نفس إحداثيات x و y وخطأ بخلاف ذلك.

    مفاهيم بايثون أوبس

    يتم التعامل مع كل شيء في بايثون ككائن، بما في ذلك القيم الصحيحة، والعوامات، والوظائف، والفئات، ولا شيء. وبصرف النظر عن ذلك، تدعم بايثون جميع المفاهيم الموجهة. فيما يلي مقدمة مختصرة لمفاهيم Oops في Python.

    • الفئات والكائنات - فئات بايثون هي مخططات الكائن. الكائن عبارة عن مجموعة من البيانات والأساليب التي تعمل على البيانات.
    • ميراث - الميراث هو أسلوب يرث فيه أحد الفئات خصائص الفئات الأخرى.
    • البناء - توفر بايثون طريقة خاصة __init__() والتي تعرف باسم المنشئ. يتم استدعاء هذه الطريقة تلقائيًا عند إنشاء كائن.
    • عضو البيانات- متغير يحتوي على البيانات المرتبطة بالفئة وكائناتها.
    • تعدد الأشكال - تعدد الأشكال هو مفهوم حيث يمكن للكائن أن يتخذ أشكالا عديدة. في بايثون، يمكن تحقيق تعدد الأشكال من خلال التحميل الزائد للطريقة وتجاوز الطريقة.
    • طريقة التحميل الزائد- في بايثون، يتم تحقيق التحميل الزائد للطريقة من خلال الوسائط الافتراضية، حيث يمكن تعريف الطريقة بمعلمات متعددة. يتم استخدام القيم الافتراضية إذا لم يتم تمرير بعض المعلمات أثناء استدعاء الأسلوب.
    • تجاوز الأسلوب - تجاوز الطريقة هو مفهوم تقوم فيه فئة فرعية بتنفيذ طريقة محددة بالفعل في فئتها الفائقة.
    • التغليف - التغليف هو تجميع البيانات والأساليب في وحدة واحدة. في بايثون، يتم التغليف من خلال معدلات الوصول، مثل العامة والخاصة والمحمية. ومع ذلك، لا تفرض Python معدّلات الوصول بشكل صارم، ويشير اصطلاح التسمية إلى مستوى الوصول.
    • تجريد البيانات : تقنية لإخفاء تعقيد البيانات وإظهار الميزات الأساسية فقط للمستخدم. يوفر واجهة للتفاعل مع البيانات. يؤدي استخراج البيانات إلى تقليل التعقيد ويجعل التعليمات البرمجية أكثر نمطية، مما يسمح للمطورين بالتركيز على الميزات الأساسية للبرنامج.

    لقراءة مفهوم عفوًا بالتفصيل، قم بزيارة الموارد التالية.

    • مفاهيم Python Oops - في Python، النموذج الموجه للكائنات هو تصميم البرنامج باستخدام الفئات والكائنات. يرتبط الكائن بكيانات الكلمة الحقيقية مثل الكتاب والمنزل والقلم الرصاص وما إلى ذلك، ويحدد الفصل خصائصه وسلوكياته.
    • كائنات وفئات بايثون - في بايثون، الكائنات هي مثيلات للفئات والفئات عبارة عن مخططات تحدد بنية البيانات وسلوكها.
    • منشئ بايثون - المنشئ هو أسلوب خاص في فئة يستخدم لتهيئة سمات الكائن عند إنشاء الكائن.
    • وراثة بايثون - الوراثة هي آلية ترث فيها الفئة الجديدة (فئة فرعية أو فئة فرعية) خصائص وسلوكيات فئة موجودة (فئة فائقة أو فئة أصلية).
    • تعدد أشكال بايثون - يسمح تعدد الأشكال بمعاملة الكائنات من فئات مختلفة ككائنات من فئة فائقة مشتركة، مما يتيح استخدام فئات مختلفة بالتبادل من خلال واجهة مشتركة.

    موضوعات بايثون المتقدمة

    تتضمن لغة بايثون العديد من التطورات والمفاهيم المفيدة التي تساعد المبرمج على حل المهام المعقدة. وترد أدناه هذه المفاهيم.

    مكرر بايثون

    المكرّر هو ببساطة كائن يمكن التكرار عليه. تقوم بإرجاع كائن واحد في كل مرة. ويمكن تنفيذه باستخدام طريقتين خاصتين، __iter__() التالى__().

    التكرارات في بايثون هي كائنات تسمح بالتكرار على مجموعة من البيانات. يقومون بمعالجة كل عنصر مجموعة على حدة دون تحميل المجموعة بأكملها في الذاكرة.

    على سبيل المثال، لنقم بإنشاء مكرر يُرجع مربعات الأرقام حتى حد معين:

     def __init__(self, limit): self.limit = limit self.n = 0 def __iter__(self): return self def __next__(self): if self.n <= 2 self.limit: square="self.n" ** self.n +="1" return else: raise stopiteration numbers="Squares(5)" for n in numbers: print(n) < pre> <p> <strong>Output:</strong> </p> <pre> 0 1 4 9 16 25 </pre> <p>In this example, we have created a class Squares that acts as an iterator by implementing the __iter__() and __next__() methods. The __iter__() method returns the Object itself, and the __next__() method returns the next square of the number until the limit is reached.</p> <p>To learn more about the iterators, visit our Python Iterators tutorial.</p> <h3> Python Generators </h3> <p>  <strong>Python generators</strong>  produce a sequence of values <strong>using a yield statement</strong> rather than a return since they are functions that return iterators. Generators terminate the function&apos;s execution while keeping the local state. It picks up right where it left off when it is restarted. Because we don&apos;t have to implement the iterator protocol thanks to this feature, writing iterators is made simpler. Here is an illustration of a straightforward generator function that produces squares of numbers:</p> <pre> # Generator Function def square_numbers(n): for i in range(n): yield i**2 # Create a generator object generator = square_numbers(5) # Print the values generated by the generator for num in generator: print(num) </pre> <p> <strong>Output:</strong> </p> <pre> 0 1 4 9 16 </pre> <h2>Python Modifiers</h2> <p>  <strong>Python Decorators</strong>  are functions used to modify the behaviour of another function. They allow adding functionality to an existing function without modifying its code directly. Decorators are defined using the <strong>@</strong> symbol followed by the name of the decorator function. They can be used for logging, timing, caching, etc.</p> <p>Here&apos;s an example of a decorator function that adds timing functionality to another function:</p> <pre> import time from math import factorial # Decorator to calculate time taken by # the function def time_it(func): def wrapper(*args, **kwargs): start = time.time() result = func(*args, **kwargs) end = time.time() print(f&apos;{func.__name__} took {end-start:.5f} seconds to run.&apos;) return result return wrapper @time_it def my_function(n): time.sleep(2) print(f&apos;Factorial of {n} = {factorial(n)}&apos;) my_function(25) </pre> <p> <strong>Output:</strong> </p> <pre> </pre> <p>In the above example, the time_it decorator function takes another function as an argument and returns a wrapper function. The wrapper function calculates the time to execute the original function and prints it to the console. The @time_it decorator is used to apply the time_it function to the my_function function. When my_function is called, the decorator is executed, and the timing functionality is added.</p> <h2>Python MySQL</h2> <p>Python MySQL is a powerful relational database management system. We must set up the environment and establish a connection to use MySQL with Python. We can create a new database and tables using SQL commands in Python.</p> <ul> <li>  <strong>Environment Setup</strong>  : Installing and configuring MySQL Connector/Python to use Python with MySQL.</li> <li>  <strong>Database Connection</strong>  : Establishing a connection between Python and MySQL database using MySQL Connector/Python.</li> <li>  <strong>Creating New Database</strong>  : Creating a new database in MySQL using Python.</li> <li>  <strong>Creating Tables</strong>  : Creating tables in the MySQL database with Python using SQL commands.</li> <li>  <strong>Insert Operation</strong>  : Insert data into MySQL tables using Python and SQL commands.</li> <li>  <strong>Read Operation</strong>  : Reading data from MySQL tables using Python and SQL commands.</li> <li>  <strong>Update Operation</strong>  : Updating data in MySQL tables using Python and SQL commands.</li> <li>  <strong>Join Operation</strong>  : Joining two or more tables in MySQL using Python and SQL commands.</li> <li>  <strong>Performing Transactions</strong>  : Performing a group of SQL queries as a single unit of work in MySQL using Python.</li> </ul> <p>Other relative points include handling errors, creating indexes, and using stored procedures and functions in MySQL with Python.</p> <h2>Python MongoDB</h2> <p> Python MongoDB is a popular NoSQL database that stores data in JSON-like documents. It is schemaless and provides high scalability and flexibility for data storage. We can use MongoDB with Python using the PyMongo library, which provides a simple and intuitive interface for interacting with MongoDB.</p> <p>Here are some common tasks when working with MongoDB in Python:</p> <ol class="points"> <li>  <strong>Environment Setup</strong>  : Install and configure MongoDB and PyMongo library on your system.</li> <li>  <strong>Database Connection</strong>  : Connect to a MongoDB server using the MongoClient class from PyMongo.</li> <li>  <strong>Creating a new database</strong>  : Use the MongoClient Object to create a new database.</li> <li>  <strong>Creating collections</strong>  : Create collections within a database to store documents.</li> <li>  <strong>Inserting documents</strong>  : Insert new documents into a collection using the insert_one() or insert_many() methods.</li> <li>  <strong>Querying documents</strong>  : Retrieve documents from a collection using various query methods like find_one(), find(), etc.</li> <li>  <strong>Updating documents</strong>  : Modify existing documents in a collection using update_one() or update_many() methods.</li> <li>  <strong>Deleting documents</strong>  : Remove documents from a collection using the delete_one() or delete_many() methods.</li> <li>  <strong>Aggregation</strong>  : Perform aggregation operations like grouping, counting, etc., using the aggregation pipeline framework.</li> <tr><td>Indexing:</td> Improve query performance by creating indexes on fields in collections. </tr></ol> <p>There are many more advanced topics in MongoDB, such as data sharding, replication, and more, but these tasks cover the basics of working with MongoDB in Python.</p> <h2> Python SQLite </h2> <p>Relational databases are built and maintained using Python SQLite, a compact, serverless, self-contained database engine. Its mobility and simplicity make it a popular option for local or small-scale applications. Python has a built-in module for connecting to SQLite databases called SQLite3, enabling developers to work with SQLite databases without difficulties.</p> <p>Various API methods are available through the SQLite3 library that may be used to run SQL queries, insert , select , update , and remove data, as well as get data from tables. Additionally, it allows transactions, allowing programmers to undo changes in case of a problem. Python SQLite is a fantastic option for creating programs that need an embedded database system, including desktop, mobile, and modest-sized web programs. SQLite has become popular among developers for lightweight apps with database functionality thanks to its ease of use, portability, and smooth connection with Python.</p> <h2> Python CGI </h2> <p>  <strong>Python CGI</strong>  is a technology for running scripts through web servers to produce dynamic online content. It offers a communication channel and a dynamic content generation interface for external CGI scripts and the web server. Python CGI scripts may create HTML web pages, handle form input, and communicate with databases. Python CGI enables the server to carry out Python scripts and provide the results to the client, offering a quick and effective approach to creating dynamic online applications.</p> <p>Python CGI scripts may be used for many things, including creating dynamic web pages, processing forms, and interacting with databases. Since Python, a potent and popular programming language, can be utilized to create scripts, it enables a more customized and flexible approach to web creation. Scalable, safe, and maintainable online applications may be created with Python CGI. Python CGI is a handy tool for web developers building dynamic and interactive online applications.</p> <h2> Asynchronous Programming in Python </h2> <p> <strong>Asynchronous programming</strong> is a paradigm for computer programming that enables independent and concurrent operation of activities. It is frequently used in applications like web servers, database software, and network programming, where several tasks or requests must be handled concurrently.</p> <p>Python has asyncio, Twisted, and Tornado among its libraries and frameworks for asynchronous programming. Asyncio, one of these, offers a simple interface for asynchronous programming and is the official asynchronous programming library in Python.</p> <p>Coroutines are functions that may be halted and restarted at specific locations in the code and are utilized by asyncio. This enables numerous coroutines to operate simultaneously without interfering with one another. For constructing and maintaining coroutines, the library offers several classes and methods, including <strong>asyncio.gather(),</strong> <strong>asyncio.wait(),</strong> and <strong>asyncio.create_task().</strong> </p> <p>Event loops, which are in charge of planning and operating coroutines, are another feature of asyncio. By cycling between coroutines in a non-blocking way, the event loop controls the execution of coroutines and ensures that no coroutine blocks another. Additionally, it supports timers and scheduling callbacks, which may be helpful when activities must be completed at specified times or intervals.</p> <h2> Python Concurrency </h2> <p>The term &apos; <strong>concurrency</strong> &apos; describes a program&apos;s capacity to carry out several tasks at once, enhancing the program&apos;s efficiency. Python offers several modules and concurrency-related methods, including asynchronous programming, multiprocessing, and multithreading. While multiprocessing involves running many processes simultaneously on a system, multithreading involves running numerous threads concurrently inside a single process.</p> <p>The <strong>threading module</strong> in Python enables programmers to build multithreading. It offers classes and operations for establishing and controlling threads. Conversely, the multiprocessing module allows developers to design and control processes. Python&apos;s asyncio module provides asynchronous programming support, allowing developers to write non-blocking code that can handle multiple tasks concurrently. Using these techniques, developers can write highperformance, scalable programs that can handle multiple tasks concurrently.</p> <p>Python&apos;s threading module enables the concurrent execution of several threads within a single process, which is helpful for I/O-bound activities.</p> <p>For CPU-intensive operations like image processing or data analysis, multiprocessing modules make it possible to execute numerous processes concurrently across multiple CPU cores.</p> <p>The asyncio module supports asynchronous I/O and permits the creation of single-threaded concurrent code using coroutines for high-concurrency network applications.</p> <p>With libraries like Dask , <a href="/pyspark-tutorial">PySpark</a> , and MPI, Python may also be used for parallel computing. These libraries allow workloads to be distributed across numerous nodes or clusters for better performance.</p> <h2> Web Scrapping using Python </h2> <p>The process of web scraping is used to retrieve data from websites automatically. Various tools and libraries extract data from HTML and other online formats. Python is among the most widely used programming languages for web scraping because of its ease of use, adaptability, and variety of libraries.</p> <p>We must take a few steps to accomplish web scraping using Python. We must first decide which website to scrape and what information to gather. Then, we can submit a request to the website and receive the HTML content using Python&apos;s requests package. Once we have the HTML text, we can extract the needed data using a variety of parsing packages, like <strong>Beautiful Soup and lxml</strong> .</p> <p>We can employ several strategies, like slowing requests, employing user agents, and using proxies, to prevent overburdening the website&apos;s server. It is also crucial to abide by the terms of service for the website and respect its robots.txt file.</p> <p>Data mining, lead creation, pricing tracking, and many more uses are possible for web scraping. However, as unauthorized web scraping may be against the law and unethical, it is essential to utilize it professionally and ethically.</p> <h2>Natural Language Processing (NLP) using Python</h2> <p>A branch of artificial intelligence (AI) called &apos;natural language processing&apos; (NLP) studies how computers and human language interact. Thanks to NLP, computers can now understand, interpret, and produce human language. Due to its simplicity, versatility, and strong libraries like NLTK (Natural Language Toolkit) and spaCy, Python is a well-known programming language for NLP.</p> <p> <strong>For NLP tasks, including tokenization, stemming, lemmatization, part-of-speech tagging, named entity identification, sentiment analysis, and others, NLTK provides a complete library.</strong> It has a variety of corpora (big, organized text collections) for developing and evaluating NLP models. Another well-liked library for NLP tasks is spaCy , which offers quick and effective processing of enormous amounts of text. It enables simple modification and expansion and comes with pre-trained models for various NLP workloads.</p> <p>NLP may be used in Python for various practical purposes, including chatbots, sentiment analysis, text categorization, machine translation, and more. NLP is used, for instance, by chatbots to comprehend and reply to user inquiries in a natural language style. Sentiment analysis, which may be helpful for brand monitoring, customer feedback analysis, and other purposes, employs NLP to categorize text sentiment (positive, negative, or neutral). Text documents are categorized using natural language processing (NLP) into pre-established categories for spam detection, news categorization, and other purposes.</p> <p>Python is a strong and useful tool when analyzing and processing human language. Developers may carry out various NLP activities and create useful apps that can communicate with consumers in natural language with libraries like NLTK and spaCy.</p> <h2>Conclusion:</h2> <p>In this tutorial, we&apos;ve looked at some of Python&apos;s most important features and ideas, including variables, data types, loops, functions, modules, and more. More complex subjects, including web scraping, natural language processing, parallelism, and database connection, have also been discussed. You will have a strong basis to continue learning about Python and its applications using the information you have learned from this lesson.</p> <p>Remember that practicing and developing code is the best method to learn Python. You may find many resources at javaTpoint to support your further learning, including documentation, tutorials, online groups, and more. You can master Python and use it to create wonderful things if you work hard and persist.</p> <h2>Prerequisite</h2> <p>Before learning Python, you must have the basic knowledge of programming concepts.</p> <h2>Audience</h2> <p>Our Python tutorial is designed to help beginners and professionals.</p> <h2>Problem</h2> <p>We assure that you will not find any problem in this Python tutorial. But if there is any mistake, please post the problem in contact form.</p> <hr></=>

    في هذا المثال، قمنا بإنشاء فئة Squares التي تعمل كمكرر من خلال تنفيذ أساليب __iter__() و __next__(). تقوم الطريقة __iter__() بإرجاع الكائن نفسه، وتقوم الطريقة __next__() بإرجاع المربع التالي من الرقم حتى يتم الوصول إلى الحد الأقصى.

    لمعرفة المزيد حول التكرارات، تفضل بزيارة البرنامج التعليمي لـ Python Iterators.

    مولدات بايثون

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

     # Generator Function def square_numbers(n): for i in range(n): yield i**2 # Create a generator object generator = square_numbers(5) # Print the values generated by the generator for num in generator: print(num) 

    انتاج:

     0 1 4 9 16 

    معدّلات بايثون

    مصممو الديكور بايثون هي وظائف تستخدم لتعديل سلوك وظيفة أخرى. أنها تسمح بإضافة وظيفة إلى وظيفة موجودة دون تعديل التعليمات البرمجية الخاصة بها مباشرة. يتم تعريف مصممي الديكور باستخدام @ الرمز متبوعًا باسم وظيفة الديكور. يمكن استخدامها للتسجيل والتوقيت والتخزين المؤقت وما إلى ذلك.

    فيما يلي مثال لوظيفة الديكور التي تضيف وظيفة التوقيت إلى وظيفة أخرى:

     import time from math import factorial # Decorator to calculate time taken by # the function def time_it(func): def wrapper(*args, **kwargs): start = time.time() result = func(*args, **kwargs) end = time.time() print(f&apos;{func.__name__} took {end-start:.5f} seconds to run.&apos;) return result return wrapper @time_it def my_function(n): time.sleep(2) print(f&apos;Factorial of {n} = {factorial(n)}&apos;) my_function(25) 

    انتاج:

     

    في المثال أعلاه، تأخذ وظيفة الديكور time_it دالة أخرى كوسيطة وترجع دالة مجمعة. تقوم وظيفة التغليف بحساب الوقت اللازم لتنفيذ الوظيفة الأصلية وطباعتها على وحدة التحكم. يتم استخدام ديكور @time_it لتطبيق وظيفة time_it على وظيفة my_function. عند استدعاء my_function، يتم تنفيذ الديكور وإضافة وظيفة التوقيت.

    بايثون ماي إس كيو إل

    يعد Python MySQL نظامًا قويًا لإدارة قواعد البيانات العلائقية. يجب علينا تهيئة البيئة وإنشاء اتصال لاستخدام MySQL مع Python. يمكننا إنشاء قاعدة بيانات وجداول جديدة باستخدام أوامر SQL في بايثون.

    • إعداد البيئة : تثبيت وتكوين MySQL Connector/Python لاستخدام Python مع MySQL.
    • اتصال قاعدة البيانات : إنشاء اتصال بين قاعدة بيانات Python وMySQL باستخدام MySQL Connector/Python.
    • إنشاء قاعدة بيانات جديدة : إنشاء قاعدة بيانات جديدة في MySQL باستخدام بايثون.
    • إنشاء الجداول : إنشاء الجداول في قاعدة بيانات MySQL باستخدام Python باستخدام أوامر SQL.
    • إدراج العملية : إدراج البيانات في جداول MySQL باستخدام أوامر Python وSQL.
    • قراءة العملية : قراءة البيانات من جداول MySQL باستخدام أوامر Python وSQL.
    • عملية التحديث : تحديث البيانات في جداول MySQL باستخدام أوامر Python وSQL.
    • الانضمام إلى العملية : الانضمام إلى جدولين أو أكثر في MySQL باستخدام أوامر Python وSQL.
    • تنفيذ المعاملات : تنفيذ مجموعة من استعلامات SQL كوحدة عمل واحدة في MySQL باستخدام Python.

    تشمل النقاط النسبية الأخرى معالجة الأخطاء وإنشاء الفهارس واستخدام الإجراءات والوظائف المخزنة في MySQL مع Python.

    بايثون مونغو دي بي

    Python MongoDB هي قاعدة بيانات NoSQL شائعة تقوم بتخزين البيانات في مستندات تشبه JSON. إنه بدون مخطط ويوفر قابلية توسع ومرونة عالية لتخزين البيانات. يمكننا استخدام MongoDB مع Python باستخدام مكتبة PyMongo، التي توفر واجهة بسيطة وبديهية للتفاعل مع MongoDB.

    فيما يلي بعض المهام الشائعة عند العمل مع MongoDB في Python:

    1. إعداد البيئة : قم بتثبيت وتكوين مكتبة MongoDB وPyMongo على نظامك.
    2. اتصال قاعدة البيانات : اتصل بخادم MongoDB باستخدام فئة MongoClient من PyMongo.
    3. إنشاء قاعدة بيانات جديدة : استخدم كائن MongoClient لإنشاء قاعدة بيانات جديدة.
    4. إنشاء مجموعات : إنشاء مجموعات داخل قاعدة بيانات لتخزين المستندات.
    5. إدخال المستندات : قم بإدراج مستندات جديدة في مجموعة باستخدام أساليب Insert_one() أو Insert_many().
    6. الاستعلام عن المستندات : استرداد المستندات من مجموعة باستخدام طرق استعلام متنوعة مثل find_one() وfind() وما إلى ذلك.
    7. تحديث المستندات : قم بتعديل المستندات الموجودة في المجموعة باستخدام أساليب update_one() أو update_many().
    8. حذف المستندات : قم بإزالة المستندات من المجموعة باستخدام الأسلوبdelet_one() أوdelete_many().
    9. تجميع : تنفيذ عمليات التجميع مثل التجميع والعد وما إلى ذلك، باستخدام إطار خط أنابيب التجميع.
    10. الفهرسة:تحسين أداء الاستعلام عن طريق إنشاء فهارس في الحقول في المجموعات.

    هناك العديد من المواضيع المتقدمة في MongoDB، مثل تقسيم البيانات والنسخ المتماثل والمزيد، ولكن هذه المهام تغطي أساسيات العمل مع MongoDB في Python.

    بايثون سكليتي

    يتم إنشاء قواعد البيانات العلائقية وصيانتها باستخدام Python SQLite، وهو محرك قاعدة بيانات مدمج بدون خادم ومكتفي بذاته. إن سهولة تنقله وبساطته تجعله خيارًا شائعًا للتطبيقات المحلية أو صغيرة الحجم. تحتوي لغة Python على وحدة مدمجة للاتصال بقواعد بيانات SQLite تسمى SQLite3، مما يتيح للمطورين العمل مع قواعد بيانات SQLite دون أي صعوبات.

    تتوفر أساليب API المختلفة من خلال مكتبة SQLite3 والتي يمكن استخدامها لتشغيل استعلامات SQL وإدراج البيانات وتحديدها وتحديثها وإزالتها، بالإضافة إلى الحصول على البيانات من الجداول. بالإضافة إلى ذلك، فهو يسمح بالمعاملات، مما يسمح للمبرمجين بالتراجع عن التغييرات في حالة حدوث مشكلة. يعد Python SQLite خيارًا رائعًا لإنشاء البرامج التي تحتاج إلى نظام قاعدة بيانات مضمن، بما في ذلك برامج سطح المكتب والجوال وبرامج الويب ذات الحجم المتواضع. أصبح SQLite شائعًا بين المطورين للتطبيقات خفيفة الوزن مع وظائف قاعدة البيانات بفضل سهولة الاستخدام وقابلية النقل والاتصال السلس مع Python.

    بايثون CGI

    بايثون CGI هي تقنية لتشغيل البرامج النصية من خلال خوادم الويب لإنتاج محتوى ديناميكي عبر الإنترنت. فهو يوفر قناة اتصال وواجهة إنشاء محتوى ديناميكية لنصوص CGI الخارجية وخادم الويب. يمكن لبرامج Python CGI النصية إنشاء صفحات ويب بتنسيق HTML والتعامل مع إدخال النماذج والتواصل مع قواعد البيانات. تمكن Python CGI الخادم من تنفيذ نصوص Python وتقديم النتائج للعميل، مما يوفر أسلوبًا سريعًا وفعالًا لإنشاء تطبيقات ديناميكية عبر الإنترنت.

    يمكن استخدام نصوص Python CGI للعديد من الأشياء، بما في ذلك إنشاء صفحات ويب ديناميكية ومعالجة النماذج والتفاعل مع قواعد البيانات. نظرًا لأنه يمكن استخدام لغة Python، وهي لغة برمجة قوية وشائعة، لإنشاء نصوص برمجية، فإنها تتيح اتباع نهج أكثر تخصيصًا ومرونة لإنشاء الويب. يمكن إنشاء تطبيقات عبر الإنترنت قابلة للتطوير وآمنة وقابلة للصيانة باستخدام Python CGI. تعد Python CGI أداة مفيدة لمطوري الويب الذين يقومون ببناء تطبيقات ديناميكية وتفاعلية عبر الإنترنت.

    البرمجة غير المتزامنة في بايثون

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

    تحتوي Python على asyncio وTwisted وTornado بين مكتباتها وأطرها للبرمجة غير المتزامنة. تقدم Asyncio، إحدى هذه المواقع، واجهة بسيطة للبرمجة غير المتزامنة وهي مكتبة البرمجة غير المتزامنة الرسمية في لغة Python.

    Coroutines هي وظائف يمكن إيقافها وإعادة تشغيلها في مواقع محددة في الكود ويتم استخدامها بواسطة asyncio. وهذا يمكّن العديد من coroutines من العمل في وقت واحد دون التدخل مع بعضها البعض. لإنشاء وصيانة coroutines، تقدم المكتبة عدة فئات وطرق، بما في ذلك asyncio.gather(), غير متزامن.انتظار ()، و asyncio.create_task().

    تعد حلقات الأحداث، المسؤولة عن تخطيط وتشغيل coroutines، ميزة أخرى للمزامنة. من خلال التنقل بين الكوروتينات بطريقة غير معيقة، تتحكم حلقة الحدث في تنفيذ الكوروتينات وتضمن عدم وجود كوروتين يمنع أي كوروتينات أخرى. بالإضافة إلى ذلك، فهو يدعم المؤقتات وجدولة عمليات الاسترجاعات، والتي قد تكون مفيدة عندما يجب إكمال الأنشطة في أوقات أو فترات زمنية محددة.

    التزامن بايثون

    على المدى ' التزامن يصف قدرة البرنامج على تنفيذ عدة مهام في وقت واحد، مما يعزز كفاءة البرنامج. تقدم بايثون العديد من الوحدات والأساليب المتعلقة بالتزامن، بما في ذلك البرمجة غير المتزامنة، والمعالجة المتعددة، وتعدد مؤشرات الترابط. في حين أن المعالجة المتعددة تتضمن تشغيل العديد من العمليات في وقت واحد على النظام، فإن المعالجة المتعددة تتضمن تشغيل العديد من سلاسل العمليات بشكل متزامن داخل عملية واحدة.

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

    تتيح وحدة الترابط في Python التنفيذ المتزامن لعدة سلاسل رسائل ضمن عملية واحدة، وهو أمر مفيد للأنشطة المرتبطة بالإدخال/الإخراج.

    بالنسبة للعمليات التي تتطلب وحدة المعالجة المركزية بشكل مكثف مثل معالجة الصور أو تحليل البيانات، تتيح وحدات المعالجة المتعددة تنفيذ العديد من العمليات بشكل متزامن عبر مراكز وحدة المعالجة المركزية المتعددة.

    تدعم وحدة asyncio عمليات الإدخال/الإخراج غير المتزامنة وتسمح بإنشاء كود متزامن أحادي الخيط باستخدام coroutines لتطبيقات الشبكة عالية التزامن.

    مع مكتبات مثل Dask، باي سبارك و MPI، يمكن أيضًا استخدام Python للحوسبة المتوازية. تسمح هذه المكتبات بتوزيع أحمال العمل عبر العديد من العقد أو المجموعات للحصول على أداء أفضل.

    تخريد الويب باستخدام بايثون

    تُستخدم عملية تجريف الويب لاسترداد البيانات من مواقع الويب تلقائيًا. تقوم الأدوات والمكتبات المختلفة باستخراج البيانات من HTML والتنسيقات الأخرى عبر الإنترنت. تعد Python من بين لغات البرمجة الأكثر استخدامًا على نطاق واسع لتجميع الويب نظرًا لسهولة استخدامها وقابليتها للتكيف وتنوع مكتباتها.

    يجب أن نتخذ بعض الخطوات لإنجاز عملية تجريف الويب باستخدام Python. يجب علينا أولاً أن نقرر أي موقع ويب يجب أن نتخلص منه وما هي المعلومات التي يجب جمعها. بعد ذلك، يمكننا تقديم طلب إلى الموقع واستلام محتوى HTML باستخدام حزمة طلبات بايثون. بمجرد حصولنا على نص HTML، يمكننا استخراج البيانات المطلوبة باستخدام مجموعة متنوعة من حزم التحليل، مثل حساء جميل وlxml .

    يمكننا استخدام العديد من الاستراتيجيات، مثل إبطاء الطلبات، وتوظيف وكلاء المستخدم، واستخدام الوكلاء، لمنع زيادة العبء على خادم موقع الويب. ومن المهم أيضًا الالتزام بشروط الخدمة الخاصة بالموقع واحترام ملف robots.txt الخاص به.

    من الممكن استخراج البيانات، وإنشاء العملاء المحتملين، وتتبع الأسعار، والعديد من الاستخدامات الأخرى لتجريد الويب. ومع ذلك، نظرًا لأن تجريف الويب غير المصرح به قد يكون مخالفًا للقانون وغير أخلاقي، فمن الضروري استخدامه بشكل احترافي وأخلاقي.

    معالجة اللغات الطبيعية (NLP) باستخدام بايثون

    يدرس فرع من الذكاء الاصطناعي (AI) يسمى 'معالجة اللغة الطبيعية' (NLP) كيفية تفاعل أجهزة الكمبيوتر واللغة البشرية. بفضل البرمجة اللغوية العصبية (NLP)، أصبح بإمكان أجهزة الكمبيوتر الآن فهم اللغة البشرية وتفسيرها وإنتاجها. نظرًا لبساطتها وتعدد استخداماتها ومكتباتها القوية مثل NLTK (مجموعة أدوات اللغة الطبيعية) وspaCy، تعد Python لغة برمجة معروفة للبرمجة اللغوية العصبية (NLP).

    بالنسبة لمهام البرمجة اللغوية العصبية، بما في ذلك الترميز، والتحفيز، والترجمة، ووضع علامات على جزء من الكلام، وتحديد الكيان المسمى، وتحليل المشاعر، وغيرها، توفر NLTK مكتبة كاملة. يحتوي على مجموعة متنوعة من المجموعات النصية (مجموعات نصية كبيرة ومنظمة) لتطوير وتقييم نماذج البرمجة اللغوية العصبية. مكتبة أخرى مشهورة لمهام البرمجة اللغوية العصبية هي spaCy، والتي توفر معالجة سريعة وفعالة لكميات هائلة من النصوص. إنه يتيح التعديل والتوسيع البسيط ويأتي مع نماذج مدربة مسبقًا لمختلف أعباء عمل البرمجة اللغوية العصبية (NLP).

    يمكن استخدام البرمجة اللغوية العصبية (NLP) في لغة Python لأغراض عملية مختلفة، بما في ذلك برامج الدردشة الآلية وتحليل المشاعر وتصنيف النصوص والترجمة الآلية والمزيد. يتم استخدام البرمجة اللغوية العصبية، على سبيل المثال، بواسطة برامج الدردشة الآلية لفهم استفسارات المستخدم والرد عليها بأسلوب اللغة الطبيعية. يستخدم تحليل المشاعر، والذي قد يكون مفيدًا لمراقبة العلامة التجارية، وتحليل تعليقات العملاء، وأغراض أخرى، البرمجة اللغوية العصبية (NLP) لتصنيف المشاعر النصية (إيجابية أو سلبية أو محايدة). يتم تصنيف المستندات النصية باستخدام معالجة اللغة الطبيعية (NLP) إلى فئات محددة مسبقًا لاكتشاف البريد العشوائي وتصنيف الأخبار وأغراض أخرى.

    بايثون هي أداة قوية ومفيدة عند تحليل ومعالجة اللغة البشرية. يمكن للمطورين تنفيذ العديد من أنشطة البرمجة اللغوية العصبية وإنشاء تطبيقات مفيدة يمكنها التواصل مع المستهلكين باللغة الطبيعية من خلال مكتبات مثل NLTK وspaCy.

    خاتمة:

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

    مجموعة سلسلة جافا

    تذكر أن ممارسة التعليمات البرمجية وتطويرها هي أفضل طريقة لتعلم لغة بايثون. قد تجد العديد من الموارد في javaTpoint لدعم المزيد من التعلم، بما في ذلك الوثائق والبرامج التعليمية والمجموعات عبر الإنترنت والمزيد. يمكنك إتقان لغة بايثون واستخدامها لإنشاء أشياء رائعة إذا عملت بجد واستمريت.

    المتطلبات المسبقة

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

    جمهور

    تم تصميم برنامج Python التعليمي الخاص بنا لمساعدة المبتدئين والمحترفين.

    مشكلة

    نحن نؤكد أنك لن تجد أي مشكلة في هذا البرنامج التعليمي لبايثون. ولكن إذا كان هناك أي خطأ، يرجى نشر المشكلة في نموذج الاتصال.