[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Array in Python – Introduction

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1570899449535{background-color: #f6f6f6 !important;}”]

An array in Python is a collection of elements where each element is being assigned an index number and are consisting of the same data type. The elements can be picked out and used using the index.

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

Why use an Array in Python?

We have previously seen how we can store values in variables, for example:

 

var = 10

The variable var, of type integer, is assigned the value 10.

 

But what if we have a hundred values that we want to save at the same time? A thousand values? One million values? In order to handle a very large amount of data at the same time, we use arrays (also called fields, or in other programming languages; lists). This allows us to save multiple values in one variable.[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal” up=”1″][vc_empty_space height=”10px”][vc_column_text]

What is an Array in Python?

Let’s start by taking a look at the so called one-dimensional array. We can think of an array as a collection or block of values. For example, we can create an array of integers:

 

my_list = [5, 2, 8, 9, 3]

my_list now contains 5 values.

You can print the entire list at once:

 

print(my_list)

and get the result

 

[5, 2, 8, 9, 3]

 

That is, instead of creating separate variables for each value, we can instead create an array that contains all values at the same time.

 

In Python, an array is a collection of elements, where each element is numbered and of the same data type.

 

When we develop our programs we will often handle collections of objects of the same type and then an array is a very useful tool to be able to structure and group the objects together.[/vc_column_text][vc_empty_space height=”10px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” up=”1″][vc_empty_space height=”10px”][vc_empty_space height=”20px”][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner width=”2/3″][vc_column_text]

How does an Array work in Python?

As we have seen before, a variable can be seen as an object or a box that contains a data type and which also can have an assigned value. The image to the right illustrates a variable that has the name var and the value 23. [/vc_column_text][/vc_column_inner][vc_column_inner width=”1/3″][vc_single_image image=”18791″ img_size=”39×48″ qode_css_animation=””][/vc_column_inner][/vc_row_inner][vc_empty_space height=”20px”][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner width=”2/3″][vc_column_text]In a similar way, we can now think of an Array as a box that has several boxes inside. We can access each box using its index and in that way use it, read the value and also edit that value. The image to the right illustrates how you might imagine an array looks, to make it easier to understand what it is. In the image there is an array with the name field that has a set of different values.[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/3″][vc_empty_space height=”15px”][vc_single_image image=”19513″ img_size=”medium” alignment=”center” qode_css_animation=””][/vc_column_inner][/vc_row_inner][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]Now that we have got a basic understanding of what an array in python is, we are now going to go on and see how we actually can create and use the array. Then we will also look at a few different uses and examples. We will also look at some simple errors that are easy to occur if you are not observant.[/vc_column_text][vc_empty_space height=”70px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row]