Lists Exercises#

Section Title: Lists Exercises

Question-1#

Create a list with the elements: \(10, 20, 30, 40, 10, 20, 30, 10, 20\) and print the following information about this tuple:

  • The number of occurrences of 30.

  • The element at index 5.

  • The third element.

  • The index of the first occurrence of 20.

  • The index of the second occurrence of 20.

  • The index of the third occurrence of 20.

  • The length (number of elements) of the list.

  • The maximum, minimum, and the sum of the elements.

  • The last four elements using slicing.

  • The first five elements using slicing.

Question-2#

Create a list with the elements: \('a', 5, 8, 'b'\) and perform the following operations on it, printing it after each operation:

  • Add 99.

  • Add 100 as the second element to the list.

  • Add all elements from the list [2, 6, 5, 9, 1, 1, 5].

  • Remove ‘a’ using the pop() method.

  • Remove ‘b’ using the remove() method.

  • Reverse the order of the list.

  • Sort it in ascending order.

  • Sort it in descending order.

Question-3#

Store the unique values in the following list into a list and sort them.

numbers = [9, 4, 10, 9, 9, 2, 7, 9, 8, 6, 4, 6, 10, 5, 1, 7, 7, 5, 9, 2, 5]

Question-4#

Generate a list containing numbers from 4 to 9, inclusive.

  • Construct a multiplication table for these numbers to resemble a 6 by 6 matrix and print it.

  • Store all sum of the values in each row in this multiplication table into a list.

Question-5#

Use list comprehension and random.randint() to construct two lists:

  • Generate ten random numbers between 1 and 20, stored in a list called left_list.

  • Generate ten random numbers between 1 and 10, stored in a list called question_list.

  • Create a new list called right_list, consisting of the differences between 30 and the sum of corresponding numbers in left_list and question_list.

  • For each line, print dashes (-) equal to the number in left_list, followed by question marks equal to the number in question_list, and then dashes equal to the number in right_list.

Sample Output Only first line out of 10.

\(--------------????------------\)

Question-6#

Choose a random 6-digit number and store its unique digits in a list as integers and in descending order.

  • Display the number and its unique digits.

  • Avoid the use of sets.

  • Sample Output:

    • The unique digits of 166464 are [6, 4, 1]

Question-7#

Choose a random 6-digit number and verify if all of its digits are distinct.

  • Avoid the use of sets.

  • Sample Output:

    • The number 986728 contains repeated digits.

    • The number 362951 has distinct digits.

Question-8#

Choose a random 6-digit number whose digits are distinct.

  • Avoid the use of sets.

Question-9#

Choose ten random 6-digit numbers whose digits are distinct and store them in a list as integers.

  • Avoid the use of sets.

Question-10#

Choose two random 6-digit numbers whose digits are distinct and store them in a list as integers.

  • Store the common digits of these two numbers in a list.

  • Avoid the use of sets.

  • Sample Output:

    • The common digits of 479310 and 706843 are [4, 7, 3, 0]

Question-11#

Choose two random 6-digit numbers with distinct digits and store them as integers in a list.

  • Store the common digits of these two numbers in another list.

  • Initialize two count variables with an initial value of zero: count_negative and count_positive.

  • If a common digit appears in the same position in both numbers, increase count_positive by one.

  • If a common digit appears in different positions in both numbers, decrease count_negative by one.

  • Avoid the use of sets.

  • Sample Output-1:

    • The common digits of 247583 and 579063 are [7, 5, 3].

    • Positive Count: 1 — Negative Count: -2

  • Sample Output-2:

    • The common digits of 470829 and 926504 are [4, 0, 2, 9].

    • Positive Count: 0 — Negative Count: -4

  • Sample Output-3:

    • The common digits of 298731 and 928051 are [2, 9, 8, 1].

    • Positive Count: 2 — Negative Count: -2

Question-12#

Prompt the user to input comma-separated integers of their choice.

  • Use the split() method of strings to store the numbers in a list.

  • Print the type of the first number in this new list.

  • Use a list comprehension to generate a new list with all these numbers in integer type.

  • Print the sum of all these numbers.

Question-13#

For the lists given below, begin with the total list initialized as [0, 0, 0].

grade_level = [ 9,  9, 10, 11, 10, 10,  9, 10, 11, 11, 10, 11,  9, 11, 11,  9, 10, 10, 10, 10]
exam_result = [59, 20, 81, 30, 62, 56, 85, 72, 48, 15, 47, 18, 11, 37, 79, 87, 79, 92, 82, 66]

Use a for loop to add each:

  • 9th grader’s exam result to the index 0 position of the total list.

  • 10th grader’s exam result to the index 1 position of the total list.

  • 11th grader’s exam result to the index 2 position of the total list. The final version of the total list should be: [sum of 9th graders’ exam results, sum of the 10th graders’ exam results, sum of the 11th graders’ exam results].

Question-14#

For the lists given below, calculate the average exam grades for each grade level and visualize them using a bar graph.

grade_level = [ 9,  9, 10, 11, 10, 10,  9, 10, 11, 11, 10, 11,  9, 11, 11,  9, 10, 10, 10, 10]
exam_result = [59, 20, 81, 30, 62, 56, 85, 72, 48, 15, 47, 18, 11, 37, 79, 87, 79, 92, 82, 66]