إنشاء Tuple
يتم إنشاء tuple in python بين الأقواس الدائرية أو من خلال دالة tuple, و tuple غير قابلة للتغيير و لكن يمكن الوصول لعناصرها من خلال أرقام index
friends = ("ahmed","amr","mohamed","ramy","hazem") print(friends) print(type(friends))
في حالة إنشاء tuple يحتوى على عنصر واحد لابد من وضع فاصلة (,) بعد هذا العنصر وإلا ستحسب tuple أنها نص
friends = ("ahmed",) print(friends)
إنشاء tuple من خلال دالة tuple
friends = tuple(("ahmed","amr","mohamed","ramy","hazem")) print(friends)
إستخدام tuple
عرض عنصر من عناصر tuple, نتيجة المثال التالي يتم عرض amr
friends =("ahmed","amr","mohamed","ramy","hazem") print(friends[1])
عرض عدد عناصر tuple
friends =("ahmed","amr","mohamed","ramy","hazem") print(len(friends))
يمكن إستخدام أسلوب التحديد لعرض عناصر محددة من tuple كما تم الشرح في درس list
كما في المثال التالي:
friends =("ahmed","amr","mohamed","ramy","hazem") print(friends[1:3])
فحص إذا كان العنصر من ضمن tuple
friends =("ahmed","amr","mohamed","ramy","hazem") if "mohamed" in friends: print("Yes, 'mohamed' is in the friends tuple")
جلب عدد قيمة معينة في tuple, المثال التالي سيطبع 2 لأن قيمة amr تكررت مرتين
friends =("ahmed","amr","mohamed","ramy","hazem","amr") print(friends.count("amr"))
العودة برقم العنصر إعتماداً على القيمة, في المثال التالي سيطبع رقم 1 لأن قيمة amr في التريب 2 أي رقم 1 في المصفوفة , نلاحظ أنة عاد بناء على الفيمة الأولى و تجاهل amr الأخير
friends =("ahmed","amr","mohamed","ramy","hazem","amr") print(friends.index("amr"))
التغيير في tuple
كما ذكر مسبقاً في الدرس الخاص بـ array collection أن tuple لا يمكن تحديثة أو التغيير فية حيث يصبح مصفوفة ثابتة, إلا أن هناك طريقة لإجراء هذة العملية تتلخص في تحويل tuple الى list حيث يمكن التعديل فيها أو إضافة عناصر أو أي إجراء كما تعلمت في درس List , ثم بعد الإنتهاء من التعديل تقوم بتحويل list الى tuple مرة أخرى في نفس المتغير كما في المثال التالي
friends =("ahmed","amr","mohamed","ramy","hazem","amr") print("friends now:") print(friends) newList = list(friends) newlist[5] = "khaled" friends = tuple(newList) print("friends after update:") print(friends)
تحزيم و فك تحزيم packing and unpacking in tuple
عندما تقوم بإنشاء tuple هذا أشبة بتحزيم عناصر tuple بما تحمل من قيم , يمكن عمل فك تحزيم لتلك العناصر و تخزينها في متغيرات خارج tuple, في المثال التالي سيتم توزيع الأرقام على المتغيرات التي هى بأسماء الأشخاص
#packing a tuple Ages = (30, 25, 33) #unpack (hazem,mohamed,ramy) = Ages print(hazem) print(mohamed) print(ramy)
يمكن التحكم في توزيع القيم حيث يمكن تخزين كل قيمة في متغير واحد و مجموعة من القيم في متغير واحد على هيئة List
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry") (green, yellow, *red) = fruits print(green) print(yellow) print(red)
fruits = ("apple", "mango", "papaya", "pineapple", "cherry") (green, *tropic, red) = fruits print(green) print(tropic) print(red)
- روابط هامة لكورس لغة بايثون
- المتغير variable python
- انواع البيانات data type python
- if condition in python
- for loop in python
- while loop in python
- arithmetic operators in python
- assignment operators in python
- comparison operators in python
- logical operators in python
- identity operators in python
- input command in python
- array collection in python
- list in python
- tuple in python
- set in python
- dictionary in python
- iterators in python
- function in python