Toqa Fayyad نشر 2 يونيو أرسل تقرير نشر 2 يونيو Python Assignment: Lists and Sets Part 1: Lists Creating Lists: Create a list named fruits that contains the following items: "Apple", "Banana", "Cherry", "Apple". Print the list and its length. Accessing List Items: Access and print the second item in the fruits list. Access and print the last item using negative indexing. Access and print the first two items using slicing. Modifying Lists: Change the second item in the fruits list to "Blueberry". Add "Orange" to the end of the list using the append() method. Insert "Mango" at the beginning of the list using the insert() method. Remove "Cherry" from the list using the remove() method. Remove the last item from the list using the pop() method. List Methods: Use the count() method to find how many times "Apple" appears in the fruits list. Sort the fruits list in alphabetical order using the sort() method. Reverse the order of the fruits list using the reverse() method. List Comprehension: Create a new list named long_fruits that contains only the fruits with more than 5 characters using list comprehension. Part 2: Sets Creating Sets: Create a set named colors that contains the following items: "Red", "Blue", "Green", "Red". Print the set and its length. Accessing Set Items: Check if "Blue" is in the colors set and print the result. Loop through the colors set and print each item. Modifying Sets: Add "Yellow" to the colors set using the add() method. Remove "Green" from the set using the remove() method. Try removing "Purple" using the discard() method and explain the difference between remove() and discard(). Set Methods: Create another set named new_colors with items: "Blue", "Purple", "Orange". Use the union() method to combine colors and new_colors and print the result. Use the difference() method to find items that are in colors but not in new_colors and print the result. Use the symmetric_difference() method to find items that are in either colors or new_colors but not in both and print the result. Use the intersection() method to find items that are common to both colors and new_colors and print the result. 3 اقتباس
0 Mustafa Suleiman نشر 2 يونيو أرسل تقرير نشر 2 يونيو الأسئلة الإختبارية لا يتم الإجابة عليها بشكل مباشر، لكن للتوضيح المطلوب منك هو أن تقومي بكتابة كود بايثون لتنفيذ مجموعة من العمليات على هياكل البيانات من نوع قائمة List و مجموعة Set. وبخصوص القوائم ستحتاجين إلى إنشاء قائمة باسم fruits تحتوي على العناصر "Apple", "Banana", "Cherry", "Apple"، ثم طباعة اطبع القائمة وطولها أيضًا. ثم كتابة كود للوصول إلى العنصر الثاني في قائمة fruits وطباعته، وإلى العنصر الأخير في القائمة باستخدام الفهرسة السالبة وطباعة العنصر، ثم الوصول إلى أول عنصرين في القائمة باستخدام مفهوم الـ slicing واطبعيهما. ثم تعديل القوائم بتغيير قيمة العنصر الثاني في قائمة fruits ليصبح "Blueberry"، وأضيفي العنصر "Orange" إلى نهاية القائمة باستخدام دالة append()، ثم أدرجي العنصر "Mango" في بداية القائمة عند الفهرس 0 باستخدام دالة insert(). ثم احذفي العنصر "Cherry" من القائمة باستخدام دالة remove()، ثم احذفي العنصر الأخير من القائمة باستخدام دالة pop(). بعد ذلك استخدمي دوال القوائم، أولاً دالة count() لمعرفة عدد مرات ظهور العنصر "Apple" في قائمة fruits، ثم رتبي عناصر قائمة fruits أبجديًا باستخدام دالة sort()، ثم اعكسي ترتيب عناصر قائمة fruits باستخدام دالة reverse(). ثم تطبيق مفهوم List Comprehension لإنشاء قائمة جديدة باسم long_fruits تحتوي فقط على الفواكه من قائمة fruits التي يزيد عدد أحرف اسمها عن 5 أحرف. والجزء الثاني في التمرين هو إنشاء المجموعات حيث عليكِ إنشاء مجموعة باسم colors تحتوي على العناصر "Red", "Blue", "Green", "Red"، واطبعي المجموعة وطولها. ثم تحققي هل العنصر "Blue" موجود ضمن مجموعة colors واطبعي نتيجة التحقق True أو False، ثم استخدمي حلقة تكرار للمرور على جميع عناصر مجموعة colors وطباعة كل عنصر. بعد ذلك أضيفي العنصر "Yellow" إلى مجموعة colors باستخدام دالة add()، واحذف العنصر "Green" من المجموعة باستخدام دالة remove(). وحاول حذف العنصر "Purple"(وهو غير موجود في المجموعة باستخدام دالة discard()، ثم اشرحي الفرق بين دالة remove() ودالة discard() عند محاولة حذف عنصر غير موجود. ثم التطبيق على دوال المجموعات، من خلال إنشاء مجموعة أخرى باسم new_colors تحتوي على العناصر "Blue", "Purple", "Orange". واستخدم دالة union() لدمج عناصر المجموعتين colors و new_colors واطبعي المجموعة الناتجة، ثم دالة difference() لإيجاد العناصر الموجودة في مجموعة colors وليست موجودة في new_colors، واطبعي المجموعة الناتجة. ثم دالة symmetric_difference() لإيجاد العناصر الموجودة في إحدى المجموعتين ولكن ليس في كلتيهما، واطبعي المجموعة الناتجة. ثم دالة intersection() لإيجاد العناصر المشتركة بين المجموعتين colors و new_colors، واطبعي المجموعة الناتجة. المجموعات set في بايثون اقتباس
0 محمد_عاطف نشر 2 يونيو أرسل تقرير نشر 2 يونيو نحن هنا في أكاديمية حسوب لا نقوم بالإجابة على أسئلة الإختبارات والتقيمات ولكن يمكننا إرشادك إلى الحل . تلك التمارين هي في الأساسيات لهذا يفضل أولا مذاكرة الأساسيات ومن ثم حل تلك التمارين . ستحتاجين أولا إلى دراسة القوائم list ويمكنكِ قراءة الدروس التالية على موسوعة حسوب لدارسة كل ما يخص القوائم : القوائم list في بايثون وهذه راوبط الدوال التي يتم طلب حل التمرين بها في القوائم : count sort reverse append insert remove pop أما بخصوص المجموعات set فيمكنك قراءة الدروس التالية : المجموعات set وهذه راوبط الدوال التي يتم طلب حل التمرين بها في المجموعات: add remove discard union difference symmetric_difference intersection والدرس التالي بخصوص بانية القوائم "List Comprehensions" : List_Comprehensions اقتباس
السؤال
Toqa Fayyad
Python Assignment: Lists and Sets
Part 1: Lists
Creating Lists:
Create a list named fruits that contains the following items: "Apple", "Banana", "Cherry", "Apple".
Print the list and its length.
Accessing List Items:
Access and print the second item in the fruits list.
Access and print the last item using negative indexing.
Access and print the first two items using slicing.
Modifying Lists:
Change the second item in the fruits list to "Blueberry".
Add "Orange" to the end of the list using the append() method.
Insert "Mango" at the beginning of the list using the insert() method.
Remove "Cherry" from the list using the remove() method.
Remove the last item from the list using the pop() method.
List Methods:
Use the count() method to find how many times "Apple" appears in the fruits list.
Sort the fruits list in alphabetical order using the sort() method.
Reverse the order of the fruits list using the reverse() method.
List Comprehension:
Create a new list named long_fruits that contains only the fruits with more than 5 characters using list comprehension.
Part 2: Sets
Creating Sets:
Create a set named colors that contains the following items: "Red", "Blue", "Green", "Red".
Print the set and its length.
Accessing Set Items:
Check if "Blue" is in the colors set and print the result.
Loop through the colors set and print each item.
Modifying Sets:
Add "Yellow" to the colors set using the add() method.
Remove "Green" from the set using the remove() method.
Try removing "Purple" using the discard() method and explain the difference between remove() and discard().
Set Methods:
Create another set named new_colors with items: "Blue", "Purple", "Orange".
Use the union() method to combine colors and new_colors and print the result.
Use the difference() method to find items that are in colors but not in new_colors and print the result.
Use the symmetric_difference() method to find items that are in either colors or new_colors but not in both and print the result.
Use the intersection() method to find items that are common to both colors and new_colors and print the result.
2 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.