خطوات تقنية | Meddean.com 📲
39K subscribers
1.28K photos
59 videos
633 files
1.96K links
*مـرحبا ➥
قنـــــــــاة "خــــطـــــــوات تــقــنــيـــة"

هذه القناة تهتم بمواضيع تختص بعالم الحاسوب وانظمة المعلومات وايضا دروس وشروحات متعلقه بالبرمجة
زورونا على موقعنا
خطوات تقنية(برمجية) | AzooTECH.com 📲
ايضاَ
ميدان التقنية | Meddean.com 📲
Download Telegram
درس اليوم في سلسلة تعليم بايثون
🖥🖥🖥🖥
Reusing Code

Code reuse is a very important part of programming in any language. Increasing code size makes it harder to maintain.
For a large programming project to be successful, it is essential to abide by the Don t Repeat Yourself, or DRY, principle. We ve already looked at one way of doing this: by using loops. In this module, we will explore two more: functions and modules.

إعادة استخدام الكود

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



قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com
درس اليوم في بايثون
📱📱📱📱
Functions

In addition to using pre-defined functions, you can create your own functions by using the def statement.
Here is an example of a function named my_func. It takes no arguments, and prints "spam" three times. It is defined, and then called. The statements in the function are executed only when the function is called.


def my_func():
print("spam")
print("spam")
print("spam")

my_func()


>>>
spam
spam
spam
>>>

المهام

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

def my_func():
print("spam")
print("spam")
print("spam")

my_func()


>>>
spam
spam
spam
>>>



قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com
درس اليوم في بايثون
📱📱📱📱
Arguments

All the function definitions we ve looked at so far have been functions of zero arguments, which are called with empty parentheses.
However, most functions take arguments.
The example below defines a function that takes one argument:


def print_with_exclamation(word):
print(word + "!")

print_with_exclamation("spam")
print_with_exclamation("eggs")
print_with_exclamation("python")

>>>
spam!
eggs!
python!
>>>

الحجج

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

def print_with_exclamation(word):
print(word + "!")

print_with_exclamation("spam")
print_with_exclamation("eggs")
print_with_exclamation("python")

>>>
spam!
eggs!
python!
>>>



قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com
Arguments

You can also define functions with more than one argument; separate them with commas.

def print_sum_twice(x, y):
print(x + y)
print(x + y)

print_sum_twice(5, 8)

>>>
13
13
>>>

الحجج

يمكنك أيضًا تعريف الدالات بأكثر من وسيطة واحدة ؛ فصلها مع الفواصل.



قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com
Arguments

Function arguments can be used as variables inside the function definition. However, they cannot be referenced outside of the function s definition. This also applies to other variables created inside a function.

def function(variable):
variable += 1
print(variable)

function(7)
print(variable)

>>>
8

NameError: name variable is not defined
>>>

الحجج

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



قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com
Returning from Functions

Certain functions, such as int or str, return a value that can be used later.
To do this for your defined functions, you can use the return statement.

For example:

def max(x, y):
if x >=y:
return x
else:
return y

print(max(4, 7))
z = max(8, 5)
print(z)

>>>
7
8
>>>

العودة من الوظائف

بعض الدالات ، مثل int أو str ، ترجع قيمة يمكن استخدامها فيما بعد.
للقيام بذلك لوظائفك المحددة ، يمكنك استخدام بيان الإرجاع.

فمثلا:

def max(x, y):
if x >=y:
return x
else:
return y

print(max(4, 7))
z = max(8, 5)
print(z)

>>>
7
8
>>>



قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com
Returning from Functions

Once you return a value from a function, it immediately stops being executed. Any code after the return statement will never happen.
For example:

def add_numbers(x, y):
total = x + y
return total
print("This won t be printed")

print(add_numbers(4, 5))

>>>
9
>>>

العودة من الوظائف

بمجرد إرجاع قيمة من إحدى الوظائف ، يتوقف الأمر فورًا عن تنفيذه. أي رمز بعد بيان العودة لن يحدث أبدا.
فمثلا:

def add_numbers(x, y):
total = x + y
return total
print("This won t be printed")

print(add_numbers(4, 5))

>>>
9
>>>



قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com
Comments

Comments are annotations to code used to make it easier to understand. They don t affect how code is run.
In Python, a comment is created by inserting an octothorpe (otherwise known as a number sign or hash symbol: #). All text after it on that line is ignored.
For example:

x = 365
y = 7
# this is a comment

print(x % y) # find the remainder
# print (x // y)
# another comment

>>>
1
>>>

تعليقات

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

x = 365
y = 7
# this is a comment

print(x % y) # find the remainder
# print (x // y)
# another comment

>>>
1
>>>



قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com
Docstrings

Docstrings (documentation strings) serve a similar purpose to comments, as they are designed to explain code. However, they are more specific and have a different syntax. They are created by putting a multiline string containing an explanation of the function below the function s first line.

def shout(word):
"""
Print a word with an
exclamation mark following it.
"""
print(word + "!")

shout("spam")

>>>
spam!
>>>

جمل التوثيق

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



قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com
Modules

You can import a module or object under a different name using the as keyword. This is mainly used when a module or object has a long or confusing name.
For example:

from math import sqrt as square_root
print(square_root(100))

>>>
10.0
>>>

وحدات

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

from math import sqrt as square_root
print(square_root(100))

>>>
10.0
>>>




قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com
Modules

Trying to import a module that isn t available causes an ImportError.

import some_module

>>>
ImportError: No module named some_module
>>>

وحدات

محاولة استيراد وحدة نمطية غير متوفرة يتسبب ImportError.



قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com
Modules

There is another kind of import that can be used if you only need certain functions from a module.
These take the form from module_name import var, and then var can be used as if it were defined normally in your code.
For example, to import only the pi constant from the math module:

from math import pi

print(pi)

>>>
3.141592653589793
>>>


Use a comma separated list to import multiple objects. For example:

from math import pi, sqrt


وحدات

هناك نوع آخر من الاستيراد يمكن استخدامه إذا كنت تحتاج فقط إلى وظائف معينة من وحدة نمطية.
تأخذ هذه النموذج من module_name import var ، ومن ثم يمكن استخدام var كما لو تم تعريفه بشكل طبيعي في التعليمات البرمجية.
على سبيل المثال ، لاستيراد ثابت pi فقط من وحدة الرياضيات



قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com
Ex1
📱📱📱
Which module is being used in this code?
import math
num = 10
print (math.sqrt(num))

1_. sqrt

2_. math

3_. num



قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com
Modules

Modules are pieces of code that other people have written to fulfill common tasks, such as generating random numbers, performing mathematical operations, etc.

The basic way to use a module is to add import module_name at the top of your code, and then using module_name.var to access functions and values with the name var in the module.
For example, the following example uses the random module to generate random numbers:

import random

for i in range(5):
value = random.randint(1, 6)
print(value)

>>>
2
3
6
5
4
>>>

وحدات

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

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

import random

for i in range(5):
value = random.randint(1, 6)
print(value)
>>>
2
3
6
5
4
>>>


قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com
Functions

Functions can also be used as arguments of other functions.


def add(x, y):
return x + y

def do_twice(func, x, y):
return func(func(x, y), func(x, y))

a = 5
b = 10

print(do_twice(add, a, b))

>>>
30
>>>

المهام

يمكن أيضًا استخدام الدالات كوسيطة للوظائف الأخرى.



قناة خـــطــــوات بـــرمـــجــيــة📱



Teleg: @programmerst
🖥 🌐: https://programmerst.blogspot.com