Python Arrays :

Arrays :

C++ Language Arrays | C# Language Arrays - Arrays are used to store multiple values in one single variable.

Example :

  1. books = ["Math","English","History","Bengali"]
  2. print(books)
  3. input()

Output :

python array example

Note : Python does not have built-in support for Arrays, but Python Lists can be used instead.


What is an Array ?

An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of books names, for example), storing the books in single variables could look like this:

book1 = "Math";
book2 = "English"
book3 = "History"
book4 = "Bengali"


However, what if you want to loop through the books and find a specific one? And what if you had not 4 books, but 400? - The solution is an array!
An array can hold many values under a single name, and you can access the values by referring to an index number.


Access or Modify the Elements of an Array :

You refer to an array element by referring to the index number and we use python built-in methods to modify the array values.

Example 1 :

  1. books = ["Math","English","History","Bengali"]
  2. a = books[0]; #Get the value of the first array item
  3. print("First value is: ",a);
  4. books[0] = "Computer" #Modify the value of the first array item
  5. print(books);
  6. a = len(books); #Return the number of elements in the books array
  7. print("Total array Length: ",a);
  8. input()

Output 1 :

python array values accessing

Note : The length of an array is always one more than the highest array index.

Example 2 :

  1. books = ["Math","English","History","Bengali"]
  2. books.append("Computer") #Add one more element to the books array
  3. print(books)
  4. books.pop(1) #Delete the second element of the books array
  5. print(books)
  6. books.remove("History") #Delete the element that has the value "History"
  7. print(books)
  8. input()

Output 2 :

python array values modifying

Array methods :

Python includes many built-in methods but here we just adding some of the following built-in methods to manipulate array values :

  • The append() method to adds an element at the end of the list
  • The clear() method to removes all the elements from the list
  • The copy() method to returns a copy of the list
  • The count() method to returns the number of elements with the specified value
  • The index() method to returns the index of the first element with the specified value
  • The insert() method to adds an element at the specified position
  • The pop() method to removes the element at the specified position
  • The remove() method to removes the first item with the specified value
  • The reverse() method to reverses the order of the list
  • The sort() method to sorts the list
  • The extend() method to add the elements of a list (or any iterable), to the end of the current list

Note : Python does not have built-in support for Arrays, but Python Lists can be used instead.


Looping Array Elements :

You can use the for loop to loop through all the elements of an array.

Example :

  1. books = ["Math","English","History","Bengali"]
  2. for a in books:
  3. print(a)
  4. input()

Output :

python array through for loop

Note : Python does not have built-in support for Arrays, but Python Lists can be used instead.


Next Update

Loading...

Currently we improved our website User Interface and other contents. Our next update is adding video tutorials on this site so, We create a new YouTube channel, our YouTube channel name is CodeX.

We need some time for make video tutorials (CodeX) so, stay with us and keep supporting and you know already we make a Collaboration with HackIsOn YouTube channel long time ago So, please Subscribe Our Channels