2020-01-12
  • |
  • daafoor
  • |
  • مشاهدات: 18169

اولا وقبل اي شيء: المصفوفة هي مكان بالذاكرة لتخزين اكثر من قيمة, بعكس المتحول العادي الذي يستطيع تخزين قيمة واحدة فقط.

المصفوفات في لغة بايثون تسمى collections, ولها 4 انواع في python:

  1. List: لها نفس صفات المصفوفة بلغات البرمجة الاخرى مثل جافا وc++, لايمكن تعديل عدد عناصرها لكن يمكن تعديل قيمهم, العناصر مرتبة وكل عنصر له عنوانindex, يمكن ان تتكرر قيم العناصر فيها.
  2. Tuple:  لايمكن تعديل عدد عناصرها ولا حتى تعديل قيمهم, العناصر مرتبة وكل عنصر له عنوانindex, يمكن ان تتكرر قيم العناصر فيها.
  3. set:  يمكن تعديل عدد عناصرها(اضافة عناصر فقط ولايمكن حذف عنصر), و يمكن تعديل قيمهم, العناصرغير مرتبة وليس لها عناوين, يعني ممكن تطبع الset مرتين وتنطبع العناصر بترتيب مختلف كل مرة, لا يمكن ان تتكرر قيم العناصر فيها.
  4. dictionary :  لايمكن تعديل عدد عناصرها لكن يمكن تعديل قيمهم, العناصر غير مرتبة لكن كل عنصر له عنوان لكن يسمى key وليس Index, لا يمكن ان تتكرر قيم العناصر فيها.

هذه الانواع الاربعة بالنهاية كلها مصفوفات, ولكن مع وجود اختلافات مابينها وقد اختصرت الاختلافات بالجدول التالي:

List

Ordered

changeable

Allows duplicate members

indexed

Tuple

Ordered

unchangeable

Allows duplicate members

indexed

Set

Unordered

Changeable(can add only)

No duplicate members

unindexed

Dictionary

unordered

changeable

No duplicate members

indexed

 

 

تعريف مصفوفة list من الارقام  وكيفية استدعاء عناصر معينة منها:

numbers=[12,2,22,2,94,2]
print(numbers[0])
print(numbers[3])
print(numbers[-1]) #prints 2
print(numbers[2:5]) #prints: 22,2,94
print(numbers[:4]) #prints: 12,2,22,2
print(numbers[2:]) #prints: 22,2,94,2
print(numbers[-4:-1]) #prints: 22,2,94

 

 انشاء List ومن ثم طباعة قيم عناصرها وعدد عناصرها:

thislist = ["apple", "banana", "cherry"]
print(thislist)

print("list length is : "+len(thislist))



#Check if "apple" is present in the list:
if "apple" in thislist:
  print("Yes, 'apple' is in the fruits list")

 

 لديك خيارات كثيرة عند انشاء list, وانواع بيانات كثيرة, وايضا بامكانك وضع عناصر بانواع بيانات مختلفة عن بعضها:

#String, int and boolean data types:
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]


#A list with strings, integers and boolean values:
list1 = ["abc", 34, True, 40, "male"]

 

كيف تعديل قيمة عنصر في list:

#Change the second item:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)




#Change the values "banana" and "cherry" with the values "blackcurrant" and "watermelon":
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)



#Change the second value by replacing it with two new values:
thislist = ["apple", "banana", "cherry"]
thislist[1:2] = ["blackcurrant", "watermelon"]
print(thislist)




#Change the second and third value by replacing it with one value:
thislist = ["apple", "banana", "cherry"]
thislist[1:3] = ["watermelon"]
print(thislist)



#Insert "watermelon" as the third item:
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "watermelon")
print(thislist)

 

اضافة عنصر جديد الى list:

#Using the append() method to append an item:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)


#Insert an item as the second position:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)



#Add the elements of tropical to thislist:
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)




#Add elements of a tuple to a list:
thislist = ["apple", "banana", "cherry"]
thistuple = ("kiwi", "orange")
thislist.extend(thistuple)
print(thislist)

 

حذف عنصر من list:

#Remove "banana":
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)


#Remove the second item:
thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist)


#Remove the last item:
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)


#Remove the first item:
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)


#Delete the entire list:
thislist = ["apple", "banana", "cherry"]
del thislist


#Clear the list content:
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)

 

 طرق استعراض العناصر في List:

#Print all items in the list, one by one:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
  print(x)




#Print all items by referring to their index number:
thislist = ["apple", "banana", "cherry"]
for i in range(len(thislist)):
  print(thislist[i])





#Print all items, using a while loop to go through all the index numbers
thislist = ["apple", "banana", "cherry"]
i = 0
while i < len(thislist):
  print(thislist[i])
  i = i + 1




#A short hand for loop that will print all items in a list:
thislist = ["apple", "banana", "cherry"]
[print(x) for x in thislist]



 

 

 

 

ابحث عن مسائل برمجة بايثون | Python programming بالانجليزي

هل أعجبك المحتوى؟

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!

التعليقات
لا يوجد تعليقات
لاضافة سؤال او تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول