



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
في هذه الدورة التدريبية القائمة على المشروع والتي تستغرق ساعة واحدة، ستتعلم كيفية عمل لعبة تفاعلية بسيطة عن طريق لغة البرمجة python. هتتعلم و تطبق أسس البرمجة زي ال (Data types, variables, conditional and Iterative statements). دا هيساعدك انك تكتب كود لألعاب تانية و مهماتك الحياتية باستخدام python. لغة python هي واحدة من أهم و أكثر لغات البرمجة استخداما و دا يرجع لسهولة ال syntax او الكلمات المستخدمة فيها اللي قريبة جدا من لغتنا العادية. حاليا لغة python بتستخدم كتير في تطبيقات الذكاء الاصطنا
Typology: Summaries
1 / 6
This page cannot be seen from the preview
Don't miss anything!




A list is a collection which is ordered and changeable. In Python lists are written with square brackets. Example Create a List: mylist = ["football", "basketball", "tennis"] print(mylist)
You access the list items by referring to the index number: Example Print the second item of the list: mylist = ["football", "basketball", "tennis"] print(mylist[ 1 ])
Negative indexing means beginning from the end, -1 refers to the last item, - 2 refers to the second last item etc. Example Print the last item of the list: mylist = ["football", "basketball", "tennis"] print(mylist[- 1 ])
You can specify a range of indexes by specifying where to start and where to end the range. When specifying a range, the return value will be a new list with the specified items. Example
Return the third, fourth, and fifth item: mylist = ["hat", "shirt", "shoes", "tie", "watch", "bag", "belt"] print(mylist[ 2 : 5 ]) Note: The search will start at index 2 (included) and end at index 5 (not included). Remember that the first item has index 0. By leaving out the start value, the range will start at the first item: Example This example returns the items from the beginning to "tie": mylist = ["hat", "shirt", "shoes", "tie", "watch", "bag", "belt"] print(mylist[: 4 ]) By leaving out the end value, the range will go on to the end of the list: Example This example returns the items from "shoes" and to the end: mylist = ["hat", "shirt", "shoes", "tie", "watch", "bag", "belt"] print(mylist[ 2 :])
Specify negative indexes if you want to start the search from the end of the list: Example This example returns the items from index -4 (included) to index - (excluded) mylist = ["hat", "shirt", "shoes", "tie", "watch", "bag", "belt"] print(mylist[- 4 :- 1 ])
To change the value of a specific item, refer to the index number: Example Change the second item: mylist = ["hat", "shirt", "shoes"] mylist[ 1 ] = "bag" print(mylist)
There are several methods to remove items from a list: Example The remove() method removes the specified item: mylist = ["hat", "shirt", "shoes"] mylist.remove("shirt") print(mylist) Example The pop() method removes the specified index, (or the last item if index is not specified): mylist = ["hat", "shirt", "shoes"] mylist.pop() print(mylist) Example The del keyword removes the specified index: mylist = ["hat", "shirt", "shoes"] del mylist[ 0 ] print(mylist) Example The del keyword can also delete the list completely: mylist = ["hat", "shirt", "shoes"] del thislist Example The clear() method empties the list: mylist = ["hat", "shirt", "shoes"] mylist.clear() print(mylist)
You cannot copy a list simply by typing list2 = list1, because: list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2. There are ways to make a copy, one way is to use the built-in List method copy().
Example Make a copy of a list with the copy() method: mylist = ["hat", "shirt", "shoes"] list2 = mylist.copy() print(list2) Another way to make a copy is to use the built-in method list(). Example Make a copy of a list with the list() method: mylist = ["hat", "shirt", "shoes"] list2 = list(mylist) print(list2)
There are several ways to join, or concatenate, two or more lists in Python. One of the easiest ways are by using the + operator. Example Join two lists: list1 = ["a", "b" , "c"] list2 = [ 1 , 2 , 3 ] list3 = list1 + list print(list3) Or you can use the extend() method, which purpose is to add elements from one list to another list: Example Use the extend() method to add list2 at the end of list1: list1 = ["a", "b", "c"] list2 = [ 1 , 2 , 3 ] list1.extend(list2) print(list1)