Pyhton Lists (Data types, variables, conditional and Iterative statements), Summaries of Programming Languages

في هذه الدورة التدريبية القائمة على المشروع والتي تستغرق ساعة واحدة، ستتعلم كيفية عمل لعبة تفاعلية بسيطة عن طريق لغة البرمجة python. هتتعلم و تطبق أسس البرمجة زي ال (Data types, variables, conditional and Iterative statements). دا هيساعدك انك تكتب كود لألعاب تانية و مهماتك الحياتية باستخدام python. لغة python هي واحدة من أهم و أكثر لغات البرمجة استخداما و دا يرجع لسهولة ال syntax او الكلمات المستخدمة فيها اللي قريبة جدا من لغتنا العادية. حاليا لغة python بتستخدم كتير في تطبيقات الذكاء الاصطنا

Typology: Summaries

2018/2019

Available from 10/29/2021

ayoub-kadiri
ayoub-kadiri 🇲🇦

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python Lists
List
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)
Access Items
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
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])
Range of Indexes
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
pf3
pf4
pf5

Partial preview of the text

Download Pyhton Lists (Data types, variables, conditional and Iterative statements) and more Summaries Programming Languages in PDF only on Docsity!

Python Lists

List

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)

Access Items

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

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 ])

Range of Indexes

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 :])

Range of Negative Indexes

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 ])

Change Item Value

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)

Remove Item

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)

Copy a List

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)

Join Two Lists

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)

List Methods