KENDRIYA VIDYALAYA SECL NOWROZABAD
SUBJECT : COMPUTER
CHAPTER 4 : Programming With Arrays
* COLLECTIONS : A collection is nothing but a container that groups multiple
items into a single object. Collections are used to store data. We can also
retrieve and manipulate data that is stored in the Collections.
*Real World example of collections :
1. Telephone Directory
2. Collections of Cards
3. Mail box
* ARRAY : An Array is a collection of similar data type variables. Arrays do not
support different data types in same collection.
An array can be declared in following format:
arr = [10, 20, 30, 40, 50, 60];
*Real World example of an Arrays :
1. Group of teachers 6. Music Playlists
2. Group of Students 7. Contact Lists
3. Book of Library 8. Game Leaderboards
4. Egg Cartons 9. Question Papers
5. Muffin Trays 10. Robotics
11. Matrices
* Arrays in Programming :
* Limitations of an Array :
1. You can only store data with same data types in an array.
2. The variables in an array are always ordered sequentially with index
starting with 0.
3. Once a Array is created, its size cannot be changed.
4. Memory wastage
* How to create empty array of sequence of integers between 0 to 11:
To Create an empty array. Begin a loop from 0 to 11. Append every item to the
array.
* append ( )method : This method is used to add an element to an array.
* pop( ) method : To remove an element of an array we use pop( ) method.
* Sorting in Array : Sorting is the process of ordering items in a collection.
Python has inbuilt sort function to order an array. The sort() method sorts the
list ascending or increasing order by default.
Example :
Result or Output :
* Searching in an Array : Python uses indexing as a method to search for an
element in an array.
If you run the above code, you would get 3 as output.
The index method works on numeric arrays too:
If you run the above code, you would get 4 as output.
* Sorting an Array using Bubble sort method :
Bubble sort is a method of sorting that works by repeatedly swapping
adjacent elements if they are in incorrect order.
Example : Sort the given array using bubble sort method :
1 5 4 3 2
Ans : Step 1 :
1 5 4 3 2
Step 2:
1 5 4 3 2
Step 3 :
1 4 5 3 2
Step 4 :
1 4 3 5 2
Step 5 :
1 4 3 2 5
Step 6 :
1 4 3 2 5
Step 7 :
1 3 4 2 5
Step 8 :
1 3 2 4 5
Step 9 :
1 3 2 4 5
Step 10 :
1 3 2 4 5
Step 11 :
1 2 3 4 5
Step 12 :
1 2 3 4 5
Step 13 :
1 2 3 4 5
Now the all numbers are sorted in ascending order.
EXERCISE
Q.1 Which statement from below best describes Arrays?
Option1 A data structure that shows a hierarchical behavior
Option2 Container of objects of similar data types (√)
Option3 Array is not a data structure
Q.2 Which of the following are advantages of arrays?
Option1 Easier to store elements of similar data type (√)
Option2 Elements stored in an array cannot be sorted
Option3 None of the above
Q.3 Which of the following are disadvantages of arrays?
Option1 There are chances of wastage of memory space if elements inserted
in an array are lesser than the allocated size. (√)
Option2 Index value of an array can be negative.
Option3 Elements are sequentially accessed.