0% found this document useful (0 votes)
5 views3 pages

Python Array Representation Guide

The document explains the array representation in Python, focusing on the use of lists and the array module for different data types. It covers basic operations such as accessing, inserting, deleting, searching, and updating elements in arrays, along with practice and programming exercises to reinforce learning. The content is aimed at undergraduate students learning about dynamic and static arrays in Python.

Uploaded by

smartythekartel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Python Array Representation Guide

The document explains the array representation in Python, focusing on the use of lists and the array module for different data types. It covers basic operations such as accessing, inserting, deleting, searching, and updating elements in arrays, along with practice and programming exercises to reinforce learning. The content is aimed at undergraduate students learning about dynamic and static arrays in Python.

Uploaded by

smartythekartel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Array representation in Python

In Python, arrays are typically implemented using the built-in list data structure or the
specialized array module for performance-sensitive tasks with uniform data types. For undergraduate
study, it is most common to first learn about and use list as a flexible, dynamic array, and then introduce
the array module and NumPy as more specialized tools.

Python list as a dynamic array:

• A Python list is an ordered collection of elements that can hold items of different data types.

• Elements are stored in contiguous memory locations, allowing for efficient access by index.

• Python lists are dynamic, meaning their size can change as elements are added or removed.

Python array module as a static array:

• The array module provides a more compact and memory-efficient array for storing a large
number of items of the same data type.

• To create one, you must first import the module and specify a "typecode" to define the data type
of the array's elements. For example, [Link]('i', [1, 2, 3]) creates an array of signed integers.

Basic operations on arrays

Accessing an array element:

• You can access individual elements using an index within square brackets [].

• Indexing is zero-based, meaning the first element is at index 0.

• You can also use negative indices to access elements from the end of the array. For
instance, arr[-1] returns the last element.

Insertion operation:

• Append (append()): Adds a single new item to the end of the array. This is an efficient O(1)
operation.

• Insert (insert(index, item)): Adds a new item at a specified index. This is an O(N) operation
because all subsequent elements need to be shifted.

• Extend (extend(iterable)): Adds multiple elements from an iterable (like another list) to the end
of the array.

Deletion operation:

• Remove by value (remove(item)): Removes the first occurrence of a specified item from the
array. This is an O(N) operation.

• Remove by index (pop(index)): Removes and returns the item at a specified index. If no index is
given, it removes and returns the last item. Both are O(N) for non-end elements.
• Delete by index (del arr[index]): Deletes an element at a specific index without returning it. Also
works with slices.

Search operation:

• Linear search (in operator): A basic search that checks if an item exists in the array. This is simple
but inefficient for large arrays, with a worst-case time complexity of O(N).

• Find index (index(item)): Returns the index of the first occurrence of a specified item. Raises
a ValueError if the item is not found.

Update operation:

• Update by index: To change an element's value, simply access it by index and use the
assignment operator (=). This is an efficient O(1) operation.

• Update by slicing: You can update a range of elements by assigning a new list to a slice of the
array.

Practice exercises

1. Multiple choice: What is the time complexity of adding an element to the end of a standard
Python list using append()?
a. O(1)
b. O(N)
c. O(N log N)
d. O(N²)

2. Fill-in-the-blanks: Complete the following code snippets.


a. To import the array module, you write import ______________.
b. To get the third element of an array named data, you would write data[______].
c. The remove() method deletes an element by its ______________, while pop() deletes it by its
______________.

3. True or false: For a standard Python list, accessing the first element is faster than accessing the
last element.

4. Short answer: Explain why using the insert() method to add an element at the beginning of a
large list can be computationally expensive. What happens to the other elements?

Programming exercises

1. Array creation and traversal:

a. Create a Python list containing the integers 10, 20, 30, 40, 50.

b. Using a for loop, iterate through the list and print each element.
c. Use another for loop to iterate through the list and print each element's index and value
(e.g., "Index 0: 10").

2. Insertion and deletion:

a. Start with the list [1, 2, 3, 4, 5].

b. Insert the number 99 at index 2. Print the list.

c. Append the number 100 to the end of the list. Print the list.

d. Remove the number 4 from the list. Print the list.

e. Remove the element at index 0 using pop(). Print the list.

3. Search and update:

a. Start with the list ['apple', 'banana', 'cherry', 'date'].

b. Write a program to search for the string 'cherry'. If found, print its index.

c. Update the element at index 1 from 'banana' to 'blueberry'.

d. Use the in operator to check if 'pear' exists in the list and print the result.

e. Print the final list.

4. Combining operations (challenge):

a. Create a list of 10 random integers between 1 and 100.

b. Find and print the largest number in the array.

c. Find and print the smallest number in the array.

d. Find the index of the first occurrence of the largest number and print it.

e. Create a new list that is a sorted version of the original list and print it

You might also like