top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Writer's pictureBechir Mathlouthi

Python Arrays

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 student names, for example), storing the students in single variables could look like this:



student1 = "john"
student2 = "jimmy"
student3 = "chris"

but what if you had not 3 students, but 500 and you want to loop through students and find a specific one


An array can hold many values under a single name, and you can access the values by referring to an index number.


Access the Elements of an Array:

ou refer to an array element by referring to the index number.


Get the value of the first array item:
x = student[5]

The Length of an Array:


Use the len() method to return the length of an array (the number of elements in an array).


Return the number of elements in the students array:
x = len(students)

Looping Array Elements :

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



Print each item in the cars array:
    for x in cars:
     print(x)

Adding Array Elements :