Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

CHAPTER 4: LISTS

List Structures

 Variable used to store one element( one value) .‫استخدمنا المتغير لتخزين قيمة واحدة‬
 If we want to store set of elements of the same type or different types we can use
Sequences data structure.
Sequence Data Structure ‫ فيمكننا استخدام‬، ‫إذا أردنا تخزين مجموعة من العناصر من نفس النوع أو أنواع مختلفة‬

Sequences data
structure

Lists Tuples Strings


‫القوائم‬ ‫المجموعات‬ ‫النصوص‬

What is Sequence?
Sequence: is a linearly-ordered set of elements accessed by index number.

)index( ‫ هي مجموعة عناصر مرتبة خطيًا (متتالية) يتم الوصول إليها بواسطة رقم الفهرس‬:‫التسلسل‬

What is index number?


‫ما هو رقم الفهرس؟‬

 Each element in the sequence have an order called index


‫كل عنرص يف التسلسل له ترتيب يسىم الفهرس‬
 zero-based indexing : begin numbering sequences of items with an index value of 0 rather
than 1
ً
1 ‫ بدال من‬0 ‫ البدء يف ترقيم تسلسل العنارص بقيمة فهرس تبلغ‬:‫الفهرسة الصفرية‬
 The index numbers in the sequence start from 0 to n-1 , n is the number of elements in
the sequence
‫ هو عدد العنارص يف التسلسل‬n ، n-1‫ إىل‬0 ‫تبدأ أرقام الفهرس يف التسلسل من‬

‫ال احلل النشر والتصوير‬ ‫المهندسة ديانا‬


CHAPTER 4: LISTS

Sequence Definition syntax Example


type ‫التعريف‬ ‫مثال‬
‫نوع‬
‫التسلسل‬
List is a mutable, linear data Lists are denoted by a  list contains the temperatures of the
‫القائمة‬ structure of variable length, comma-separated list of days of the week:
allowing mixed-type elements within square :‫تحتوي القائمة على درجات حرارة أيام األسبوع‬
elements. brackets []. temps=[68.8, 70.2, 67.2 , 71.8 , 73.2 ,
‫هي بنية بيانات خطية قابلة للتغيير‬ ‫تتم اإلشارة إلى القوائم بقائمة عناصر‬ 75 , 70]
‫ وتسمح بتخزين‬، ‫ومتغيرة الطول‬ .‫مفصولة بفواصل داخل أقواس مربعة‬  empty list  L=[]
‫عناصر بأنواع مختلفة‬ ‫قائمة فارغة‬

Tuple is an immutable linear data tuples are denoted by  nums = (10, 20, 30)
‫المجموعة‬ structure parentheses() instead of  student= ('John Smith',48,'Computer
‫ هي بنية بيانات خطية غير قابلة للتغيير‬square brackets Science')
 empty tuple  tup=()
‫يتم اإلشارة إلى المجموعات بأقواس‬
‫بدالً من األقواس المربعة‬
Tuple that consists of one t=(1,) 
element must include a ‫ مكونة من عنصر واحد فقط‬tuple
comma following the first
element.
‫ الذي يتكون‬Tuple ‫يجب أن يتضمن‬
‫من عنصر واحد فاصلة بعد العنصر‬
.‫األول‬
String is an immutable linear data String surrounded by single ‘’ name= 'John Smith'
‫النصوص‬ structure used to store a or double quotes”” message=''hello''
sequence of characters
‫هي بنية بيانات خطية غير قابلة‬ ‫يكون النص محاط بعالمات اقتباس‬
‫للتغييرتستخدم لتخزين سلسلة من‬ ‫مفردة أو مزدوجة‬
‫األحرف‬

- Linear data structure: linear ordering for the elements


)‫ الترتيب الخطي للعناصر(ان تكون العناصر متتالية‬:‫بنية البيانات الخطية‬
- Mutable: the contents of the sequence may be altered
‫ يمكن تغيير(تعديل) محتويات التسلسل‬:‫قابل للتغيير‬
- Immutable: the contents of the sequence can’t be altered

‫ ال يمكن تغيير محتويات التسلسل‬:‫غير قابل للتغيير‬

‫ال احلل النشر والتصوير‬ ‫المهندسة ديانا‬


CHAPTER 4: LISTS

Sequence operations
sequences ‫العمليات التي يمكن تنفيذها على جميع انواع ال‬

Operation definition example List Tuple String

s=[10,20,30,40] s=(10,20,30,40) s=”hello”


w=[60,50] w=(50,60) w=”!”

length len() function used to get len(s) 4 4 5


the number of elements in s ‫عدد عناصر‬
the sequence . len(w) 2 2 1
‫ يستخدم هذا الفنكشن لمعرفة‬len() w ‫عدد عناصر‬
‫عدد العناصر في التسلسل‬
Access Read element from the s[0] 10 10 h
sequence by using index ‫قراءة القيمة في‬
s ‫ في‬0 ‫ رقم‬index
index ‫ لقراءة عنصر باستخدام رقم‬w[1] 50 60 Error because
‫قراءة القيمة في‬ there is no
w ‫ في‬1 ‫ رقم‬index index 1 in
string w
‫خطأ ألنه ال يوجد‬
index
w ‫ في النص‬1
Update Update the value of the s[0] =1 [1,20,30,40] Not Allowed ‫غير مسموح‬
element at a specific index will update the
first element in s
‫ معين‬index ‫ تعديل قيمة في‬to 1 instead of 10 functions are allowed for list only
‫سيتم تحديث العنصر‬ since it is mutable, but the tuple
ً‫ بدال‬1 ‫ إلى‬s ‫األول في‬ and string are immutable(cannot be
10 ‫من‬ altered)
Delete del() used to Delete a value del(s[0]) [20,30,40]
from the list ‫ فقط النها قابلة‬list ‫هذه العمليات مسموحة في‬
will delete the ‫للتعديل‬
‫ حذف قيمة‬first element in ‫ غير قابل للتعديل‬string ‫ و‬tuple ‫لكن‬
the list s
‫سيحذف العنصر األول‬
s ‫في القائمة‬
Append append() used to append a s.append(100) [10,20,30,40,100]
new element to the end of
the list will add the value
list ‫ اضافة عنصر جديد الى نهاية‬100 to the end of
the list s

100 ‫سيضيف القيمة‬


s ‫إلى نهاية القائمة‬

‫ال احلل النشر والتصوير‬ ‫المهندسة ديانا‬


CHAPTER 4: LISTS

Insert  insert function s.insert(2,90) [10,20,90,30,40]


used to insert a new
element at a specific index will insert the
in the list value 90 at index
‫ معين‬index ‫إدراج عنصر جديد في‬ 2
 syntax ‫ في‬90 ‫إدراج القيمة‬
insert(index,value) 2 ‫ رقم‬index
sort sort() function used to sort w.sort() [50,60]
or order elements of the
list
‫ترتيب العناصر‬
reverse reverse() function used to s.reverse() [40,30,20,10]
reverse the order of the
elements in list
‫عكس ترتيب العناصر في القائمة‬
count count() used to count how s.count(10) 1 1 Error
many times a specific value ‫كم مرة تكررت القيمة‬ Not allowed to
repeated in the sequence S ‫ في‬10 count an
‫احسب عدد مرات تكرار قيمة معينة‬ integer value
in string
‫ال يمكن حساب كم‬
‫مرة تكرر رقم في‬
‫نص‬

: ‫التصحيح‬
s.count(‘10’)

s.count(‘l’) 0 0 2
‫كم مرة تكررت القيمة‬
S ‫‘ في‬l’
index index() return the index of s.index(30) 2 2 Error
a specific value in the ‫ال يمكن البحث عن‬
sequence ‫رقم في نص‬
‫ لقيمة محددة اذا‬index ‫إرجاع‬
‫كانت موجودة في التسلسل‬ :‫التصحيح‬
s.index(‘30’)

s.index(‘e’) Error: 'e' is not in Error: 'e' is not in 1


list list
membership We can use the ‘h’ in s False False True
membership operators (in, 50 not in s True True True
not in) to check if value is a
member in the sequence or
not

‫يمكننا استخدام اشارات العضوية‬


‫) للتحقق مما إذا كانت‬in , not in(
‫عضوا في التسلسل أم ال‬
ً ‫القيمة‬

‫ال احلل النشر والتصوير‬ ‫المهندسة ديانا‬


CHAPTER 4: LISTS

Minimum min() function used to find min(s) 10 10 ‘e’


value the minimum value in the s ‫ايجاد اصغر قيمة في‬
sequence
‫ايجاد اصغر قيمة‬
Maximum max() function used to get max(s) 40 40 ‘o’
value the maximum value in the s ‫ايجاد اكبر قيمة في‬
sequence
‫ايجاد اكبر قيمة‬
sum sum() function used to sum(s) 100 100 Error
calculate the sum of ‫ال يمكن استخدام‬
elements in lists and tuples ‫احسب مجموع االرقام‬ ‫ مع نص‬sum
only s ‫في‬

‫ او‬list ‫لحساب مجموع العناصر في‬


‫ فقط‬tuple
concatenation We can concatenate the s+w [10,20,30,40,60,5 (10,20,30,40,50,60) “hello!”
sequences of same type 0]
together by using +
operator
‫يمكننا ربط التسلسالت من نفس‬
+ ‫النوع معًا باستخدام‬
slice Slicing/ Extracting a portion s[1:4] [20,30,40] (20,30,40) “ell”
from the sequence
‫اقتطاع جزء من التسلسل‬ ‫اقتطاع السلسلة من‬
Syntax : 3 ‫ الى‬1 ‫ رقم‬index

S[m:n]
‫ الذي يبدا االقتطاع‬index ‫ رقم‬M
‫منه‬

‫ الذي يتوقف عنده‬index ‫ رقم‬N


‫االقتطاع لكن من دون تضمين‬
index ‫القيمة في هذا‬
‫ او كالهما‬n ‫ او‬m ‫ يمكن اهمل‬s[1:] [20,30,40] (20,30,40) “ello”
‫اقتطاع السلسلة من‬
‫ الى‬1 ‫ رقم‬index
‫النهاية‬
s[:2] [10,20] (10,20) “he”
1 ‫ الى‬0 ‫من‬
s[:] [10,20,30,40] (10,20,30,40) “hello”

‫ الى النهاية‬0 ‫من‬


‫(الحصول على‬
)‫التسلسل كامل‬

‫ال احلل النشر والتصوير‬ ‫المهندسة ديانا‬


CHAPTER 4: LISTS

Nested List

 lists and tuples can be nested


‫يمكن ان تكون القوائم والمجموعات متداخلة‬
 Example:

List Traversal

 List traversal: accessing, one-by-one, each element of a list.


‫ إلى كل عنصر من عناصر القائمة‬، ‫ واحدًا تلو اآلخر‬، ‫الوصول‬
 example

o search for a particular item in a list

‫البحث عن عنصر معين في القائمة‬


o Print all the elements of a list

‫اطبع كل عناصر القائمة‬


o add up all the elements of a list

‫اجمع كل عناصر القائمة‬

Iterating over Sequences in Python (‫ الدوران عبر تسلسل‬/‫)التكرار‬

To traverse a sequence, we should iterate over a sequence by using while statement or for
statement.

What is for statement?

 For statement is an iterative control statement that iterates once for each element in a
specified sequence of elements.

sequence ‫جملة تحكم تكرارية تستخدم للدوران على جميع عناصر ال‬

 For statement used in definite loops


)‫ التي نوعها محدد (عدد التكرارات محدد قبل تنفيذ اللوب‬loops ‫ تستخدم في ال‬for ‫جملة‬

‫ال احلل النشر والتصوير‬ ‫المهندسة ديانا‬


‫‪CHAPTER 4: LISTS‬‬

‫‪‬‬ ‫‪Syntax of for statement:‬‬

‫‪Loop variable‬‬

‫‪for k in sequence:‬‬ ‫‪header‬‬


‫‪clause‬‬
‫‪statements‬‬ ‫‪suite‬‬

‫‪indentation‬‬

‫مبدأ عمل ‪For statement‬‬ ‫‪‬‬


‫تدور ‪ for‬على جميع عناصر ‪ sequence‬وكل قيمة يتم تخزينها في ‪ loop variable‬وهو ‪ k‬حسب المثال‬
‫ثم تنفذ التعليمات التابعة ل ‪ for‬لكل قيمة من القيم‬

‫مثال على ‪ : for‬برنامج لطباعة جميع العناصر في ‪list‬‬ ‫‪‬‬


‫]‪nums=[10,20,30,40‬‬

‫‪for k in nums:‬‬
‫)‪print(k‬‬

‫‪ For‬تدور على القيم الموجودة في ‪ nums‬وتخزن كل قيمة في ‪k‬‬ ‫‪o‬‬


‫لكل قيمة يتم تنفيذ التعليمات التابعة ل ‪ for‬وهي طباعة قيمة ‪k‬‬ ‫‪o‬‬
‫المخرجات من البرنامج ‪:‬‬ ‫‪o‬‬
‫‪10‬‬
‫‪20‬‬
‫‪30‬‬
‫‪40‬‬

‫يمكن حل نفس المثال باستخدام ‪while loop‬‬ ‫‪‬‬

‫]‪nums=[10,20,30,40‬‬
‫‪k=0‬‬
‫‪while k<len(nums):‬‬
‫)]‪print(nums[k‬‬
‫‪k=k+1‬‬

‫‪‬‬ ‫‪for statement can be applied to all sequence types, including strings and tuples‬‬
‫جملة ‪ for‬يمكن استخدامها مع جميع انواع ال ‪sequence‬‬

‫ال احلل النشر والتصوير‬ ‫المهندسة ديانا‬


CHAPTER 4: LISTS

‫ لطباعة جميع الحروف في النص‬for ‫مثال على استخدام‬ 


for c in ‘hello’:
print(c)
c ‫ وتخزن كل حرف في‬hello ‫ تدور على القيم الموجودة في النص‬For o
c ‫ وهي طباعة قيمة‬for ‫لكل قيمة يتم تنفيذ التعليمات التابعة ل‬ o
: ‫المخرجات من البرنامج‬ o
h
e
l
l
o

Range function

 Range function: used for generating a sequence of integers that a for loop can iterate over.

‫ الدوران عليها‬for ‫ للحصول على سلسلة من االرقام الصحيحة التي يمكن ل‬range ‫يستخدم‬

 Syntax:

range (m,n,s)

o m: is the starting value in the sequence


‫القيمة التي يبدأ منها التسلسل‬
m can be omitted then the sequence will begin from 0 by default
‫ افتراضيًا‬0 ‫ بحيث يبدأ التسلسل من‬m ‫يمكن حذف‬
o n: is the ending value but it is not included in the sequence

.‫القيمة التي ينتهي عندها التسلسل لكن ال يتم تضمينها في التسلسل‬


o s: is the step between each number in the sequence ,
‫الخطوة بين كل رقم والرقم الذي يليه في التسلسل‬
 it is optional value ‫قيمة اختيارية‬
 by default is 1 )1 ‫(وتكون افتراضيًا‬
 It can be negative value ‫يمكن ان تكون قيمة سالبة‬

 Examples:
1. range(1, 11) generates the sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2. range(1, 11,1) generates the sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3. range(1, 11,2) generates the sequence [1, 3, 5, 7, 9]
4. range(5) generates the sequence [0, 1, 2, 3, 4]
5. range(11,1,-1) generates the sequence [11,10,9,8,7,6,5,4,3,2]

‫ال احلل النشر والتصوير‬ ‫المهندسة ديانا‬


CHAPTER 4: LISTS

list ‫ لطباعة عناصر‬range function ‫استخدام‬ 

nums=[10,20,30,40]

for k in range(len(nums)):
print(nums[k])

-----------
- Range(len(nums)) = range(0,len(nums)) = range(0,4) [0,1,2,3]
- In the above example K called index variable: is a variable whose changing value is used to access
elements of a sequence.
index variable ‫ تسمى‬k ‫في المثال السابق‬

.‫هو متغير تستخدم قيمته المتغيرة للوصول إلى عناصر التسلسل‬

k ‫ يتم تخزينه في‬index ‫ وبالتالي كل‬nums ‫ في‬index ‫ والتي تمثل قيم ال‬range ‫ على القيم الناتجة من‬for ‫تدور‬ -
nums[k] ‫ وطباعتها عن طريق‬nums ‫ثم استخدامه في الوصول الى القيمة في‬

List Comprehensions

List Comprehensions: used to generate a new list based on another list

.‫ موجودة مسبقا‬list ‫ جديدة باالعتماد على‬list ‫يستخدم النشاء‬

Syntax:

[expression for k in sequence if condition]

w=[10,20,30,40]

Generate a new list based


on w by adding 2 for each
element in w
[K+2 for k in l]
w ‫قم بإنشاء قائمة جديدة بنا ًء على‬
w ‫ لكل عنصر في‬2 ‫بإضافة‬

[12,22,32,42]

‫ال احلل النشر والتصوير‬ ‫المهندسة ديانا‬


CHAPTER 4: LISTS

Examples:

List assignment

 If we have a list:

list1=[10,20,30,40]

 list2 = list 1
 So list1 and list2 referred to same location (instance) of memory.
‫ يشيران الى نفس المواقع في الذاكرة‬list 2 ‫ و‬List 1

 Any change in list 1 will affect list 2


list2 ‫ سيؤثر على‬list1 ‫اي تعديل على‬

‫ال احلل النشر والتصوير‬ ‫المهندسة ديانا‬


CHAPTER 4: LISTS

Copying of lists (‫)االستنساخ‬

 We can use list() function to make a copy from the list


list ‫ لعمل نسخة من‬list () ‫يمكننا استخدام‬
 If we have this list:
list1= [10,20,30,40]
 list2 = list(list1)  this statement will make a copy from list1 and assign it to list 2 ,
list2 ‫ ويعطيها ل‬list 1 ‫ ينشئ نسخة من‬list ()
 so list 1 and list 2 referred to different instance of memory.

‫ يشيران الى موقعان مختلفان في الذاكرة‬list 2 ‫ و‬List 1

 change in list1 does NOT cause change in list2

list2 ‫ ال يؤثر على‬list1 ‫اي تعديل على‬

‫ال احلل النشر والتصوير‬ ‫المهندسة ديانا‬

You might also like