Programming Flashcards
Programming
Languages:
1- Low-level:
✓ They are harder to read.
✓ They are closer to machine
code
✓ Difficult for beginners to
learn
2- High-level:
✓ use natural language in their
syntax
✓ making them easier to read
and write.
عندي نوعين من لغات البرمجة:
اول نوع هو الlow level
بيبقى صعب اننا نقراه لأنه اقرب ف كتابته ل لغة الكمبيوتر اللي هي لغة الصفر والواحد وبالتالي بيبقى صعب على المبتدئين انهم يتعلموه
تاني نوع هو الhigh level
دا اقرب ل لغة الانسان وسهل للمبتدئين وسهل في القراية، زي بايثون كدا
Python Variables:
✓ Variables are used to store information to be referenced and manipulated in a computer program.
✓ Their sole purpose is to label and store data in memory.
يعني ايه فاريابل؟ قاللي ببساطة دي حاجة بخزن جواها معلومات اقدر استخدمها بعد كدا او أعدل عليها
Naming Convention:
✓ Choose a name that represents what it holds
✓ A variable name must start with a letter or the underscore character
✓ A variable name cannot start with a number
✓ A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
✓ Variable names are case-sensitive (age, Age and AGE are three different variables)
وانا بسمي الفاريابل بتاعي لازم اخد بالي من كام نقطة
✓ لازم اسمي الفاريابل ب اسم يدل على الداتا اللي جواه يعني مثلا مسميهوش X لأني بعدين هنسى X دي معناها ايه
✓ بداية اسم الفاريابل لازم يبقى حرف او اندرسكور (_)
✓ اسم الفاريابل مينفعش يبدأ برقم
✓ الاسم مينفعش يبقى فيه حاجة غير الحروف والارقام والاندرسكور (اي حاجة تانية هتضرب ايرور)
✓ اسامي الفاريابلز بتختلف ب اختلاف الحرف كابيتال ولا سمول، يعني age و Age و AGE دول تلاتة فاريابلز مختلفين
Python data types:
✓ Numerals:
1-integer
2- float
3- complex numbers
✓ Dictionary
✓ Boolean
✓ Set
✓ Sequence
1- string
2- list
3- tuple
Numeric:
1- integers
2- complex numbers
3- float
عندي نوع من الداتا بيبقى ارقام، له 3 اشكال
اول شكل انه يبقى int يعني رقم صحيح عادي اذا كان نيجاتيف او بوستيف
تاني شكل انه رقم مركب يعني فيه حاجة مش مُعرفة
تالت شكل ال float هو اي رقم فيه علامة عشرية (ديسيمال) حتى لو بعد العلامة العشرية اصفار زي 3.0
Sequence:
1- string
2- list
3- tuple
تاني نوع من الداتا انه يكون داتا ليها تسلسل
اول شكل ليها انها تبقى str يعني تسلسل من الحروف ورا بعض زي حرف، كلمة، جملة او حتى مقال، بنكتبها بين علامة تنصيص مفردة او مزدوجة “ or ‘
تاني شكل هو ال list دا بيتكتب بين اقواس مربعة [] وبفصل بين كل عنصر والتاني ب فصلة ,
فايدة الليست اني اقدر اعدل عليها والترتيب فيها مهم
تالت شكل هو ال tuple نفس استخدام ال list تقريبا ولكن الفرق انه بيتكتب بين اقواس مدورة () و مقدرش اعدل فيه
Dictionary:
✓ Dictionaries are used to store data values in key:value pairs.
✓ A dictionary is a collection which is ordered*, changeable and do not allow
duplicates
Boolean:
نوع كمان من الداتا هو ال dict ودا بيبقى في curled brackets {}
جواه حاجتين وهما ال key (اسم الكاتيجوري) بعدها بنحط نقطتين فوق بعض ونكتب ال value اللي هو المعلومة اللي شايلها الkey
شبه
{‘name’:’ahmed’}
مزايا الdict انه مُرَتَّب وقابل للتغير (بقدر ازود keys او اعمل ابديت للفاليو اللي جوا key كان موجود اوريدي عندي) واهم نقطة انه مفهوش تكرار للkeys يعني مينفعش اكتب نفس ال key مرتين
بعدها نوع اسمه boolean دا نوع من البيانات عبارة عن true or false بس بنستخدمه ف الشروط عشان نشوف لو الشرط متحقق (true) او مش متحقق (false)
Operators:
✓ Arithmetic operator
✓ Relational operators
✓ Assignment operators
✓ Logical operators
✓ Membership operators
✓ Identity operators
✓ Bitwise operators
دي العمليات كلها اللي عندي بقدر فيها استخدم 2 variable واعمل مابينهم عملية معينة
Arithmetic Operator: operators that perform addition,
subtraction, multiplication, and division.
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
// Floor division)
℅ (Modulus)
** (Exponentiation)
دي اول نوع من العمليات وهي العمليات الحسابية
+ الجمع
- الطرح
* الضرب
/ القسمة
// دي عملية بتطلعلي الرقم الصحيح من القسمة (يعني بتشيل الديسيمال وكل اللي بعده)
℅ دي عكس اللي فوقها، هي بتطلعلي الديسيمال بس للرقم بعد القسمة (بتشوف الكسور بس وبتفكس للرقم الصحيح، بنستخدمها كتير ف الفرق بين الارقام الفردية والزوجية عشان لو رقم زوجي اتقسم على اتنين المفروض ميكونش له ديسيمال والعكس صحيح)
** الباور
Weight of each operator:
Highest
()
**
/, //, %, *
+, -
Lowest
نخلي بالنا او لو في عملية حسابية هنعاملها معاملة الماث بتاعنا عادي بنحسب اللي بين الاقواس الاول وبعدين الباور وبعدين الضرب والقسمة وف الاخر خالص الطرح والجمع
Relational operators:
✓ Relational operators are also called comparison operators. It performs a comparison between two values.
✓ It returns a Boolean True or False depending upon the result of the comparison
Equal to (==)
Not equal to (!=)
Greater Than (>)
Less Than (<)
Greater Than or equal to (>=)
Less Than or equal to (<=)
تاني نوع من العمليات هي عمليات المقارنة او العلاقات، وهي وظيفتها تقارن بين حاجتين من نفس النوع (مينفعش اقارن بين str و int مثلا لازم احول الint ل str الاول)
دايما بترجع داتا نوعها boolean يعني ي اما true ي اما false حسب المقارنة بقى صحيحة ولا لا
Concatenating Strings
You can concatenate or combine strings using the “+” operator observing the spaces
لو عايزة اجمع بين كذا str بعمل كدا عن طريق اني احط بينهم + بس لازم اراعي ان لو حطيتهم زي ما هما مش هيبقى في مابينهم مسافة ف لو عايز مسافة ممكن ازودها ب ٣ طرق
✓ “hello” + “ “ + “world”
✓ “hello “ + “world”
✓ “hello” + “ world”
String length:
✓ To determine the length of a string, you can use the len()
function.
✓ It returns the number of characters in the string
اول فانكشن هناخدها ف بايثون هي string function بتساعدني اعرف طول الstr بتاعي
✓ بتعد من واحد عادي
✓ بتعد اي حاجة حرفيا على انها كاركتر يعني المسافة او رمز او اي حاجة بتتحسب
✓ بترجع داتا نوعها int لاني بسال على عدد الكاركترز ف هي بترجعلي عددهم
Print(Len(“hello world”))
11
String indexing:
✓ Indexing allows you to
access individual characters
in a string directly by using a
numeric value.
✓ String indexing is zerobased: the first character in
the string has index 0, the
next is 1, and so on
الاندكسنج مش هو الlen، عشان منتلغبطش الlen بتعد الطول كله، لكن الاندكس بتدور على حتة (الحتة دي ممكن تبقى كاركتر واحد او slice يعني اكتر من كاركتر)
✓ بدور على الكاركتر او الslice ب اني استخدم الاندكس بتاعهم (مكانهم ف السترينج متعلم ب رقم)
✓ بنبدأ نعد الأماكن من أول الزيرو، يعني اول كاركتر عندي دايما بيبقى الاندكس بتاعه زيرو
✓ بكتب الاندكس بين اقواس مربعة[] لازقة ف السترينج بتاعي
My_string=”hello”
Print(My_string[2])
l
(مش e عشان احنا بنعد من الزيرو)
String negative indexing:
✓ You can also use negative
indexing to access characters
from the end of the string.
✓ For example, -1
represents the last character, -2
represents the second
last character, and so on.
ببساطة زي ما بنعد من زيرو من بداية الstr، نقدر نعد من -1 من نهاية السترينج (مش هنقول من شمال ل يمين ولا يمين لشمال عشان بتفرق من لغة للتانية)
My_string=”hello”
Print(My_string[-1])
o
My_string=”أهلا”
Print(My_string[-2])
ل
String slicing:
✓ You can return a range of
characters by using the slice
syntax.
✓ It is done by specifying the
start and end indices of the
substring you want to extract.
✓ The start index is inclusive,
while the end index is exclusive.
من الاخر زي ما كنا بندور على كاركتر ف الاندكسنج، ممكن ندور على حتة كاملة اكتر من كاركتر (slice) برضو جوا نفس الاقواس المربعة [] بس بنحط نقطة بداية وبعدين نقطة نهاية
Mystring[start:stop]
✓ الاندكس اللي بيتكتب ف نقطة النهاية بيبقى exclusive يعني احنا بنقف اول ما نوصلله لكن مبناخدهوش معانا من ضمن ال slice
لو عايز ابدأ من الأول خالص بسيب الstart فاضي، ولو عايز اخد لحد النهاية (اخد اخر حرف معايا كمان) بسيب الstop فاضي
Slicing & Step:
✓ Start & end are index based on position in the sequence
✓ Step is optional, but it starts from 1 by default
✓ Step can be negative number
Sequence[start: end: step]
في parameter تالت بيتحط جوا البراكتس بتاعت الslice indexing وهو الstep، هو ف الطبيعي اصلا بيبقى موجود بس by default بيبقى 1
يعني
Sequence[start: end: 1] = Sequence[start: end]
يبقى الdefault للstep انه يبقى 1 (مش بنضطر نكتبه) طب يعني ايه؟ يعني الslice اللي هاخدها هبص عليها من البداية للنهاية
طب لو عايز ابدأ من النهاية؟ بكتب ف الstep انه يبقى -1
ازاي اقدر اقراه صح؟
اول حاجة بنعلم على الstart وبعدين نعلم على ال stop وبعدييين ف الاخر نبص على الstep، لو لقينا ال step ماشية صح في اتجاه from start to stop بحسب الاندكس للفاريابل واكتبه
طب لو لقيت ال step عكس الاتجاه اللي مكتوب بيه ال start to stop؟ يبقى الاجابة no value
print(my_text[2:5:-1])
هنا هبص ع الstart الاقيه رقم بوستيف وبعدها ال stop رقم بوستيف برضو بس بعده، يعني كدا الطبيعي ابقى ماشي من البداية للنهاية، بس مكتوبلي امشي العكس! يبقى no value
Slicing & step:
✓ steps can skip points
كدا احنا عرفنا ان ال default بيبقى 1 يعني من البداية للنهاية
وممكن نكتب -1 يعني من النهاية للبداية
في حاجة كمان اني اقدر اقولله يكتب حاجة ويسيب حاجة، ازاي؟
لو كتبت مثلا في الstep انه يبقى -2، طلاما نيجاتيف يبقى من النهاية للبداية، وكمان بقولله يبدأ يعد خطوة اتنين، وميكتبش غير الخطوة التانية بس
لو 3 يبقى طلاما بوستيف هتبقى من البداية للنهاية ويعد خطوة اتنين تلاتة ويكتب التالتة بس وهكذا
Functions:
✓ are named blocks of code that are designed to do specific job.
✓ A function is something you can call
✓ It can take input parameters optionally (the things you put in the parentheses), process those inputs in its body.
✓ Then performs an action and optionally returns a value
Importance of functions:
1- Maximizing code reuse
2- Minimizing code redundancy
3- Functions provide a tool for splitting systems into pieces that have welldefined roles.
4- Healthy maintenance
✓ يعني ايه فانكشن؟ دا اسم بيدل على block if code بيعمل وظيفة معينة
✓ الفانكشن دي حاجة انا بستدعيها (ليها اسلوبين هنعرفهم)
✓ ممكن تاخد parameter (الداتا اللي بتبقى بين الاقواسب بتاعت الفانكشن)
✓ بتعمل اكشن معين وممكن ترجع value (مش شرط، في فانكشنز بترجع value وفانكشن لا) يعني ليها return او ملهاش
طب ايه اهمية الفانكشنز؟
١- بتخليني اقدر اعيد استخدام الكود اكتر من مرة
٢- بتوفر عليا اني اكتب نفس الكود بالظبط كذا مرة (بتقلل ال redundancy)
٣- الفانكشن عبارة عن اداة بتقسم السيستم بتاعنا لقطع اصغر ليها قوانين معينة ودا بيسهل علينا الوصول لاي حاجة اسرع
٤- بقدر اصلح اي مشكلة بسهولة
Types of functions:
1. Built-in Functions: Python has several built-in functions like print(), len(), and type()
2. User-defined Functions: These are the functions created by users using the def keyword. They contain custom logic.
3. Methods: is a function that is associated with an object or class in object-oriented programming (OOP). Methods operate on data contained within the object and are used to define the behavior of the object.
عندي 3 انواع من الفانكشن
اول نوع هو الفانكشن المعتمدة على نفسها زي
print()
len()
type()
تاني نوع هو فانكشن المستخدم هو اللي بيخترعها ب انه يستخدم كلمة اسمها def (define) بتبقى معمولة على مزاجه عشان يعمل بيها وظيفة معينة
تالت نوع هو الmethod ودي بتبقى مرتبطة بالفاريابل ونوعه وبتقوم بوظيفة معينة
Function vs Method:
Association:
✓ A function is an independent block of code. It is not bound to any object and can be called directly.
✓ A method is a dependent, associated with an object (or
class). It is a function that is par of a class definition and operat
Calling:
✓ You call a function using its name
directly and passing the required arguments.
✓ You call a method using the instance (or class) to which it belongs, followed by a dot
(.), and then the method name with arguments.
اول حاجة الارتباط:
✓ الفانكشن مش مرتبطة بنوع داتا معين ( str, int, ..etc) هي مستقلة عادي وشغالة مع اي نوع
✓ الmethot معتمدة على نوع الداتا (هناخد الmethods لكل نوع داتا)
ازاي بنكتبها:
✓ الفانكشن بتتكتب ب اسمها عادي وبعدين بحط الفاريابل جوا الاقواس بتاعتها
print(variable)
✓ الmethod بتتنده باسم الفاريابل يعني بكتب اسم الفاريابل الاول وبعدين نقطة وبعدين الmethod اللي هستخدمها (حسب نوع الداتا اللي شايلها الفاريابل بتاعي)
variable.capitalize()
Parameters of functions:
✓ A parameter is the variable listed inside the parentheses () in the function.
✓ It is also termed as an argument.
✓ They represent the data that the function expects when it executes.
Example: x=’good morning’
len (x)
x is the parameter
✓Any kind of a function can have one or more parameters or no parameters at all.
✓ This depends on the code behind the function & the way it executes.
يعني ايه parameter؟
✓ دي الحاجة اللي موجودة جوا الاقواس بتاعت الفانكشن او الميثود
✓ بنسميها argument برضو
argument = parameter
✓ بتعبر عن الداتا اللي هنشتغل عليها ف الفانكشن بتاعتنا
✓ اي نوع من الفانكشن ممكن يبقى عنده parameter واحد او اكتر او ممكن يبقى معندهوش اصلا
✓ دا بيعتمد على هنستخدم الفانكشن ازاي ونوع الفانكشن برضو
parameters/ arguments can be:
A. Obligatory/ non-default parameter
✓ These are parameters that must be passed to a function when it is called.
✓ Otherwise, if the caller doesn’t provide these parameters, Python will raise a “TypeError”.
B. No parameter (parameterless functions)
✓ Means that the function does not accept any parameters.
✓ In other words, when calling those functions, they execute their block of code without needing any additional input.
C. Default parameter
✓ It resembles the obligatory parameter, meaning that a function is waiting for a parameter to perform.
✓ It only differs that this parameter has a default values that is given to a function when designing it.
✓ Such parameters are optional during a function call. If no argument is provided, the default value is used, and if an argument is provided, it will overwrite the default value.
D. Keyword parameter
✓ These are parameters that are passed explicitly using the name of the parameter first then the value.
Function_name(parameter_name=value)
✓ This allows you to pass arguments out of order or skip some default parameters.
اول نوع هو الاجباري
✓ دا معناه ان الداتا دي لااازم تتكتب بين الاقواس
✓ لو الاقواس اتسابت من غيرها هتعمل ايرور
تاني نوع لو مفيش باراميتر
✓ دا معناه ان الفانكشن مش بتقبل اصلا نكتب حاجة بين الاقواس بتاعتها
✓ من الاخر بتبقى فانكشن بسيطة مش عايزة معلومات كتير عشان تقوم بوظيفتها
تالت نوع لما الباراميتر يبقى default يعني متسستم على داتا معينة حتى لو مش مكتوبة
✓ بيبقوللي هو شبه الاجباري ف ان الفانكشن مش هتشتغل من غير المعلومة اللي هتتحط بين الاقواس
✓ بس الفرق ان لو احنا مكتبناش حاجة بين الاقواس ف الفانكشن فيها معلومة تلقائية متسستم عليها الكود
✓ هنا اني اكتب حاجة جوا الاقواس دي حاجة اختيارية اقدر اكتب (اغير الشيء الdefault) او اقدر اسيبها فاضية عادي وهو هيتعامل على اساس الdefault parameter اللي عنده ومش هيطلعلي ايرور ولا حاجة
اخر نوع هو الkeyword
✓ دا بيتكتب اسم الباراميتر الاول وبعدين = وبعدين الvalue
✓ بتبقى حاجات بتساعدنا اننا نسكيب الباراميتر نوعه default او نرتب ال output باسلوب معين
A. Obligatory Parameter
Example:
✓ len is a function designed to expect a parameter to perform. Hence len expects an obligatory parameter to start executing its built-in code & gets the length
دا مثال على الارجيومنت الاجباري
الفانكشن len كل لازمتها اني اعد طول str
لو انا مكتبتش بين اقواسها انا عايزة اعد ايه، هتعد الهوا؟ مش هتعد حاجة ف لازم اكتب
Len()
TypeError: len() takes exactly one argument (0 given)
len(‘hello’(
5
B. No Parameter (Parameterless
Functions)
Example:
✓ String methods like capitalize, lower, upper don’t need a parameter to
perform. Hence, they are called parameter-less methods.
مثال على الفانكشنز اللي مبنكتبش فيها ارجيومنتس
الميثود بتاعت capitalize كل لازمتها اني اكبر بيها كل حروف السترينج، ودي ميثود يعني اوريدي بتتنده ب اسم الفاريابل وبعدين نقطة وبعدين الميثود
ف انا هكتب
‘play’.capitalize()
خلاص مش محتاجة معلومات تانية عشان يقوم بوظيفته ف انا مش هكتب حاجة بين الاقواس
لو كتبت حاجة بين القواس هيطلعلي ايرور
‘play’.capitalize(1)
TypeError: str.capitalize() takes no arguments (1 given)
C. Default Parameter
Example:
✓ replace is a string method that has default count parameter.
String.replace( newvalue , oldvalue , count )
مثال على ان يكون الارجيومنت له default
الميثود اللي اسمها replace جواها 3 ارجيومنتس
اول اتنين obligatory (هبدل ايه ب ايه) وتالت واحد dafault (هبدل كام مرة)
الdefault بتاعه all (هبدل كل المرات)
اقدر اغير الdefault واحط count من عندي زي اني اكتب 1 (هبدل مرة واحدة بس)
المهم اني لو مكتبتهوش هيعتمد على ال default بتاعه ويبدل كله عادي ومش هيطلع ايرور
D. Keyword Parameter
Example:
• print is a function designed to show a message to the user. It takes many
parameters. Some are default, some are called by keyword.
Return:
✓ A return is a value that a function returns when it completes its task.
✓ Return statements can return data of any type, including integers, floats, strings, lists, dictionaries, and even other functions.
✓ Return is optional, again this depends on the code behind the function & the way it
executes.
✓ Accordingly, functions can have:
A. Return value
Example: len() → which returns an integer
B. No return value
Example: print() → has no return value.
يعني ايه return:
✓ دا ال value اللي بيرجعلي لما الفانكشن يخلص مهمته
✓ ممكن يرجعلي اي نوع داتا زي int او str او boolean .. الخ
✓ مش شرط الفانكشن يكون له return value عادي
✓ انواع الreturn
اول حاجة فانكشن له return value زي len بترجعلي داتا نوعها int (طول الstr)
تاني حاجة فانكشن مش بترجعلي حاجة زي print (يعني مقدرش اخزنه ف فاريابل هو بس بيطبعلي الحاجة في صفحة اليوزر لكن مبيرجعليش داتا ف السيستم بتاعي، ماليش دعوة بالصفحة بتاعت اليوزر)
String methods:
✓ lower()
✓ upper()
✓ capitalize()
✓ count()
✓ startswith()
✓ endswith()
✓ index()
✓ find()
✓ split()
✓ replace()
lower(): Converts all characters in a string to lowercase
text = “Hello World”
print(text.lower()) # Output: “hello world”
upper(): Converts all characters in a string to uppercase.
text = “Hello World”
print(text.upper()) # Output: “HELLO WORLD
capitalize(): Convert the first character of a string to uppercase
text = “hello world”
print(text.capitalize()) # Output: “Hello world”
lower()
دي ميثود بتخلي كل الحروف في الـ string تتحول لحروف سمول.
upper()
دي ميثود بتخلي كل الحروف في الـ string تتحول لحروف كابتال.
capitalize()
دي ميثود بتخلي أول حرف كابتال وباقي الحروف تتحول لسمول.
كلهم
✓No parameter
كل الميثودز دي ما بتحتاجش أي بارامتر.
مثال:
text = “hello”
print(text.lower()) # صح
print(text.lower(“”)) # غلط
# TypeError: lower() takes no arguments (1 given)
✓Return: str
كل ميثودز دي بترجع نسخة جديدة من الـ string بتعديل الحروف عليها.
count(): Returns the number of occurrences of a substring in the string.
text = “Hello World”
print(text.count(“o”)) # Output: 2
دي ميثود بتحسبلي عدد مرات ظهور حرف أو كلمة معينة (بكتبها انا ف ال parameter) جوا الـ string.
✓ Obligatory parameter
لازم أحدد إيه اللي عايز أعده (Substring).
لو ماحددتش حاجة، هيطلع Error.
مثال:
print(text.count())
TypeError: count() takes at least 1 argument (0 given)
✓ return : int
بترجع رقم يمثل عدد مرات ظهور الحاجة اللي بدور عليها.
✗ Case-Sensitive
بتحسب الحروف زي ما هي، يعني لو الحرف كابتال أو سمول، هيبقى مختلف.
مثال:
text = “Hello World”
print(text.count(“h”))
# Output: 0
print(text.count(“H”))
# Output: 1
startswith(): Returns “True” if a string starts with the given prefix
text = “Hello World”
print(text.startswith(“H”)) # Output: “True”
endswith(): Returns “True” if a string ends with the given suffix
text = “Hello World”
print(endswith(“o”)) # Output: “False”
Startswith
دي ميثود بتتأكدلي من لو الstr بيبتدي بحرف معين (او كذا حرف)
Endswith
زيها بتتأكدلي لو الstr بينتهي بحاجة معينة
✓ obligatory parameter
الميثودز دي لازم تاخد على الأقل بارامتر واحد.
لو ماكتبتيش حاجة هتطلعلك خطأ.
مثال:
print(‘Hello’.startswith())
TypeError: startswith() takes at least 1 argument (0 given)
✓ return: True أو False (Boolean).
index(): Finds the index of the first occurrence of a specified substring in a string.
text = “Hello World”
print(text.index(“o”))
# Output: 4
print(text.index(“W”))
# Output: 6
دي ميثود بتدورلي على أول ظهور لحرف أو كلمة معينة جوا الـ string وترجعلي مكانها (الـ Index)
✓ Obligatory Parameter
لازم أقولها أنا بدور على إيه (Substring).
لو نسيت أديها حاجة، هيطلع خطأ.
مثال:
print(“Hello”.index())
# TypeError: index() takes at least 1 argument (0 given
✓ Return: Integer
بترجع رقم مكان أول ظهور للحرف أو الكلمة اللي بتدور عليها.
✗ Error لو الحاجة مش موجودة
لو الحاجة مش موجودة جوا الـ string، هيطلع Error.
find(): Finds the index of the first occurrence of a specified substring in a string.
If the substring is not found, it returns -1 (بدل ما يطلع Error زي index()).
text = “Hello World”
print(text.find(“o”)) # Output: 4
print(text.find(“W”)) # Output: 6
print(text.find(“z”)) # Output: -1
دي ميثود بتدورلي على أول ظهور لحرف أو كلمة معينة جوا الـ string وترجعلي مكانها (الـ Index). زيها زي index بس الفرق بينها وبين index() إنها ما بتطلعش Error لو الحاجة مش موجودة، وترجع -1 بدل كده.
✓ Obligatory Parameter
لازم تحدد إيه اللي بتدور عليه (Substring).
لو ما حددتش، هيطلع Error.
مثال:
text = “Hello”
print(text.find())
# TypeError: find() takes at least 1 argument (0 given)
✓ Return: Integer
بترجع مكان العنصر أو -1 لو العنصر مش موجود.
✗ Case-Sensitive
زيها زي index()، بتهتم بالكابتال والسمول.
مثال:
text = “Hello World”
print(text.find(“h”)) # Output: -1
print(text.find(“H”)) # Output: 0
replace() Method
Replaces all occurrences of a substring with another substring in a string.
Does not modify the original string, it returns a new one.
text = “Hello World”
print(text.replace(“World”, “Python”)) # Output: “Hello Python”
print(text.replace(“l”, “z”))
# Output: “Hezzo Worzd”
دي ميثود بتعمل استبدال لكل ظهور لكلمة أو حرف معين جوا الـ string بحاجة تانية (بكتب ف ال parameters اولا عايز ابدل ايه، ثانيا عايز ابدله ب ايه، ثالثا(اختياري) عايز ابدا كام مرة)
دا معناه انه له 3 parameters
✓ 1st & 2nd Obligatory Parameters
لازم تحدد على الأقل حاجتين:
1. الـsubstring اللي عايز تستبدله.
2. الحاجة الجديدة اللي هتحطها مكانه.
مثال:
text = “Hello”
print(text.replace())
# TypeError: replace() takes at least 2 arguments (0 given)
✓ 3rd Default Parameter: Count
ممكن تضيف بارامتر ثالث (عدد مرات التبديل).
لو مضيفتهوش هيشتغل على الdefault وهيبدل كل مرة يلاقي الحاجة اللي عايز تبدلها
مثال:
text = “Hello World World”
print(text.replace(“World”, “Python”, 1)) # Output: “Hello Python World”
✓ Case-Sensitive:
زي باقي الميثودز، بتتعامل مع الحروف الكابتال والسمول كأنهم مختلفين.
مثال:
text = “Hello World”
print(text.replace(“h”, “z”)) # Output: “Hello World”
print(text.replace(“H”, “z”)) # Output: “zello World”
Example with a custom argument
split(): Splits a string into a list using a specified delimiter (separator).
By default, it splits the string by spaces.
text = “Hello World Python”
print(text.split()) # Output: [‘Hello’, ‘World’, ‘Python’]
text = “apple,banana,orange”
print(text.split(‘,’)) # Output: [‘apple’, ‘banana’, ‘orange’]
دي ميثود بتقسم الـ str ل List من الداتا باستخدام الفاصل اللي بتحدده.
لو مفيش فاصل مكتوب (في الباراميتر)، هيفصل بالمسافة (دا ال default).
ليها 2 parameters
✓ 2 default parameters
اول واحد اللي هو ال seperator
لو مكتبتش فاصل معين الديفولت مسافة
تاني واحد ال count، لو مكتبتش عايز افضل كام مرة هيفصل كل المرات
✓ Return: List
بيفصل الstr لعناصر بيجمعهم ف list
لو كتبنا سترينج مفهوش فاصل هيرجع عنصر واحد بس
text = “Hello”
print(text.split()) # Output: [‘Hello’]