0% found this document useful (0 votes)
29 views20 pages

INFO 136 Session 2 Slides

The document discusses arrays and lists in Python, describing arrays as containers that hold a fixed number of items of the same type and lists as more versatile containers that can hold elements of different types. It covers basic operations on arrays like traversing, inserting, deleting, searching, and updating elements as well as similar operations on lists. The document also provides examples of creating, initializing, and manipulating array and list elements in Python code.

Uploaded by

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

INFO 136 Session 2 Slides

The document discusses arrays and lists in Python, describing arrays as containers that hold a fixed number of items of the same type and lists as more versatile containers that can hold elements of different types. It covers basic operations on arrays like traversing, inserting, deleting, searching, and updating elements as well as similar operations on lists. The document also provides examples of creating, initializing, and manipulating array and list elements in Python code.

Uploaded by

Classic Kachere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

ARRAYS

Chapter 2: Introduction
Arrays

Objectives
In this Chapter you will learn to:
 Explore the array data structure
 Explore the list data structure
Introduction
 Array is a container which can hold a fix number of items and these items should be of the same type.
 Most of the data structures make use of arrays to implement their algorithms. Following are the
important terms to understand the concept of Array.
 Element− Each item stored in an array is called an element.
 Index − Each location of an element in an array has a numerical index, which is used to identify the
element.
Introduction
 Basic Operations of an Array
 Following are the basic operations supported by an array.
 Traverse − print all the array elements one by one.
 Insertion − Adds an element at the given index.
 Deletion − Deletes an element at the given index.
 Search − Searches an element using the given index or by the value.
 Update − Updates an element at the given index.
Introduction
 Array is created in Python by importing array module to the python program.
 Then, the array is declared as shown below
Introduction
 Creating our first array
 create an array named array1.
Introduction
 Explanation
 from array import *
 The import * it means we are importing everything associated with the array module
 Here we creating an array called array1 and we use the array module.
 We use the typecode (i) indicating that we are dealing with a signed integer
 Elements in the array are accessed from index position 0
 We use an object x to iterate in our array1 so that we can print all the elements in array1
 for x in array1:
 print(x)
 Lets say we just want to print a specific element in our array, we can make use of the index position of the
element.
 We can say print(array1[0])
 We printing the element at index position 0, which means it’s the first element in our array
Deleting an array element
 Deletion refers to removing an existing element from the array and re-organising all elements of an
array.
 We can remove an element from the array, using the python in-built remove() method.
 This will remove element 10 from the array
Searching for an array element
 We can search for an array element based on its value or its index.
 Here, we search a data element, by using the python in-built index() method.
 This will show the index position associated with the element, if the element is not there, an exception
will be raised
 This will print 2, the index position of element 50
Updating an array element
 We can update an array element and assign it a new value.
 To do that, we need to indicate the index of the element we want to update its value and then assign the
new value to the element.
 Element at index position 3 which is 90 has been updated to 200
Using the sum function
 Python has an inbuilt function for the sum
 We can use the function to calculate the sum of array elements
Using the array range
 We can use the range to indicate the elements that we want to print from an array
 The elements to be printed is from index position 1 to index position 2: That’s 100 and 50
 We can also print all the elements starting from a certain index position like index 1:
 This will print all elements 100, 50, 90
Lists in Python
 The list is a most versatile datatype available in Python, which can be written as a list of comma-
separated values (items) between square brackets.
 An important thing about the list is that, items in a list need not be of the same type.
 Here we can create two lists as shown
 We use the for loop to print the elements in the list
Lists in Python
 We can also update a list using the same method we used when dealing with an array
 Here we have changed the element at index position 2 to kadoma
Basic List Operations
task
 Find the largest number in a list
 You are given the following numbers in a list
 number = [200, 10, 1000, 500, 40, 999]
 Find the largest number
solution
List methods
 append you add element at the end of the list
 insert you put an element at a specific index position
 remove you remove an element from the list
 clear you remove all the elements from the list
 Show the output on each method when its called
Sorting elements in a list
 We can sort elements in a list using these two methods: sort and reverse
 Sort method we sort in ascending order
 Reverse method we sort in descending order
Any questions

Any questions?

You might also like