From be4dbda80ee818be865e931f9ae2c5de919319b6 Mon Sep 17 00:00:00 2001 From: baloola Date: Wed, 29 Jan 2025 01:19:15 +0100 Subject: [PATCH] adding strings & functions lesson --- lessons/beginners-en/def/index.md | 376 +++++++++--------- lessons/beginners-en/def/info.yml | 3 +- lessons/beginners-en/str/index.md | 297 +++++++------- lessons/beginners-en/str/info.yml | 3 +- .../pyladies-en-vienna-2024-spring/info.yml | 36 +- 5 files changed, 360 insertions(+), 355 deletions(-) diff --git a/lessons/beginners-en/def/index.md b/lessons/beginners-en/def/index.md index bed4530a13..6f7e2993c0 100644 --- a/lessons/beginners-en/def/index.md +++ b/lessons/beginners-en/def/index.md @@ -1,8 +1,9 @@ -# Functions -In the one of the past [lessons]({{ lesson_url('beginners-en/functions') }}) -we were working with functions that were written by someone else - they were -already built-in in Python - `print` or we imported them from a module - `turtle` for example. + # الدوال (functions) + +في أحد [الدروس]({{ lesson_url('beginners-en/functions') }}) السابقة +كنا نعمل مع دوال كتبها شخص آخر - كانت +مضمنة بالفعل في Python - `print` أو قمنا باستيرادها من وحدة - `turtle` على سبيل المثال. ```python from math import pi @@ -10,82 +11,77 @@ from math import pi print(pi) ``` -Today we will learn how to code our own functions -which will be helpful when we need to run tasks repeatedly. +اليوم سوف نتعلم كيفية برمجة دوال خاصة بنا +وهو ما سيكون مفيدًا عندما نحتاج إلى تشغيل المهام بشكل متكرر. -It's not hard: +الأمر ليس صعبًا: ```python -def find_perimeter(width, height): - "Returns the rectangle's perimeter of the given sides" +def find_perimeter(width, height): + "Returns the rectangle's perimeter of the given sides" return 2 * (width + height) print (find_perimeter(4 , 2)) ``` -How does it work? - -You *define* a function with the command `def`. Right after that -you have to write name of the function, parentheses (which may contain -*arguments*) and then, of course, a *colon*. - +## كيف يمكن فعل ذلك؟ -We have already said that after a colon, everything that -belongs to (in our case) the function must be indented. -The indented code is called *function body* and it contains -the commands that the function performs. -In the body you can use various commands including `if`, `loop`, etc. +*تعرف* دالة باستخدام الأمر `def`. مباشرة بعد ذلك +يجب عليك كتابة اسم الدالة ، والأقواس (التي قد تحتوي على +*قيم (arguments)*) ثم ، بالطبع ، *فاصلة منقوطة*. -The body can start with a *documentation comment* or so called *docstring* which describes what -the function is doing. -A function can return a value with the `return` command. More on that later. +لقد قلنا بالفعل أنه بعد الفاصلة المنقوطة ، كل شيء +ينتمي إلى الدالة (في حالتنا) يجب ان يفصل بالمساحات (indented) . +يُطلق على الكود المفصول بالمساحات *جسم الدالة* ويحتوي على +الأوامر التي تؤديها الدالة. +يمكن أن يبدأ الجسم بـ *تعليق توثيق* أو ما يسمى *docstring* والذي يصف ما +تفعله الوظيفة. +يمكن للدالة إرجاع قيمة باستخدام الأمر `return`. المزيد عن ذلك لاحقا. ```python -def print_score(name, score): - print(name, 'score is', score) +def print_score(name, score): + print(name, 'score is', score) if score > 1000: - print('World record!') - elif score > 100: - print('Perfect!') - elif score > 10: - print('Passable.') - elif score > 1: - print('At least something. ') - else: + print('World record!') + elif score > 100: + print('Perfect!') + elif score > 10: + print('Passable.') + elif score > 1: + print('At least something. ') + else: print('Maybe next time. ') -print_score('Your', 256) +print_score('Your', 256) print_score('Denis', 5) ``` -When you call a function, the arguments you write in parentheses -are assigned to the corresponding variables in the function definition's -parentheses. +عندما تستدعي دالة ، يتم تعيين القيم التي تكتبها بين قوسين +إلى المتغيرات المقابلة في أقواس تعريف الدالة. -So when you call our new function with `print_score('Your', 256)`, -imagine that, internally, it assigns the values like this: +لذلك عندما تستدعي دالتنا الجديدة مع `print_score('Your', 256)` ، +تخيل أنه ، داخليًا ، يعين القيم مثل هذا: ```python name = 'Your' score = 256 -print(name, 'score is', score) +print(name, 'score is', score) if score > 1000: ... #etc. - -``` -## Return -The `return` command *terminates* the function immediately and returns the calculated value -out of the function. You can use this command only in functions! +``` +## الإرجاع (Return) -It behaves similar to the `break` command that terminates loops. +يُنهي أمر `return` الدالة على الفور ويرجع القيمة المحسوبة +خارج الدالة. يمكنك استخدام هذا الأمر فقط في الدوال! +يتصرف بشكل مشابه للأمر `break` الذي ينهي الحلقات. ```python def yes_or_no(question): @@ -107,46 +103,45 @@ else: ``` > [note] -> Same as `if` and `break`, `return` is a Python *command*, not a function. -> That's why `return` has no parentheses after it. +> مثل `if` و `break` ، `return` هو أمر Python *، وليس دالة*. +> لهذا السبب لا يوجد `return` أقواس بعده. -Try to write a function that returns the area of an ellipse with given -dimensions. -The formula is A = πab, -where a and b are the lengths of the axes. -Then call the function and print the result. +حاول كتابة دالة ترجع مساحة الشكل البيضاوي بأبعاد معينة. +الصيغة هي A = πab ، +حيث a و b هما أطوال المحاور. +ثم قم باستدعاء الدالة وطباعة النتيجة. {% filter solution %} ```python from math import pi -def ellipse(a, b): +def ellipse(a, b): return pi * a * b - + print('The ellipsis area with 3 cm and 5 cm axes length is', ellipse(3, 5),'cm2.') ``` {% endfilter %} -### Return or print? +### الإرجاع أو الطباعة؟ (Return or print?) -The last program could be also written like that: +يمكن أيضًا كتابة البرنامج الأخير مثل هذا: ```python from math import pi -def ellipse(a, b): +def ellipse(a, b): print('The area is', pi * a * b) # Caution, 'print' instead of 'return'! - + ellipse(3, 5) ``` -The program works this way, too. But it loses one of the main advantages -that functions have - when you want to use the value differently than to `print` it. +يعمل البرنامج بهذه الطريقة أيضًا. لكنه يخسر إحدى المزايا الرئيسية +التي تتمتع بها الدوال - عندما تريد استخدام القيمة بشكل مختلف عن `print` لها. -A function that *returns* its result can be used as part of other calculations: +الدالة التي *تعيد* نتيجتها يمكن استخدامها كجزء من حسابات أخرى: ```python @@ -156,45 +151,48 @@ def elliptical_cylinder(a, b, height): print(elliptical_cylinder(3, 5, 3)) ``` -But if our ellipse function just *printed* the result, we wouldn't be -able to calculate the area of elliptical cylinder this way. -The reason why `return` is better than `print` is that a function -can be re-used in many different situations. When we don't actually -want to know the intermediate results, we can't use functions with `print`. +ولكن إذا كانت دالة الشكل البيضاوي لدينا *تطبع* النتيجة فقط ، فلن نتمكن بذلك + من حساب مساحة الاسطوانة البيضاوية. -It is similar with input: If I hardcoded `input` into a function, I could use -it only in situations where there's a user with keyboard present. -That's why it's always better to pass arguments to a function, and call -`input` outside of the function: +السبب في أن `return` أفضل من `print` هو أنه يمكن إعادة استخدام الدالة +في العديد من المواقف المختلفة. عندما لا نريد في الواقع +معرفة النتائج الوسيطة ، لا يمكننا استخدام الدوال مع `print`. + +يشبه ذلك الإدخال: إذا قمت بكتابة `input` الثابت في دالة ، يمكنني استخدامه +فقط في المواقف التي يوجد فيها مستخدم مع لوحة مفاتيح موجودة. +لهذا السبب من الأفضل دائمًا تمرير القيم إلى دالة ، واستدعاء +`input` خارج الدالة: ```python from math import pi -def ellipse(a, b): +def ellipse(a, b): """This reusable function returns only the result - the ellipse's area with a and b axes""" #This is only the calculation return pi * a * b - + #print and input are "outside" the reusable function! x = float(input('Enter length of 1st axis: ')) y = float(input('Enter length of 2nd axis: ')) print('The ellipsis area is', ellipse(x, y),'cm2.') ``` -There are of course exceptions: A function that directly generates -a text can be written with `print`, or a function that processes text information. -But when the function calculates something it's better to not have -`print` and `input` inside it. +`input` خارج الدالة: +بالطبع هناك استثناءات: يمكن كتابة دالة تولد مباشرةً +نصًا باستخدام `print` ، أو دالة تعالج معلومات النص. +ولكن عندما تحسب الدالة شيئًا ما ، فمن الأفضل عدم وجود +`print` و `input` بداخلها. -## None -When the function run does not end with an explicit `return`, -the value that it returns is automatically `None`. +## لا شيء (None) -`None` is a value that is already "inside" Python (same as `True` and `False`). -It's literally "none, nothing". +عندما لا ينتهي تشغيل الدالة بـ `return` صريح ، +القيمة التي تعيدها هي تلقائيًا `None`. + +`None` هي قيمة موجودة بالفعل "داخل" Python (مثل `True` و `False`). +إنها حرفيًا "لا شيء ، لا شيء". ```python def nothing(): @@ -205,12 +203,12 @@ print(result) # returns None print(result is None) # returns True ``` -## Local variables +## المتغيرات المحلية (Local variables) -Congratulations! You can now define your own functions! -Now we have to explain what local and global variables are. +تهانينا! يمكنك الآن تعريف دالتك الخاصة! +الآن يجب أن نشرح ما هي المتغيرات المحلية(local) والعالمية(global). -A function can use variables from "outside": +يمكن للدالة استخدام متغيرات من "الخارج": ```python pi = 3.1415926 # a variable defined outside the function @@ -221,13 +219,13 @@ def circle_area(radius): print(circle_area(100)) ``` -But every variable and argument that is defined in the function body are -*brand new* and they share nothing with the "outside" variables. +ولكن كل متغير(variable) و قيمة(argument) يتم تعريفها في جسم الدالة هي +*جديدة تمامًا* ولا تشارك أي شيء مع المتغيرات "الخارجية". -Variables that are defined inside a function body are called *local variables*, -because they work only locally inside the function. +المتغيرات التي يتم تعريفها داخل جسم الدالة تسمى *المتغيرات المحلية* ، +لأنها تعمل فقط محليًا داخل الدالة. -For example, the following won't work how you would expect: +على سبيل المثال ، لن يعمل ما يلي كما تتوقع: ```python x = 0 # Assign value to global variable x @@ -239,17 +237,14 @@ set_x(40) print(x) ``` -Variables that are not local are *global variables* - -they exist throughout the whole program. - -If a function defines a local variable with the same name, this local variable will only -have the value that was assigned in the function. +إذا قامت دالة بتعريف متغير محلي بنفس الاسم ، فسيكون لهذا المتغير المحلي فقط +قيمة داخل الدالة. -Let's look at an example. -Before you run the next program, try to guess how it will behave. -Then run it, and if it did something different than -you expected, try to explain why. -There is a catch! :) +لنلقي نظرة على مثال. +قبل تشغيل البرنامج التالي ، حاول تخمين كيف سيتصرف. +ثم قم بتشغيله ، وإذا فعل شيئًا مختلفًا عن +ما كنت تتوقعه ، حاول أن تشرح السبب. +هناك خدعة! :) ```python from math import pi @@ -266,36 +261,28 @@ print(area) print(a) ``` -Now try to answer the following questions: -* Is the variable `pi` local or global? -* Is the variable `area` local or global? -* Is the variable `a` local or global? -* Is the variable `b` local or global? +الآن حاول الإجابة على الأسئلة التالية: + +* هل المتغير `pi` محلي أم عالمي؟ +* هل المتغير `area` محلي أم عالمي؟ +* هل المتغير `a` محلي أم عالمي؟ +* هل المتغير `b` محلي أم عالمي؟ {% filter solution %} -* `pi` is global - it's not defined within the function and it's -accessible in the whole program. -* `area` - Note there are two variables of that name! One is global -ant the other one is local inside the function `ellipse_area`. -* `a` - Note there are also two variables of that name. This was that catch: -Writing `a = a + 3` has no point. A value is assigned to the local -variable `a`, but the function ends right after that, and this `a` is no -longer available, it will never be used. -* `b` is only local - it's an argument for the `ellipse_area` function. +* `pi` عالمي - لم يتم تعريفه داخل الدالة وهو +متاح في البرنامج بأكمله. +* `area` - لاحظ أن هناك متغيرين بهذا الاسم! واحد عالمي +والآخر محلي داخل الدالة `ellipse_area`. +* `a` - لاحظ أيضًا وجود متغيرين بهذا الاسم. هذا هو المقصود بالخدعة: +كتابة `a = a + 3` ليس لها معنى. يتم تعيين قيمة للمتغير المحلي +`a` ، ولكن تنتهي الدالة مباشرة بعد ذلك ، والمتغير `a` لن يكون +متاحًا بعد الآن ، فلذلك لن يتم استخدامه أبدًا. +* `b` محلي فقط - إنه قيمة(argument) لدالة `ellipse_area`. {% endfilter %} -If it seems confusing and complicated just avoid naming variables (and -function's arguments) within a function the same as those outside. - -## Arguments, their count and defaults - -If you define a function with a certain number of `positional arguments`, then the calling of the -function needs to pass all of these arguments as well. Following will happen if too little -arguments are passed in. - ```python def say_name_5(first_name, last_name): for i in range(5): @@ -307,16 +294,20 @@ say_name_5("Eva") # TypeError: say_name_5() missing 1 required positional argument: 'last_name' ``` -But in this case, we should not be forced to input both First name and Surname. -There are powerful and well known people without a Surname, that would not be able to use our -function without a workaround - like setting their surname to empty string `""`. +إذا قمت بتعريف دالة بعدد معين من *القيم* ، فإن استدعاء +الدالة تحتاج إلى تمرير جميع هذه القيم أيضًا. سيحدث ما يلي إذا تم تمرير القليل جدًا +من القيم. + +ولكن في هذه الحالة ، يجب ألا نضطر إلى إدخال كل من الاسم الأول واسم العائلة. +هناك أشخاص معروفون بدون اسم عائلة ، في هذه الحالة لن نتمكن من استخدام +الدالة الا بحل بديل - مثل تعيين اسم العائلة إلى نص فارغ `""`. -### Defaults +### الافتراضيات (Defaults) -It is possible (although it does not always make sense) to add `default` argument values -which will get used if the function call does not have these arguments specified. +من الممكن (على الرغم من أنه لا معنى له دائمًا) إضافة قيم *افتراضية* للقيم +التي سيتم استخدامها إذا لم يحدد استدعاء الدالة هذه القيم. -For specifying default argument, we use `argument=value` syntax in the function definition: +لتحديد القيمة الافتراضية ، نستخدم بناء الجملة `argument=value` في تعريف الدالة: ```python def say_name_5(first_name="", last_name=""): @@ -331,7 +322,7 @@ say_name_5() ``` -A problem also appears in case we supply more arguments than the function allows: +تظهر مشكلة أيضًا في حالة استدعائنا لدالة و استخدام قيم اكثر مماتسمح به الدالة: ```python say_name_5("Eva", "Blasco", "of Royal Court!") @@ -339,15 +330,13 @@ say_name_5("Eva", "Blasco", "of Royal Court!") # TypeError: say_name_5() takes 2 positional arguments but 3 were given ``` -Remember that for positional arguments, the order in which they are passed in is important. +## التكرار (Recursion) -## Recursion +*التكرار* هو تقنية برمجة ، +عندما تستدعي دالة نفسها. -*Recursion* is a programming technique, -when a function calls itself. - -Such a recursion will end in an infinite call. -When you enter this program: +مثل هذا التكرار سينتهي في مكالمة لا نهائية. +عندما تدخل هذا البرنامج: ```python def recursive_function(): @@ -358,24 +347,24 @@ def recursive_function(): recursive_function() ``` -How does it work? - -* Python defines a function `recursive_function` -* Calls the function `recursive_function`: - * Calculates the result - * Calls the function `recursive_function`: - * Calculates the result - * Calls the function `recursive_function`: - * Calculates the result - * Calls the function `recursive_function`: - * Calculates the result - * Calls the function `recursive_function`: +كيف يعمل؟ + +* يعرف Python دالة `recursive_function` +* يستدعي الدالة `recursive_function`: + * يحسب النتيجة + * يستدعي الدالة `recursive_function`: + * يحسب النتيجة + * يستدعي الدالة `recursive_function`: + * يحسب النتيجة + * يستدعي الدالة `recursive_function`: + * يحسب النتيجة + * يستدعي الدالة `recursive_function`: * ... * ... - * after hundreds of iterations, Python notices that this - leads nowhere, and ends up with an error. + * بعد مئات من التكرارات ، يلاحظ Python أن هذا + يؤدي إلى لا شيء ، وينتهي بخطأ. -The error message corresponds to this: +تتوافق رسالة الخطأ مع هذا: ``` Traceback (most recent call last): @@ -391,22 +380,23 @@ Traceback (most recent call last): RecursionError: maximum recursion depth exceeded ``` -## Controlled nesting +## التداخل المتحكم فيه (Controlled nesting) + +كيف تستخدم التكرار عمليا؟ -How to use recursion in practice? -One way is to count how many more times to "dive". +يمكن فهم ذلك باستخدام مثال "الغوص". -Imagine a diver exploring the depths of the sea in the following way: +تخيل غواصًا يستكشف أعماق البحر بالطريقة التالية: -* How to *"explore the sea"* at a certain depth: - * I look around - * If I'm already in too deep, I am scared. I will not explore further. - * Otherwise: - * I dive 10 m lower - * *I will explore the sea* at a new depth - * I submerge again in 10 m +* كيف يتم "استكشاف البحر" عند عمق معين: + * أنظر حولي + * إذا كنت بالفعل في عمق كبير جدًا ، فأنا خائف. لن أستكشف المزيد. + * غير ذلك: + * أغوص 10 م أقل + * *سأستكشف البحر* على عمق جديد + * أغوص مرة أخرى في 10 م -Or in Python: +أو في Python: ```python def explore(depth): @@ -422,37 +412,41 @@ explore(0) ``` ``` -* Python defines the function ``explore'' -* Calls the `explore` function with a depth of 0: - * Prints `I'm looking around at a depth of 0 m' - * Checks that `0 ≥ 30` (which is not true) - * Prints ``I dive more (from 0 m)'' - * Calls the `explore` function with a depth of 10 m: - * Writes `I look around at a depth of 10 m' - * Checks that `10 ≥ 30` (which is not true) - * Writes ``'I dive more (from 10 m)'' - * Calls the `explore` function with a depth of 20m: - * Checks that `20 ≥ 30` (which is not true) - * Writes ``I dive more (from 30 m)'' - * Calls the `explore` function with a depth of 30 m: - * Checks that `30 ≥ 30` (which is true! finally!) - * Prints ``Enough! I have seen it all!'' - * and ends - * Prints ``Surfacing (at 20 m)'' - * Prints ``Surfacing (at 10m)'' - * Prints ``Surfacing (at 0 m)'' +* يحدد بايثون الدالة "explore" +* يستدعي الدالة "explore" بعمق 0: +* يطبع "I'm looking around at a depth of 0 m" +* يتحقق من أن "0 ≥ 30" (وهو ليس صحيحًا) +* يطبع "I dive more (from 0 m)" +* يستدعي الدالة "explore" بعمق 10 أمتار: +* يكتب "I look around at a depth of 10 m" +* يتحقق من أن "10 ≥ 30" (وهو ليس صحيحًا) +* يكتب "I dive more (from 10 m)" +* يستدعي الدالة "explore" بعمق 20 مترًا: +* يتحقق من أن "20 ≥ 30" (وهو ليس صحيحًا) +* يكتب "I dive more (from 30 m)" +* يستدعي الدالة "explore" بعمق 20 مترًا: +* يتحقق من أن "20 ≥ 30" (وهو ليس صحيحًا) +* يكتب "I dive more (from 30 m)" +* يستدعي الدالة "explore" بعمق 20 مترًا: +* وظيفة `explore` بعمق 30 مترًا: +* تتحقق من أن `30 ≥ 30` (وهو صحيح! أخيرًا!) +* تطبع ``كفى! لقد رأيت كل شيء!'' +* وتنتهي +* تطبع ``Surfacing (at 20 m)'' +* تطبع ``Surfacing (at 10m)'' +* تطبع ``Surfacing (at 0 m)'' ``` -## Factorial +## عاملي (Factorial) -Recursive algorithms have their origins in mathematics. The factorial of x, or -the product of all numbers from 1 to x, written as x!, -mathematicians define as follows: +الخوارزميات التكرارية مستوحاة في الاساس من الرياضيات. عاملي x ، أو +ناتج ضرب جميع الأعداد من 1 إلى x ، مكتوبًا كـ x! ، +يعرفه علماء الرياضيات على النحو التالي: * 0! = 1 -* For positive x is x! = x · (x - 1)! +* اذا كانت قيمة x موجبة :
!x! = x · (x - 1)
+أو في Python: -Or in Python: ```python def factorial(x): diff --git a/lessons/beginners-en/def/info.yml b/lessons/beginners-en/def/info.yml index 0c04d630d5..9157352f5b 100644 --- a/lessons/beginners-en/def/info.yml +++ b/lessons/beginners-en/def/info.yml @@ -1,6 +1,7 @@ -title: Custom functions +title: الدوال (Custom functions) style: md attribution: +- Translated by Mussab Abdalla - Translated by Marketa Muzikova and Atul Shurma - | Originally written by Petr Viktorin, 2014-2017 for [PyLadies CZ]. diff --git a/lessons/beginners-en/str/index.md b/lessons/beginners-en/str/index.md index dc02f51357..cd18d79f82 100644 --- a/lessons/beginners-en/str/index.md +++ b/lessons/beginners-en/str/index.md @@ -1,40 +1,42 @@ -# Strings +# النصوص (Strings) -Now we will learn about strings. -You already know how to write them in Python code. +الآن سنتعلم عن النصوص. +أنت تعرف بالفعل كيفية كتابتها في كود Python. ```python 'This is string' "And this is also string" ``` -Sometimes you will need a string that is multiple lines long. -But you can write strings only on one line in Python -(you can actually write on more lines but the text would -appear on just one). +الآن سنتعلم عن النصوص. +أنت تعرف بالفعل كيفية كتابتها في كود Python. -In your text, you can use a special character that means -new line `\n`: +في بعض الأحيان ستحتاج إلى نص طويل من الأسطر. +ولكن يمكنك كتابة نصوص على سطر واحد فقط في Python +(يمكنك بالفعل الكتابة على المزيد من الأسطر ولكن النص سيظهر على سطر واحد فقط). + +في النص الخاص بك ، يمكنك استخدام حرف خاص يعني +سطر جديد `\n`: ```python print('Hello word\nHow are you?') ``` -A backslash allows us to write characters which we can't easily -write on the keyboard. +تسمح لنا الشرطة المائلة العكسية (backslash) بكتابة الأحرف التي لا يمكننا كتابتها بسهولة +على لوحة المفاتيح. -The backslash also allows us to use both types of quotes in one piece of text. +تسمح لنا الشرطة المائلة العكسية(backslash) أيضًا باستخدام كلا نوعي الاقتباسات ( " و ' ) في جزء واحد من النص. ```python print('"Don\'t do it", said dad.') print("\"Don't do it\", said dad.") ``` -Backward slashes can also insert exotic characters -that you do not have on the keyboard. -Fancy characters can be written as `\N` and a character -name in compound ("curly") braces. -Try for example the following characters -(some might not work for your system): +يمكن للشرطة المائلة العكسية أيضًا إدراج أحرف غريبة +ليس لديك على لوحة المفاتيح. +يمكن كتابة الأحرف المميزة كـ `\N` واسم حرف +في الأقواس المركبة ("{}"). +جرب على سبيل المثال الأحرف التالية +(قد لا يعمل البعض على نظامك): ```python print('--\N{LATIN SMALL LETTER L WITH STROKE}--') @@ -45,27 +47,23 @@ print('--\N{SNOWMAN}--') print('--\N{KATAKANA LETTER TU}--') ``` -If you want to write a backslash character in your text -you have to write it twice (for example, in a path to a -file in Windows). -So the sequence `\\` means one backslash. +إذا كنت تريد كتابة حرف الشرطة العكسية (backlash) في النص الخاص بك، فيجب عليك كتابته مرتين (على سبيل المثال، في مسار إلى ملف في Windows). +لذا فإن التسلسل `\\` يعني شرطة عكسية واحدة. ```python -print('C:\\PyLadies\\New Folder') +print('C:\\Khartoum\\New Folder') ``` -But back to multi-line strings. There is also another way how to write them -in Python. You just have to wrap them in *three* single -or *three* double quotes: +ولكن دعنا نعود إلى النصوص متعددة الأسطر. هناك أيضًا طريقة أخرى لكتابتها في بايثون. كل ما عليك فعله هو وضعها بين *ثلاث* علامات اقتباس مفردة أو *ثلاث* علامات اقتباس مزدوجة: ```python basen = '''Hello World! How are you?''' ``` -Programmers also use three quotes to document their functions. -It is called a `docstring` (documentation string). -You do not need to worry about how to define a function, the next lecture will be aimed directly at this topic. +يستخدم المبرمجون أيضًا ثلاث علامات اقتباس لتوثيق دوالهم. +ويطلق على ذلك اسم `docstring` (نص توثيق). +لا داعي للقلق بشأن كيفية تعريف دالة، فالمحاضرة القادمة ستركز بشكل مباشر على هذا الموضوع. ```python def multiply(a, b): @@ -77,124 +75,132 @@ def multiply(a, b): return a * b ``` -However you can use the inbuilt `help()` function on any function to see its `"docstring"`. -In order to leave the newly opened help window, press `q` as `quit`. - -Now we will have a look at how to work with strings. +ومع ذلك، يمكنك استخدام الدالة المضمنة (inbuilt) `()help` في أي دالة لرؤية ``docstring`` الخاصة بها. +لمغادرة نافذة التعليمات المفتوحة حديثًا، اضغط على `q` كـ `quit`. +سنلقي الآن نظرة على كيفية العمل مع النصوص. -## Subscripting +## تجزئة النصوص (Subscripting) -You already know how to concatenate strings by addition and multiplication. +أنت تعرف بالفعل كيفية ربط النصوص عن طريق الجمع والضرب. ```python concatenated_string = 'a' + 'b' long_string = 'o' * 100 ``` -Now we will learn how we can get part of a string. -We will start with single characters. -This is done by *subscripting*. The Syntax looks similar -to calling a function but with *square brackets*! +ومع ذلك، يمكنك استخدام الدالة المضمنة (inbuilt) `()help` في أي دالة لرؤية ``docstring`` الخاصة بها. +لمغادرة نافذة التعليمات المفتوحة حديثًا، اضغط على `q` كـ `quit`. + +سنلقي الآن نظرة على كيفية العمل مع النصوص. + +## تجزئة النصوص (Subscripting) + +أنت تعرف بالفعل كيفية ربط النصوص عن طريق الجمع والضرب. + +الآن سنتعلم كيفية الحصول على جزء من نص. +سنبدأ بالأحرف المفردة. +يتم ذلك عن طريق *تجزئة النص (Subscripting)*. تبدو البنية النحوية (syntax) مشابهة +لاستدعاء دالة ولكن باستخدام *أقواس مربعة*! ```python -fifth_character = 'PyLadies'[5] +fifth_character = 'Khartoum'[5] print(fifth_character) ``` -Does it work? Did you really get the fifth character? +هل يعمل؟ هل حصلت حقًا على الحرف الخامس؟ {% filter solution %} -You didn't – you got the *sixth* character. +لم تفعل - لقد حصلت على الحرف *السادس*. {% endfilter %} -As you may have already noticed, programmers in Python start counting from zero. -First comes 0, then 1, and so on. +كما ربما لاحظت بالفعل ، يبدأ المبرمجون في Python بالعد من الصفر. +يأتي الأول 0 ، ثم 1 ، وهكذا. -It's the same with strings - the first character is on position zero. +الأمر نفسه ينطبق على النصوص - الحرف الأول في الموضع صفر. -Why is it like that? -You would have to know about pointers and arrays -to fully understand, so now let's just assume -that programmers are weird. Or that they just like -weird numbers. +لماذا هو هكذا؟ +يجب أن تعرف عن المؤشرات (pointers) والمصفوفات (arrays) +لفهمها تمامًا ، لذلك دعونا نفترض الآن +أن المبرمجين غريبو الأطوار. أو أنهم يحبون فقط +الأرقام الغريبة. ```plain [0] [1] [2] [3] [4] [5] [6] [7] ╭───┬───┬───┬───┬───┬───┬───┬───╮ - │ P │ y │ L │ a │ d │ i │ e │ s │ + │ K │ h │ a │ r │ t │ o │ u │ m │ ╰───┴───┴───┴───┴───┴───┴───┴───╯ ``` -What happens if you pick characters with negative numbers? +ماذا يحدث إذا اخترت أحرفًا بأرقام سالبة؟ {% filter solution %} ```python -print('PyLadies'[-1]) # → s -print('PyLadies'[-2]) # → e -print('PyLadies'[-3]) # → i -print('PyLadies'[-4]) # → d +print('Khartoum'[-1]) # → m +print('Khartoum'[-2]) # → u +print('Khartoum'[-3]) # → o +print('Khartoum'[-4]) # → t ``` -Negative numbers pick characters from the end. +الأرقام السلبية تختار الأحرف من النهاية. ```plain [0] [1] [2] [3] [4] [5] [6] [7] [-8][-7][-6][-5][-4][-3][-2][-1] ╭───┬───┬───┬───┬───┬───┬───┬───╮ - │ P │ y │ L │ a │ d │ i │ e │ s │ + │ K │ h │ a │ r │ t │ o │ u │ m │ ╰───┴───┴───┴───┴───┴───┴───┴───╯ ``` {% endfilter %} -Strings can do more tricks. -You can find out how long the string is, -or if it contains a certain substring. +يمكن للنصوص القيام بالمزيد من الحيل. +يمكنك معرفة طول النص ، +أو إذا كان يحتوي على نص فرعية معينة. - - - + + + - - + + - - + + - - + +
CodeDescriptionExampleالرمزالوصفمثال
len(r)Length of stringlen('PyLadies')طول النصlen('Khartoum')
x in rTrue if the string x is in the string r'Ladies' in 'PyLadies'True إذا كان النص x موجود في النص r'toum' in 'Khartoum'
x not in rThe opposite of x in r'eye' not in 'PyLadies'عكس x in r'eye' not in 'Khartoum'
-Python is case sensitive, so `'ladies' in 'PyLadies'` -is `False`. If you want to do a case insensitive test, -you would have to change the case of both strings -(both to lower, or both to upper) and then do `x in y`. +Python حساس لحالة الأحرف ، لذلك `'Toum' in 'Khartoum'` +تعطي نتيجة `False`. إذا كنت تريد إجراء اختبار غير حساس لحالة الأحرف ، +عليك تغيير حالة النصين +(كلاهما إلى lower ، أو كلاهما إلى upper) ثم قم بإجراء `x in y`. -And how to change the case of our string? -To do that, we will need another Python feature: methods. +وكيفية تغيير حالة نصنا؟ +لذلك ، سنحتاج إلى ميزة Python أخرى: الطرق(Methods). -## Methods +## الطرق (Methods) -*Methods* are like functions - we can call something with them. -Unlike a function, a method is tied to some *object*. -It is called by writing a `dot` and a method name just after the object. -And then, of course, parentheses, which may contain arguments. +*الطرق (Methods)* تشبه الدوال - يمكننا استدعاء شيء ما باستخدامها. +على عكس الدالة ، فإن الطريقة مرتبطة ببعض ال *objects*. +يتم استدعاؤها عن طريق كتابة `نقطة` واسم الطريقة بعد ال object مباشرةً. +وبعد ذلك ، بالطبع ، أقواس ، والتي قد تحتوي على وسيطات. -The String methods `upper()` and `lower()` change the case. +تغير طريقتا النص `upper()` و `lower()` الحالة. ```python string = 'Hello' @@ -203,27 +209,26 @@ print(string.lower()) print(string) ``` -> [note] -> Notice that the original string has not changed. -> Methods return a new string and the old string stays -> as it was. +> [ملاحظة] +> لاحظ أن النص الأصلي لم يتغير. +> تعيد الطرق نصًا جديدًا ويبقى النص القديم +> كما كان. > -> That is Python's standard behavior: already existing string can't be changed, -> we can only create a new one - derived from the old one. +> هذا هو سلوك Python القياسي: لا يمكن تغيير النص الموجود بالفعل ، +> يمكننا فقط إنشاء نص جديد - مشتق من النص القديم. > -> But it is not true that all methods do not change the original object they are being called on. -> This is true just for strings. +> لكن ليس صحيحًا أن جميع الطرق لا تغير ال object الأصلي الذي يتم استدعاؤه عليه. +> هذا صحيح فقط بالنسبة للنصوص. -### Initials +### الأحرف الأولى (Initials) -For practicing methods and subscripting, try to write a program, -which will ask the user for their name, then their surname -and then it will print their *initials* - the first letter of -name and surname. +لممارسة الطرق وتجزئة النص (subscripting) ، حاول كتابة برنامج ، +الذي سيطلب من المستخدم اسمه ، ثم اسم عائلته +ثم سيقوم بطباعة *الحروف الأولى* له - الحرف الأول من +الاسم واسم العائلة. -Initials are always upper case (even if the -user won't write it that way). +الحروف الأولى دائمًا بأحرف كبيرة (حتى إذا لم يكتبها المستخدم بهذه الطريقة). {% filter solution %} ```python @@ -233,10 +238,10 @@ initials = name[0] + surname[0] print('Initials:', initials.upper()) ``` -There are more ways how to write such a program. -You can call `upper()` twice - on name and surname separately. +هناك طرق أخرى لكتابة مثل هذا البرنامج. +يمكنك استدعاء `upper()` مرتين - على الاسم واسم العائلة بشكل منفصل. -Or like this: +أو مثل هذا: ```python name = input('Enter your name: ') @@ -244,44 +249,48 @@ surname = input('Enter your surname: ') print('Initials:', (name[0] + surname[0]).upper()) ``` -I recommend the first option. It is longer but way more clear. +ننصحك بالخيار الأول، فهو أطول ولكنه أكثر وضوحًا. {% endfilter %} -There are many more string methods. You can find the most -useful ones in our [cheatsheet](https://github.com/muzikovam/cheatsheets/blob/master/strings/strings-en.pdf). -All methods are in the [Python documentation](https://docs.python.org/3/library/stdtypes.html#string-methods). +هناك العديد من طرق النصوص الأخرى. يمكنك العثور على أكثرها +فائدة في [ورقة الغش الخاصة بنا](https://github.com/muzikovam/cheatsheets/blob/master/strings/strings-en.pdf). + +جميع الطرق موجودة في [وثائق Python](https://docs.python.org/3/library/stdtypes.html#string-methods). -Notice that `len` isn't a method but a function. It's -written `len(s)` not `r.len()`. -You will find out why it is like that in a minute. +لاحظ أن `len` ليست طريقة ولكنها دالة. كتبت `len(s)` وليس `()r.len` , +ستكتشف لماذا الأمر كذلك في دقيقة. -## Formatting +## التنسيق (Formatting) + +من المفيد بشكل خاص طريقة `format` ، والتي تحل محل +زوج من الأقواس المجعدة `{}` في النص لأي شيء +يحصل عليه كوسيطة. -Especially useful is the `format` method, which replaces -a pair of curly braces in string for whatever it -gets as an argument. ```python write = '{}×{} equals {}'.format(3, 4, 3 * 4) print(write) ``` -The String `'{}×{} equals {}'` is something like a *template*. -Imagine it as form, where we have highlighted fields where Python -fills in values. +من المفيد بشكل خاص طريقة `format` ، والتي تحل محل +زوج من الأقواس المجعدة في النص لأي شيء +يحصل عليه كوسيطة. + +النص `'{}×{} equals {}'` يشبه نوعًا من *القالب او النموذج (Template)*. +تخيلها كنموذج ، حيث قمنا بتمييز الحقول التي يملأ Python +القيم فيها. -If you want to fill values in a different order, or you want -the template to be clearer, you can write variables into your -curly braces: +إذا كنت تريد ملء القيم بترتيب مختلف ، أو كنت تريد +أن يكون القالب أكثر وضوحًا ، يمكنك كتابة المتغيرات في +الأقواس المجعدة الخاصة بك: ```python write = 'Hi {name}! The result is {number}.'.format(number=7, name='Mary') print(write) ``` -Formatting is used when you need to include a variable value in -the string. +يتم استخدام التنسيق عندما تحتاج إلى تضمين قيمة متغير (variable) في النص ```python number = 3 + 4 @@ -290,14 +299,14 @@ write = 'Hi {}! The result is {}.'.format(name, number) print(write) ``` -### F-strings +### نصوص f (F-strings) -There is another (newer and shorter) way how to use formatting in Python and it involves `f-strings`. -Instead of using .format() method, you prepend letter `f` before -the string or the multiline string and Python will replace the variable names present -in curly brackets inside the template with their values during script execution. +هناك طريقة أخرى (أحدث وأقصر) لاستخدام التنسيق في Python وهي تتضمن `f-strings`. +بدلاً من استخدام طريقة .format() ، يمكنك إضافة حرف `f` قبل +النص أو النص متعدد الأسطر وسوف يستبدل Python أسماء المتغيرات الموجودة +في الأقواس المجعدة داخل القالب بقيمها أثناء تنفيذ البرنامج النصي. -The syntax looks like this: +يبدو بناء الجملة كما يلي: ```python number = 3 + 4 @@ -306,60 +315,60 @@ write = f"Hi {name}! The result is {number}." print(write) ``` -## Substrings +## السلاسل الفرعية (Substrings) -Now we will go back to subscripting. -Try to find out what the following program does: +الآن سنعود إلى لتجزئة النصوص. +حاول معرفة ما يفعله البرنامج التالي: ```python -string = 'PyLadies' +string = 'Khartoum' substring = string[5:] print(substring) ``` > [warning] -> Keep in mind that we are still counting from 0! +> تذكر أننا لا نزال نعد من 0! {% filter solution %} -`string[5:]` will print the *substring* from the fifth character to the end. +`string[5:]` سيطبع *النص الفرعي* من الحرف الخامس إلى النهاية. {% endfilter %} -We can also use `string[:5]`, which will select all characters -up to the fifth character, which is not included. -So `string[:5] + string[5:] == string`. +يمكننا أيضًا استخدام `string[:5]` ، والذي سيحدد جميع الأحرف +حتى الحرف الخامس ، والذي لا يتم تضمينه. +لذلك `string[:5] + string[5:] == string`. -What does `string[2:5]` do? +ماذا يفعل `string[2:5]`؟ -And what about `string[-4:]`? +ماذا عن `string[-4:]`؟ ```python -string = 'PyLadies' +string = 'Khartoum' print(string[:4]) print(string[2:5]) print(string[-4:]) ``` -You have to think about which number, which *index*, you want to use. +يجب أن تفكر في الرقم ، أي *الرقم التتابعي (index)* ، الذي تريد استخدامه. -It is better to think of these numbers as being on the borderlines -between characters, it makes it easier to understand: +من الأفضل التفكير في هذه الأرقام على أنها على حدود +بين الأحرف ، مما يسهل فهمها: {{ anchor('slicing-diagram') }} ```plain ╭───┬───┬───┬───┬───┬───┬───┬───╮ - │ P │ y │ L │ a │ d │ i │ e │ s │ + │ K │ h │ a │ r │ t │ o │ u │ m │ ├───┼───┼───┼───┼───┼───┼───┼───┤ │ │ │ │ │ │ │ │ │ 0 1 2 3 4 5 6 7 8 -8 -7 -6 -5 -4 -3 -2 -1 ╰───────────────╯ - 'PyLadies'[:4] == 'PyLa' + 'Khartoum'[:4] == 'Khar' ╰───────────────╯ - 'PyLadies'[2:6] == 'Ladi' + 'Khartoum'[2:6] == 'arto' ╰───────────╯ - 'PyLadies'[-3:] == 'ies' + 'Khartoum'[-3:] == 'oum' ``` diff --git a/lessons/beginners-en/str/info.yml b/lessons/beginners-en/str/info.yml index 41e797e6b9..4a8d16bbc7 100644 --- a/lessons/beginners-en/str/info.yml +++ b/lessons/beginners-en/str/info.yml @@ -1,6 +1,7 @@ -title: Strings +title: النصوص (Strings) style: md attribution: +- Translated by Mussab Abdalla - Translated by Marketa Muzikova - | Originally written by Petr Viktorin, 2014-2017 for [PyLadies CZ]. diff --git a/runs/2024/pyladies-en-vienna-2024-spring/info.yml b/runs/2024/pyladies-en-vienna-2024-spring/info.yml index bd4407a415..500b9a7d98 100644 --- a/runs/2024/pyladies-en-vienna-2024-spring/info.yml +++ b/runs/2024/pyladies-en-vienna-2024-spring/info.yml @@ -115,24 +115,24 @@ plan: url: https://docs.google.com/document/d/1lsA28-Np3AyLuR6U-prwGijm5b3vwGWczjCuD0EVDQM/edit type: homework -# - title: Strings, Custom functions -# date: 2024-04-16 -# slug: custom_func -# materials: -# - lesson: beginners-en/str -# - lesson: beginners-en/def -# - title: Functions cheatsheet -# url: https://github.com/ehmatthes/pcc/releases/download/v1.0.0/beginners_python_cheat_sheet_pcc_functions.pdf -# type: cheatsheet -# - title: Homework -# url: https://docs.google.com/document/d/1SPL6yMD3XOm9ZFDMDX_U4zSP-ltWd7QKvie5iCS8mno/edit -# type: homework -# - type: cheatsheet -# title: Depth exploration colored example for function recursion -# url: https://docs.google.com/document/d/1QWr8yzBfYtNaKLgRKoSL5tV5MGp46BOcD9Q3Q9ouOV8/edit -# - title: Practice exercises (offline session 17.04.2024) -# url: https://docs.google.com/document/d/17kX5w9eEn-Ed1gcONAaU5kSKANBI24bjL_7-3XZi0Dg/edit -# type: cheatsheet + - title: النصوص (Strings) , و الدوال المخصصة (Custom functions) + date: 2024-04-16 + slug: custom_func + materials: + - lesson: beginners-en/str + - lesson: beginners-en/def + - title: Functions cheatsheet + url: https://github.com/ehmatthes/pcc/releases/download/v1.0.0/beginners_python_cheat_sheet_pcc_functions.pdf + type: cheatsheet + - title: Homework + url: https://docs.google.com/document/d/1SPL6yMD3XOm9ZFDMDX_U4zSP-ltWd7QKvie5iCS8mno/edit + type: homework + - type: cheatsheet + title: شرح مفصل لمثالة دالة (explore) (English) + url: https://docs.google.com/document/d/1Fay2O_I4Sh4uUXhg7UbDIzsYGAZwyO8WMqFqUUY8kMI/edit + - title: تمارين(Practice exercises) + url: https://docs.google.com/document/d/1uQt886tRZh2KtMSvtsLIlqEORasykap65d_8LxrBTBU/edit?usp=sharing + type: cheatsheet # - title: GIT # slug: git # date: 2024-04-23