📖 Scenario: You are organizing a small fruit basket for a picnic. You want to keep track of the fruits you have by listing them in order.
🎯 Goal: Create an array (list) of fruits, add a new fruit count, and then display the list of fruits.
📋 What You'll Learn
Create a list called fruits with the exact elements: 'apple', 'banana', 'cherry'
Create a variable called new_fruit and set it to the string 'orange'
Add the new_fruit to the fruits list using the append method
Print the fruits list to show all fruits
💡 Why This Matters
🌍 Real World
Lists are used everywhere to keep track of collections like shopping items, tasks, or contacts.
💼 Career
Knowing how to create and update lists is a basic skill for programming jobs, especially in data handling and user interface development.
Progress0 / 4 steps
1
Create the initial array of fruits
Create a list called fruits with these exact elements in order: 'apple', 'banana', 'cherry'
DSA Python
Hint
Use square brackets [] to create a list and separate items with commas.
2
Add a new fruit variable
Create a variable called new_fruit and set it to the string 'orange'
DSA Python
Hint
Use the equals sign = to assign the string 'orange' to new_fruit.
3
Add the new fruit to the fruits list
Use the append method on the fruits list to add the new_fruit variable
DSA Python
Hint
Call fruits.append(new_fruit) to add the new fruit to the list.
4
Print the fruits list
Print the fruits list to display all the fruits after adding the new one
DSA Python
Hint
Use print(fruits) to show the list.
Practice
(1/5)
1. Which of the following is the correct way to declare and initialize an array of integers with values 1, 2, and 3 in Python?
easy
A. arr = (1, 2, 3)
B. arr = [1, 2, 3]
C. arr = {1, 2, 3}
D. arr = int[3]
Solution
Step 1: Understand Python array syntax and check each option
In Python, arrays are usually represented by lists, which use square brackets []. arr = [1, 2, 3] uses square brackets with values 1, 2, 3 which is correct. arr = (1, 2, 3) uses parentheses which create a tuple, not an array. arr = {1, 2, 3} uses curly braces which create a set, not an array. arr = int[3] is invalid syntax in Python.
Final Answer:
arr = [1, 2, 3] -> Option B
Quick Check:
List syntax = [1, 2, 3] [OK]
Hint: Use square brackets [] for arrays (lists) in Python [OK]
Common Mistakes:
Using parentheses () instead of square brackets
Using curly braces {} which create sets
Trying to declare array size like int[3]
2. Which of the following is the correct syntax to create an empty list in Python?
easy
A. list = ()
B. list = {}
C. list = []
D. list = empty()
Solution
Step 1: Identify empty list syntax and evaluate options
In Python, an empty list is created using empty square brackets []. list = [] uses [], which is correct. list = () uses parentheses which create an empty tuple. list = {} uses curly braces which create an empty dictionary. list = empty() is invalid syntax.
Final Answer:
list = [] -> Option C
Quick Check:
Empty list = [] [OK]
Hint: Empty list always uses [] brackets [OK]
Common Mistakes:
Using () which creates a tuple
Using {} which creates a dictionary
Trying to call empty() function which doesn't exist
3. What will be the output of the following code?
arr = [0] * 4
print(arr)
medium
A. Error
B. [0, 1, 2, 3]
C. [4, 4, 4, 4]
D. [0, 0, 0, 0]
Solution
Step 1: Understand list multiplication and predict the output
Multiplying a list by an integer repeats the list elements that many times. Here, [0] * 4 creates a list with four zeros. The print statement outputs the list: [0, 0, 0, 0].
Final Answer:
[0, 0, 0, 0] -> Option D
Quick Check:
[0] * 4 = [0, 0, 0, 0] [OK]
Hint: List * n repeats elements n times [OK]
Common Mistakes:
Expecting a range of numbers instead of repeated zeros
Confusing list multiplication with addition
Thinking it causes an error
4. Find the error in the following code snippet:
arr = [1, 2, 3]
arr[3] = 4
print(arr)
medium
A. IndexError: list assignment index out of range
B. SyntaxError
C. [1, 2, 3, 4]
D. [4, 2, 3]
Solution
Step 1: Check list indices and analyze assignment at index 3
List indices start at 0. For a list of length 3, valid indices are 0, 1, 2. Trying to assign arr[3] = 4 is invalid because index 3 does not exist yet, causing an IndexError.
Final Answer:
IndexError: list assignment index out of range -> Option A
Quick Check:
Index 3 invalid for length 3 list [OK]
Hint: List indices go from 0 to length-1 [OK]
Common Mistakes:
Trying to assign to an index outside the list
Confusing append with direct index assignment
Expecting automatic list resizing
5. You want to create a 2D array (list of lists) with 3 rows and 4 columns, all initialized to zero. Which code correctly does this without causing unexpected behavior?
hard
A.
arr = [[0 for _ in range(4)] for _ in range(3)]
print(arr)
B.
arr = [[0]*4]*3
print(arr)
C.
arr = [0]*3
for i in range(4):
arr[i] = [0]*4
print(arr)
D.
arr = [[0]*3 for _ in range(4)]
print(arr)
Solution
Step 1: Understand 2D array creation pitfalls and analyze each option
Using [[0]*4]*3 creates 3 references to the same inner list, causing changes in one row to affect others.
arr = [[0]*4]*3
print(arr)
creates shared inner lists (bad).
arr = [[0 for _ in range(4)] for _ in range(3)]
print(arr)
uses nested list comprehension creating independent inner lists (correct).
arr = [0]*3
for i in range(4):
arr[i] = [0]*4
print(arr)
has index errors and wrong loop ranges.
arr = [[0]*3 for _ in range(4)]
print(arr)
creates 4 rows and 3 columns, which is reversed.
Final Answer:
arr = [[0 for _ in range(4)] for _ in range(3)] -> Option A
Quick Check:
Nested list comprehension avoids shared references [OK]
Hint: Use nested list comprehension for independent inner lists [OK]
Common Mistakes:
Using multiplication for nested lists causing shared references
Mixing up rows and columns sizes
Index errors in loops when initializing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.