Change language

Python arrays – Python tutorial for beginners – CodeBerry Programming School

Python arrays – Python tutorial for beginners – CodeBerry Programming School

Hi everyone and welcome.

My name is John and this is a CodeBerry programming school, video on python.

And today were going to be looking at an array.

So an array is a data structure in python where all the elements have to be of the same type.

For example, they all have to be integers or numbers or strings or characters.

So how do we create an array? So we need to import it into our file.

So the first thing that we need to do at the top of our pie file is import and then array and Im going to import it as a arr.

So its easier to type.

This lets us access to class data objects from the python standard library.

The next thing that we need to do is define a name for our array.

So Im going to just going to call mine abc for lack of a better way.

The next thing we need to do is we need to access our class and as we imported it as arr we are going to do that.

And then Im going to type the word array Now, the next thing that comes is quite important.

So the whole thing is in the brackets.

However, the first letter or the first thing that we put in denotes the data type.

That is going to go into this specific array.

So if I come over to this tab, we can see that on the left hand side here.

Theres the Type code and then the Python type.

And this is what we are going to use to put into the first part of our rate to determine what sort of array it has or what, Sorry? Sorry, what values it has in it.

So Im going to use d in this case and we can see down here, if we use d, we get a float and its a double float, which basically just gives us a few more extra bytes to play with.

I want to copy d.

Here, Im going to come back and then well put it in so now we can see that our array is a data type with a d.

Which is a float.

As we just explained, the next thing we need is the comma and now we can use the square brackets to actually input the values that we want into this array.

So because these are all floats, Im going to be using decimal point numbers.

So lets just do 2.5 and then a comma 3.6, another comma 5.6 And then lets do 11.5 There we go.

So now that saved that is essentially our array.

So what weve done is weve had to import it in, weve called it arr Thats what this means when you import something, as its just a quicker way of typing it.

Were accessing the array class were saying that our data type is d, which is for floats and weve got our floating point numbers in here.

So if I was to just print the whole thing out, were going to get this back, we can see that we have everything back, it tells us thats an array and all the data.

We just went through, We can now loop through this array, like we would do in any other way we can use a for loop.

So Im just going to for i in abc.

And Im going to print(i).

So this is going to loop through all of the values that are in our array, which is these ones here, its going to display them on the screen.

There we go.


Its worth noting we can actually index this as well.

So we can say print(abc).

And lets print the first item we should be 2.5 And then the 3rd one, Which is 11.5 You also slice with minus numbers, so [-1] will be the end number there it is.

So to insert a new value into our array, we simply need to do our array name, which is abc.

And then dot insert, open up our brackets.

And now we need to choose the location that were going to insert into our array just by using the indexing that I just showed you.

So Im going to do zero because that is where I want to put it right at the very beginning and then the value, Im going to do 1.8 and then going to print out the whole of the array again, So we can see that that has been added right at the front down here.

You can see 1.8.

If I was to try and input a character, lets say h its going to come back and its going to give us an error because its a it must be a real number, which is what weve chosen here.

So lets change that back for 1.8 There we go.

If we wanted to modify a value that is in our array, lets just close that up there.

We can do that by saying we want abc to determine which array we want to change.

And were going to say our index and this time were going to use a square bracket.

So were going to say we want to modify the first index, And then were going to change this to the value of 1.9.

So Im going to copy the print statement.

Im going to put it underneath.

So were going to see here that were going to add in 1.8 to the beginning of the array.

But then were going to change it to 1.9.

We can see right there.

We added in 1.8 and then we changed it to 1.9 So thats it guys.

Thats it for this one.

Hopefully youve got some good value out of this one and you understand a bit more about how an array works and how you can use it.

Just remember that.

You can only have one data type in it at a time So if youve enjoyed this video, go ahead and check out for more.

Ive been John and Ill see you in the next one.



Learn programming in R: courses


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Latest questions


Common xlabel/ylabel for matplotlib subplots

12 answers


How to specify multiple return types using type-hints

12 answers


Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers


Flake8: Ignore specific warning for entire file

12 answers


glob exclude pattern

12 answers


How to avoid HTTP error 429 (Too Many Requests) python

12 answers


Python CSV error: line contains NULL byte

12 answers


csv.Error: iterator should return strings, not bytes

12 answers



Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python


How to specify multiple return types using type-hints


Printing words vertically in Python


Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries


Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically