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]

Business Applications#

Question: Max of Revenue#

Revenue (R) is the product of the number (n) of items sold and the price (p) of the item.

  • \(R = p\cdot n\)

The number of item sold is given by the following equation: \(n = -1.5p+30\)

  • Construct a list that consists of the revenues for \(0\le p \le 20\).

  • Plot the graph of revenue for \(0\le p \le 20\)

Question: Profit#

Profit (P) is the difference between Revenue and Cost

  • \(Profit = Revenue - Cost\)

The monthly fixed cost of a cable factory is 1500 dollars. Each cable costs 13 dollars and sells for 25 dollars.

  • For what number of cables produced profit is zero.

    • Hint: Calculate the profit for \(n\) between 1 and 400.

  • Construct three lists that consist of the revenues, costs, and profits for \(0\le p \le 400\).

  • Plot the reveue, cost, profit for producing up to 400 cables.

  • Draw a vertical line where the profit is zero for \(n\).

Sample Plot:

Question: Break-Even Point#

Break-Even Point is the point where total cost equals total revenue, meaning profit is zero.

For the given cost and the revenue functions find the break-even point and plot it.

\(C(n) = 0.5n +2000\)

\(R(n) = 2.5n\)

Hint: Calculate the cost and revenue for n between 1 and 10,000.

Sample Plot:

Question: Exponential Depreciation#

The value \(V\) of a truck in dollars is given by the formula \(V(t)=140000(1.35)^{-0.6t}\) where \(t\) is the age of the truck in years.

  • Plot the graph of values of the truck in 30 years.

Question: Demand#

If the demand function is given as the following linear relation \(p = -2q+300\).

  • Plot the demand function for \(1\le q\le 200\).

Question: Supply Function#

If the supply function is given by the linear relation \(p = 4q+100\).

  • Plot the supply function for \(1\le q\le 100\).

Question: Equilibrium Point#

An equilibrium point is where the demand and supply curves intersect.

For the given demand and the supply functions find the equilibrium point and plot it.

Demand: \(p(q) = -0.03q + 1000\)

Supply: \(p(q) = 0.02q + 400\)

  • Hint: Calculate the demand and supply for \(q\) between 0 and 20,000.

Sample plot:

Question: Stock Percentage Changes#

The following is a list of the prices for Tesla stock over a certain period of time.

stock_prices = [174.9499969482422,
 186.60000610351562,
 180.11000061035156,
 173.74000549316406,
 179.24000549316406,
 176.75,
 176.19000244140625,
 178.7899932861328,
 178.0800018310547,
 176.2899932861328,
 174.77000427246094,
 175.0,
 177.94000244140625,
 177.47999572753906,
 173.7899932861328,
 170.66000366210938,
 177.2899932861328,
 182.47000122070312,
 178.00999450683594,
 187.44000244140625,
 184.86000061035156]

Create a list of percentage changes using the stock_prices list.

  • The first element of the percentage list is np.nan because there is no price available for the previous day.

Question: Stock Behavior#

The following is a list of the prices for Tesla stock over a certain period of time.

stock_prices = [174.9499969482422,
 186.60000610351562,
 180.11000061035156,
 173.74000549316406,
 179.24000549316406,
 176.75,
 176.19000244140625,
 178.7899932861328,
 178.0800018310547,
 176.2899932861328,
 174.77000427246094,
 175.0,
 177.94000244140625,
 177.47999572753906,
 173.7899932861328,
 170.66000366210938,
 177.2899932861328,
 182.47000122070312,
 178.00999450683594,
 187.44000244140625,
 184.86000061035156]

Create a list indicating the behavior of stock prices as Increasing, Decreasing, or Flat using the stock_prices list.

  • Count how many days the stock price went up.

  • The first element of the behavior list is np.nan because there is no price available for the previous day.