كورس للمبتدئين PYTHON
هل أنت متشوق للغوص في عالم البرمجة ولكنك غير متأكد من أين تبدأ؟ لا تبحث أكثر - Python هي نقطة بداية ممتازة للمبتدئين والمطورين المخضرمين.
في هذا الدليل، سأطلعك على الأساسيات حتى تتمكن من البدء في رحلتك مع Python.
جدول المحتويات
المتطلبات الأساسية
لماذا نتعلم بايثون؟
الخصائص الأساسية لبايثون
الاستخدامات العملية لبايثون
كيفية كتابة "Hello, World" في بايثون
متغيرات بايثون وأنواع البيانات
أنواع البيانات البدائية (الأساسية)
خصائص أنواع البيانات البدائية
حالات الاستخدام لأنواع البيانات البدائية
أنواع البيانات غير البدائية (المركبة) في بايثون
خصائص أنواع البيانات غير البدائية
حالات الاستخدام لأنواع البيانات غير البدائية
المشغلات في بايثون
المشغلات الحسابية
مشغلات المقارنة
العبارات في بايثون
عبارات التعيين
عبارات الطباعة
عبارات الشرط (if, elif, else)
الحلقات (for and while)
حلقة For
حلقة While
عبارات Break and Continue
الدوال في بايثون
الخاتمة
jadwal almuhtawayat
المتطلبات الأساسية
قبل الشروع في مغامرة البرمجة هذه، تأكد من توفر ما يلي:
تثبيت Python.
محرر أكواد مثل VSCode أو Vim أو Sublime.
الآن، دعنا نستكشف مزايا استخدام Python.
لماذا يجب أن تتعلم بايثون؟
إذا كنت تتساءل لماذا يعد بايثون خيارًا ممتازًا للمبتدئين والمطورين المخضرمين على حد سواء، فإليك بعض الأسباب:
سهولة القراءة والبساطة: تعمل قواعد بايثون الواضحة على تعزيز قابلية قراءة التعليمات البرمجية، مما يقلل من وقت التطوير ويجعلها مناسبة للمبتدئين.
التنوع: يمكنك استخدام بايثون لبناء مجموعة متنوعة من التطبيقات، من تطوير الويب إلى علم البيانات والذكاء الاصطناعي. كما يحتوي على مكتبة قياسية شاملة والعديد من الحزم المفيدة من جهات خارجية.
المجتمع والتوثيق: يتمتع بايثون بمجتمع قوي وتوثيق شامل يوفر دعمًا واسع النطاق، مما يعزز شعبية اللغة ونموها.
التوافق بين الأنظمة الأساسية: يضمن التنفيذ السلس عبر أنظمة Windows وmacOS وLinux.
المكتبات والأطر الشاملة: يبسط النظام البيئي الغني المهام المعقدة، مما يوفر الوقت والجهد للمطورين.
نأمل أن تكون مهتمًا بمزايا بايثون - لذا دعنا نتعمق في خصائصها الرئيسية.
الخصائص الرئيسية لبايثون
إن فهم الخصائص الرئيسية لبايثون سيمنحك نظرة ثاقبة على نقاط قوتها ولماذا تعد خيارًا شائعًا بين المطورين:
اللغة المفسرة: لا تتم ترجمة الكود الخاص بك مباشرةً بواسطة الجهاز المستهدف. بدلاً من ذلك، يقوم برنامج خاص يسمى المترجم بقراءة الكود وتنفيذه، مما يسمح بتنفيذ الكود الخاص بك عبر الأنظمة الأساسية.
الكتابة الديناميكية: تلغي الكتابة الديناميكية الحاجة إلى إعلانات صريحة عن نوع البيانات، مما يعزز البساطة والمرونة.
موجه للكائنات: يدعم بايثون مبادئ موجهة للكائنات، مما يعزز من قابلية الكود لإعادة الاستخدام.
بناء الجملة القائم على المسافة البادئة: يفرض بناء الجملة القائم على المسافة البادئة قابلية قراءة الكود ويحافظ على أسلوب ترميز ثابت.
إدارة الذاكرة: تعمل إدارة الذاكرة التلقائية من خلال جمع القمامة على تبسيط التعامل مع الذاكرة للمطورين.
الاستخدامات العملية لبايثون
إن تنوع بايثون وسهولة قراءته تجعله مناسبًا لمجموعة واسعة من التطبيقات. فيما يلي بعض الاستخدامات العملية:
تطوير الويب: بايثون، مع أطر عمل مثل Django وFlask، يدعم تطوير الواجهة الخلفية لتطبيقات الويب القوية.
علم البيانات والتعلم الآلي: يستخدم على نطاق واسع في علم البيانات، تدعم مكتبات بايثون مثل NumPy وPandas تحليل البيانات والتعلم الآلي.
الأتمتة والبرمجة النصية: تتميز بايثون بأتمتة المهام والبرمجة النصية، وتبسيط العمليات المتكررة.
الذكاء الاصطناعي ومعالجة اللغة الطبيعية: تهيمن بايثون، مع مكتبات مثل TensorFlow، على تطبيقات الذكاء الاصطناعي ومعالجة اللغة الطبيعية.
تطوير الألعاب: يسهل بايثون، جنبًا إلى جنب مع Pygame، تطوير الألعاب ثنائية الأبعاد للهواة والمطورين المستقلين.
الحوسبة العلمية: بايثون هي أداة قيمة في الحوسبة العلمية، اختارها العلماء والباحثون لمكتباتها الشاملة.
يتم تثبيت بايثون مسبقًا في معظم توزيعات Linux. اتبع هذه المقالة لمعرفة كيفية تثبيت بايثون على Windows وMacOS.
كيفية كتابة "Hello, World" في Python
عادةً ما يكون هذا هو الإنجاز الأول عند البدء في كتابة التعليمات البرمجية بأي لغة: أن تقول التعليمات البرمجية الخاصة بك "Hello world". افتح أي محرر تعليمات برمجية من اختيارك، وقم بإنشاء ملف باسم project.py. داخل الملف، اكتب ما يلي:
متغيرات وأنواع بيانات بايثون
أنواع البيانات البدائية (الأساسية):
خصائص أنواع البيانات البدائية:
حالات الاستخدام لأنواع البيانات البدائية:
أنواع البيانات البدائية
script. The print statements display the contents of the name, age, height, and is_student variables.
أنواع البيانات غير البدائية (المركبة) في بايثون
أنواع البيانات غير البدائية هي هياكل يمكنها الاحتفاظ بقيم متعددة وتتكون من أنواع بيانات أخرى، بما في ذلك الأنواع البدائية والأنواع المركبة الأخرى. وعلى عكس أنواع البيانات البدائية، تسمح الأنواع غير البدائية بتمثيلات أكثر تعقيدًا وهيكلة للبيانات.
تتضمن أنواع البيانات غير البدائية ما يلي:
Non-primitive data types include:
- List (
list): Represents an ordered and mutable collection of values. Example:fruits = ["apple", "banana", "cherry"] - Tuple (
tuple): Represents an ordered and immutable collection of values. Example:coordinates = (3, 7) - Dictionary (
dict): Represents an unordered collection of key-value pairs. Example:person = {"name": "Alice", "age": 25, "is_student": True}
خصائص أنواع البيانات غير البدائية:
- القابلية للتغيير: القوائم قابلة للتغيير، مما يعني أنه يمكن تعديل عناصرها بعد إنشائها. من ناحية أخرى، فإن الثنائيات غير قابلة للتغيير - لا يمكن تغيير عناصرها. القواميس قابلة للتغيير - يمكنك إضافة أو تعديل أو إزالة أزواج القيمة والمفتاح.
- جمع القيم: تسمح أنواع البيانات غير البدائية بتجميع قيم متعددة في بنية واحدة، مما يتيح إنشاء تمثيلات بيانات أكثر تعقيدًا.
- مرتبة (القوائم والثنائيات): تحافظ القوائم والثنائيات على ترتيب العناصر، مما يسمح بالفهرسة المتوقعة.
- تعيين القيمة والمفتاح (القاموس): تقوم القواميس بربط المفاتيح بالقيم، مما يوفر طريقة لتنظيم واسترجاع البيانات بناءً على معرفات محددة.
حالات الاستخدام لأنواع البيانات غير البدائية:
- القوائم: مفيدة عندما تحتاج إلى مجموعة يمكن تغييرها أثناء تنفيذ البرنامج، مثل الاحتفاظ بقائمة من العناصر التي قد تتغير بمرور الوقت.
- المجموعات: مناسبة عندما تريد التأكد من أن البيانات تظل ثابتة ولا يمكن تعديلها عن طريق الخطأ. تُستخدم غالبًا لتمثيل مجموعات ثابتة من القيم.
- القواميس: مثالية للسيناريوهات التي تحتاج فيها البيانات إلى الارتباط بعلامات أو مفاتيح محددة. فهي توفر استرجاعًا فعالاً للبيانات استنادًا إلى هذه المعرفات.
Alright, continuing with our Python code – modify the
project.py file as shown below:Run the program to see how lists and tuples allow you to organize and store data. In this code snippet:
- The
fruitsvariable is a list containing strings representing different fruits. - The
coordinatesvariable is a tuple with two integers representing coordinates. - The
personvariable is a dictionary associating keys ("name," "age," "is_student") with corresponding values.
non-primitive data types
You can perform various operations on these structures, such as adding elements to a list or accessing individual items in a tuple.
تُعد أنواع البيانات بالغة الأهمية لعدة أسباب:
- تخصيص الذاكرة: تتطلب أنواع البيانات المختلفة كميات مختلفة من الذاكرة. إن معرفة نوع البيانات يسمح للكمبيوتر بتخصيص الكمية المناسبة من الذاكرة لمتغير.
- العمليات: يدعم كل نوع بيانات عمليات معينة. على سبيل المثال، يمكنك إضافة رقمين صحيحين، أو ربط سلسلتين، أو مقارنة قيمتين منطقيتين.
- منع الأخطاء: قد يؤدي استخدام نوع بيانات خاطئ في عملية ما إلى حدوث أخطاء. تساعد أنواع البيانات في منع العواقب غير المقصودة من خلال فرض قواعد حول كيفية تفاعل الأنواع المختلفة.
Arithmetic Operators:
Arithmetic operators are fundamental components of any programming language, allowing developers to perform basic mathematical operations on numerical values.
In Python, several arithmetic operators enable you to carry out calculations efficiently. Let's explore these operators:
- Addition (+): Adds two operands.
- Subtraction (-): Subtracts the right operand from the left operand.
- Multiplication (*): Multiplies two operands.
- Division (/): Divides the left operand by the right operand (always returns a float).
- Modulus (%): Returns the remainder of the division of the left operand by the right operand.
- Exponentiation (**): Raises the left operand to the power of the right operand.
Modify your project.py file to include examples of these operators:
The code above initializes two variables, num1 and num2, with the values 10 and 3 respectively, representing two numerical operands.
Then, arithmetic operations are performed using these operands:
add_resultstores the result of addingnum1andnum2.sub_resultstores the result of subtractingnum2fromnum1.mul_resultstores the result of multiplyingnum1andnum2.div_resultstores the result of dividingnum1bynum2.mod_resultstores the remainder of dividingnum1bynum2.exp_resultstores the result of raisingnum1to the power ofnum2.
Finally, the results of these arithmetic operations are printed using print() statements, each labeled appropriately, such as "Addition:", "Subtraction:", and so on, followed by the corresponding result.
Here's the output:
arithmetic operations
Comparison Operators
Comparison operators in Python are essential tools for evaluating and comparing values. They enable you to express conditions and make decisions based on the relationship between different values. They return either True or False based on the comparison result.
Here are the common comparison operators:
- Equal to (==): Checks if two operands are equal.
- Not equal to (!=): Checks if two operands are not equal.
- Greater than (>): Checks if the left operand is greater than the right operand.
- Less than (<): Checks if the left operand is less than the right operand.
- Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
- Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
Extend your project.py file to include examples of comparison operators:
The variable age is initialized with the value 25, representing a person's age.
Then, the comparison operator >= is used to evaluate whether age is greater than or equal to 18. The result of this comparison determines the boolean value stored in the variable is_adult. If the age is 18 or older, is_adult will be True, indicating adulthood.
Then the logical operator and is utilized to combine two comparison operations. The first comparison, age >= 13, checks if the age is 13 or older. The second comparison, age < 18, ensures the age is less than 18. If both conditions are true, is_teenager will be True, signifying teenage years.
Finally, the results are printed using print() statements, indicating whether the person is classified as an adult (True or False) and whether they are identified as a teenager (True or False).
Here's the output:
Statements in Python
Statements instruct the interpreter to perform specific actions or operations. These actions can range from simple assignments of values to variables to more complex control flow structures and iterations.
Understanding different types of statements is essential for writing effective and expressive Python code.
Assignment Statements
Assignment statements are the most basic type of statement in Python. They are used to assign values to variables, creating a named reference to data.
Here's an example:
In this snippet, x is assigned the integer value 10, and the name is assigned the string "Alice". These assignments create variables that can be used throughout the program.
Print Statement
The print statement is used to display output in the console. It is a crucial tool for debugging and providing information to users. Example:
This code prints the string "Hello, Python!" to the console.
Conditional Statements (if, elif, else)
Conditional statements are used when you want to execute different blocks of code based on certain conditions.
Suppose you wanted to determine if a person has reached the legal age for drinking. Modify the project.py file with the following code:
In this example:
- The
ifstatement checks ifageis less than 18. - The
elifstatement (shorthand for else if) checks ifageis between 18 (inclusive) and 21 (exclusive). - The
elsestatement is executed if none of the above conditions are met.
if else statement
A person aged 20 is not allowed to drink.
Loops (for and while)
Loops are used to repeat a block of code multiple times. There are two main types of loops in Python: for loops and while loops.
For Loop:
A for loop is used when you know the number of iterations in advance. Suppose you had a list containing the names of fruits, and you wanted to print each fruit. In this case, a for loop is an ideal choice for iterating over the elements of the list.
Here's an example using Python:
In this example, the for loop iterates over each element in the fruits list and prints each fruit.
list of fruits in the for loop
While Loop:
A while statement is a control flow statement that allows you to execute a block of code repeatedly as long as a specified condition is true.
Suppose you want to simulate counting until a certain threshold is reached. Modify your project.py and add the following code:
In this scenario, the while loop continues executing as long as the count variable is less than 5. The code inside the loop increments the count and prints the current count in each iteration.
output of while loop
Break and Continue Statements
Break and continue statements are used within loops.
break: Exits the loop.continue: Skips the rest of the code inside the loop for the current iteration, then continues the loop.Examples:
# Break Statement Example print("Output with 'break':") for i in range(5): if i == 3: print(f"Encountered 'break' at i={i}") break print(i) # Continue Statement Example print("\nOutput with 'continue':") for i in range(5): if i == 2: print(f"Skipped iteration with 'continue' at i={i}") continue print(i)In the
breakexample, the loop stops wheniis equal to 3, and the numbers 0, 1, and 2 are printed.In the
continueexample, wheniis equal to 2, thecontinuestatement skips theprint(i)statement for that iteration, resulting in the omission of the number 2 from the output.break and continue statements output
Functions in Python
Functions are reusable blocks of code, enhancing modularity by enclosing functionality into separate, organized units. This approach helps avoid code duplication and significantly improves code readability.
Inside the
project.pyfile, write the following code:def greet(): print("Hello, World!") # Call the function to execute greet()The code above contains a simple Python function called
greet(). When 'called' or 'invoked', this function prints "Hello, World!" to the console. It's a basic example illustrating how functions work in Python.You can take this a step further by including parameters. Parameters serve as placeholders for values passed to a function during its invocation, allowing functions to accept input and perform operations based on that input.
Modify the previous example on
if elif elsestatement to include functions:def check_age(age): if age < 18: print("You are a minor.") elif 18 <= age < 21: print("You are an adult, but not yet allowed to drink.") else: print("You are a legal adult.") # Call the function with a specific age check_age(20)In this example, the
check_agefunction takes anageparameter and performs the same conditional check as the original code. The function allows you to reuse this logic for different age values by simply calling the function with the desired age.You can call
check_agefunction with any age value, and it will print the appropriate message based on the age provided.functions
Conclusion
Embarking on your Python learning journey, this guide introduces the benefits of learning Python, its key characteristics, and practical use cases.
Starting with the iconic "Hello, World!" and progressing through variables, data types, statements, and functions, you've gained some hands-on experience with basic Python. We also talked about primitive and non-primitive data types, conditional statements, and loops.
Happy coding!




















تعليقات
إرسال تعليق