Professional Documents
Culture Documents
Chapter 4 ملخص
Chapter 4 ملخص
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
What is Sequence?
Sequence: is a linearly-ordered set of elements accessed by index number.
)index( هي مجموعة عناصر مرتبة خطيًا (متتالية) يتم الوصول إليها بواسطة رقم الفهرس:التسلسل
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
هي بنية بيانات خطية غير قابلة يكون النص محاط بعالمات اقتباس
للتغييرتستخدم لتخزين سلسلة من مفردة أو مزدوجة
األحرف
Sequence operations
sequences العمليات التي يمكن تنفيذها على جميع انواع ال
: التصحيح
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[m:n]
الذي يبدا االقتطاعindex رقمM
منه
Nested List
List Traversal
To traverse a sequence, we should iterate over a sequence by using while statement or for
statement.
For statement is an iterative control statement that iterates once for each element in a
specified sequence of elements.
sequence جملة تحكم تكرارية تستخدم للدوران على جميع عناصر ال
Loop variable
indentation
for k in nums:
)print(k
]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
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)
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]
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
Syntax:
w=[10,20,30,40]
[12,22,32,42]
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