In this Python tutorial we will learn about Python Arrays and we will also cover different examples related to Python Sets.
- Python array
- Creating an array
- Adding elements to a array
- Accessing elements from the array
- Removing elements from the array
Python array
In Python array is a group of items supply the neighbouring memory location. It can store the multiple items of the identical type. The array can make it simple to compute the position of each element.
Array can hold in python by simply using a module called as an array. It can be very helpful when we have to operate only a particular data type values.
So, with this, we have learned about the array which can be done in the python.
Creating an array
Array can be created in python by simply imported an array module. The array() function is used to create an array with data type in its argument.
Example:
In the following example we are creating an array and the array is created by simply using an array() function.
# Python program to demonstrate Create of Array
# Import array library
import array as arr
# creating an array with an integer type
m = arr.array('i', [2, 4, 6])
# printing an original array
print ("New created array is : ", end =" ")
for i in range (0, 3):
print (m[i], end =" ")
print()
# creating an array with double type
n = arr.array('d', [3.5, 4.2, 4.3])
# printing original array
print ("New created array is : ", end =" ")
for i in range (0, 3):
print (n[i], end =" ")
Output:
After running the above code we get the following output in which we can see that the after using the array() function array is created.
Github Link
Check this code in Repository from Github and you can also fork this code.
Github User Name: PythonT-Point
Adding elements to a array
In python the elements can be added by simply using the insert() function. Insert function is used to add one or more data elements to an array.
Example:
In the following example we are adding the elements to an elements by simply using the insert() function.
- m = arr.array(‘i’, [2, 4, 6,8]) is used as array with the int type.
- print (“Array before insertion : “, end =” “) is used to print the array before insertion.
- m.insert(2, 5) is used to inserting an array using insert() function.
- n = arr.array(‘d’, [3.5, 4.2, 5.3,6.4]) is used an array with float type.
- n.append(5.4) is used as adding an array using append() function.
# Python program to demonstrate Add elements to an Array
# importing an "array" for array creations
import array as arr
# array with int type
m = arr.array('i', [2, 4, 6,8])
print ("Array before insertion : ", end =" ")
for i in range (0, 4):
print (m[i], end =" ")
print()
# inserting array using insert() function
m.insert(2, 5)
print ("Array after insertion : ", end =" ")
for x in (m):
print (x, end =" ")
print()
# array with float type
n = arr.array('d', [3.5, 4.2, 5.3,6.4])
print ("Array before insertion : ", end =" ")
for x in range (0, 4):
print (n[x], end =" ")
print()
# adding an element using append()
n.append(5.4)
print ("Array after insertion : ", end =" ")
for x in (n):
print (x, end =" ")
print()
Output:
In the following output you can see that the array is inserting using the insert() function and append() function.
Read: Python Sets
Accessing elements from the array
In Python the elements of the array can be access to the index number. The index[] operator is used to access an item in an array.
Example:
In the following code we will accessing the elements from the array by using the index[] operator.
- m = ary.array(‘i’, [2, 4, 6, 8, 10, 12]) is used an array with int type.
- print(“Accessing the element is: “, m[1]) is used to print an accessing elements of array.
- n = ary.array(‘d’, [3.5, 4.2, 5.3,6.7]) is used as an array with float type.
# accessing of element from list and importing array module
import array as ary
# array with int type
m = ary.array('i', [2, 4, 6, 8, 10, 12])
# accessing element of array
print("Accessing the element is: ", m[1])
# accessing element of array
print("Accessing the element is: ", m[4])
# array with float type
n = ary.array('d', [3.5, 4.2, 5.3,6.7])
# accessing element of array
print("Accessing the element is: ", n[2])
# accessing element of array
print("Accessing the element is: ", n[3])
Output:
After running the above code we get the following output in which we can see that the elements of an array can be access to the index number.
Read:Python Dictionary
Removing elements from the array
In Python for removing the elements the remove() function is used. The remove() only remove one element at a time.
Here the remove() function in the list only remove the second event of the searched element.
Example:
In the following code we will remove the element from the array by using the remove() function.
- m = array.array(‘i’, [2, 4, 6, 8, 10,12]) is used to initializing the array with the array values.
- print (m.pop(3)) is used as to remove element at the 3rd position.
- m.remove(2) is used remove() to remove 2nd occurrence of 2.
# Python program to demonstrate the removal of elements in a Array
# importing "array" for array operations
import array
# initializing array with array values
m = array.array('i', [2, 4, 6, 8, 10,12])
# printing the original array
print ("The new created array is : ", end ="")
for i in range (0, 6):
print (m[i], end =" ")
print ("\r")
# using pop() to remove element at 3rd position
print ("The pop element is : ", end ="")
print (m.pop(3))
# printing array after popping
print ("The array after popping is : ", end ="")
for i in range (0, 5):
print (m[i], end =" ")
print("\r")
# using remove() to remove 2nd occurrence of 2
m.remove(2)
# printing array after removing
print ("The array after removing is : ", end ="")
for i in range (0, 4):
print (m[i], end =" ")
Output:
After running the above code we get the following output in which we can see that the element are popped and then after popping removing the elements from the array.
So, in this tutorial, we have learned about the Python Arrays and we have covered all these topics.
- Python array
- Creating an array
- Adding elements to a array
- Accessing elements from the array
- Removing elements from the array
Do follow the following tutorials also:
Cool blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweeks would really make my blog jump out. Please let me know where you got your design. Many thanks
I like what you guys are up too. Such clever work and reporting! Carry on the excellent works guys I have incorporated you guys to my blogroll. I think it’ll improve the value of my site 🙂
I was recommended this blog by my cousin. I’m no longer positive whether or not this post is written by way of him as no one else know such particular approximately my problem. You’re wonderful! Thanks!
I have read a few just right stuff here. Definitely worth bookmarking for revisiting. I surprise how a lot effort you put to make this sort of fantastic informative website.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Good write-up, I am normal visitor of one¦s site, maintain up the excellent operate, and It’s going to be a regular visitor for a long time.
I’m extremely inspired along with your writing abilities as smartly as with the structure for your blog. Is that this a paid theme or did you modify it yourself? Either way stay up the excellent quality writing, it’s uncommon to peer a nice blog like this one today..
I truly appreciate this post. I’ve been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thx again!
Your work has captivated me just as much as it has you. The sketch you’ve created is tasteful, and the material you’ve written is impressive. However, you seem anxious about the prospect of presenting something that could be considered questionable. I believe you’ll be able to rectify this situation in a timely manner.
After all, what a great site and informative posts, I will upload inbound link – bookmark this web site? Regards, Reader.
I loved as much as you will receive carried out right here The sketch is tasteful your authored subject matter stylish nonetheless you command get got an edginess over that you wish be delivering the following unwell unquestionably come further formerly again as exactly the same nearly very often inside case you shield this hike
Hi i think that i saw you visited my web site thus i came to Return the favore Im attempting to find things to enhance my siteI suppose its ok to use a few of your ideas
Wow wonderful blog layout How long have you been blogging for you make blogging look easy The overall look of your site is great as well as the content
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
At this time it looks like WordPress is the preferred blogging platform out there right now. (from what I’ve read) Is that what you’re using on your blog?
Some truly fantastic information, Glad I found this.
The degree of my fascination with your creations is equal to your own enthusiasm. The sketch is tasteful, and the authored material is of a high caliber. Yet, you appear uneasy about the prospect of heading in a direction that could cause unease. I’m confident you’ll be able to resolve this situation efficiently.
Site fantástico Muitas informações úteis aqui estou enviando para alguns amigos e também compartilhando deliciosos E claro obrigado pelo seu esforço
I am constantly thought about this, regards for putting up.
Baddiehubs I am truly thankful to the owner of this web site who has shared this fantastic piece of writing at at this place.
helloI like your writing very so much proportion we keep up a correspondence extra approximately your post on AOL I need an expert in this space to unravel my problem May be that is you Taking a look forward to see you
Simply desire to say your article is as surprising The clearness in your post is simply excellent and i could assume you are an expert on this subject Fine with your permission let me to grab your feed to keep up to date with forthcoming post Thanks a million and please carry on the gratifying work
you are truly a just right webmaster The site loading speed is incredible It kind of feels that youre doing any distinctive trick In addition The contents are masterwork you have done a great activity in this matter
лицензионные платформы для игр на деньги, официальные зеркала, бонусы и фриспины. рейтинг казино с выводом на карту sjnjpgucbq
Sky Scarlet This is my first time pay a quick visit at here and i am really happy to read everthing at one place
GlobalBllog I’m often to blogging and i really appreciate your content. The article has actually peaks my interest. I’m going to bookmark your web site and maintain checking for brand spanking new information.
Fran Candelera This is really interesting, You’re a very skilled blogger. I’ve joined your feed and look forward to seeking more of your magnificent post. Also, I’ve shared your site in my social networks!
Fran Candelera You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
Your writing is not only informative but also incredibly inspiring. You have a knack for sparking curiosity and encouraging critical thinking. Thank you for being such a positive influence!
Blue Techker Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
Aroma Sensei Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
Isla Moon very informative articles or reviews at this time.
Noodlemagazine This is my first time pay a quick visit at here and i am really happy to read everthing at one place
Noodlemagazine I truly appreciate your technique of writing a blog. I added it to my bookmark site list and will
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.