Python array | numpy for python | How to create numpy or np array in python

  1. What is an array?
  2. Important points regarding python array (list)
  3. Built-in methods for python array
  4. Python program to use all below methods for python array (list).
    append()‘, ‘count()‘, ‘copy()‘, ‘remove()‘, ‘clear()‘, ‘extend()‘, ‘index()‘, ‘pop()‘, ‘insert()‘, ‘reverse()‘, ‘sort()
  5. What is a python numpy array?
  6. How to create numpy or np array in python?
  7. Python program to use all below methods to create python numpy array.
    ‘np.array()’, ‘np.zeros()’, ‘np.ones()’, ‘np.empty()’, ‘np.arange()’, ‘np.linspace()’

What is an array?

An array can be defined as the collection of data items. In C/C# programming language array is a collection of items of similar data type.

Python does not have built-in support for array, Python List can be used in place of python array.

Jump to top of the page↵

Important points regarding python array (list)

Below are few useful points regarding the python array (list)

  1. Python array (list) starts with index 0.
  2. Python array (list) can be multi-dimensional.
  3. Searching and sorting are very convenient.

Jump to top of the page

Built-in methods for python array

Below are the built-in methods for the python array.

  1. append()
  2. count()
  3. copy()
  4. remove()
  5. clear()
  6. extend()
  7. index()
  8. pop()
  9. insert()
  10. reverse()
  11. sort()

Jump to top of the page

EXAMPLE: Using the above python array methods in the program.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 13 23:35:25 2021
website : http://mycloudplace.com
@author: mycloudplace
"""
if __name__ == "__main__":
    #Creating empty Array (list)
    arr_methods = ['append()', 'count()', 'copy()', 'remove()', 'clear()', 'extend()', 'index()', 'pop()', 'insert()', 'reverse()', 'sort()']
    print("Using below methods in our program")
    for i in range(len(arr_methods)):
        print(i+1, ":", arr_methods[i])
    
    arr = []
    print("appending numbers to array")
    for i in range(10):
        arr.append(i)
    print(arr)
    
    print("Getting the counts of a value in an array")
    arr.extend([4,4])
    print(arr)
    print(arr.count(4))
    
    arr_copy = arr.copy()
    print(arr_copy)
    
    print("Removing all items from the array 'arr_copy'")
    arr_copy.clear()
    print(arr_copy)
    
    print("Removing the first item from list")
    arr.remove(4)
    print(arr)
    
    print("Removing the items from specified position")
    arr.pop(5)
    print(arr)
    
    print("Inserting a vluee at specified position ")
    arr.insert(5,99)
    print(arr)
    
    print("Getting the index of first item with specified value")
    idx = arr.index(4)
    print(idx)

    print("Reversing the order of list items")
    arr.reverse()
    print(arr)
    
    print("Sorting the items of list")
    arr.sort()
    print(arr)

Jump to top of the page

OUTPUT:

Using below methods in our program
1 : append()
2 : count()
3 : copy()
4 : remove()
5 : clear()
6 : extend()
7 : index()
8 : pop()
9 : insert()
10 : reverse()
11 : sort()
appending numbers to array
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Getting the counts of a value in an array
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 4]
3
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 4]
Removing all items from the array 'arr_copy'
[]
Removing the first item from list
[0, 1, 2, 3, 5, 6, 7, 8, 9, 4, 4]
Removing the items from specified position
[0, 1, 2, 3, 5, 7, 8, 9, 4, 4]
Inserting a vluee at specified position 
[0, 1, 2, 3, 5, 99, 7, 8, 9, 4, 4]
Getting the index of first item with specified value
9
Reversing the order of list items
[4, 4, 9, 8, 7, 99, 5, 3, 2, 1, 0]
Sorting the items of list
[0, 1, 2, 3, 4, 4, 5, 7, 8, 9, 99]

What is a python numpy array?

Python numpy stands for ‘Numerical Python’. Numpy array is similar to python list but they are different in computing speed and performance. Numpy is a scientific library that is the core of scientific computing. Python numpy array is mainly used in artificial intelligence and machine learning projects.

Before going to use numpy, make sure the python numpy package is installed. python numpy array can be installed using the below commands

  • pip install numpy
  • conda install numpy

Jump to top of the page

How to create a numpy or np array in python?

To create np array we first import the numpy package in our program.

We can use below python numpy methods to create numpy array

  1. np.array()
  2. np.zeros()
  3. np.ones()
  4. np.empty()
  5. np.arange()
  6. np.linspace()

Jump to top of the page

EXAMPLE: Using the above python numpy methods in the program.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 14 10:50:44 2021
@author: hadoop
website : http://mycloudplace.com
@author: mycloudplace
"""
import numpy as np
if __name__ == "__main__":
    print("Creating a basic numpy array")
    arr_np = np.array([1,2,3,4,5])
    print(arr_np)
    
    print("Creating numpy array of length 5, all filled with value 0.")
    arr_np_zero =  np.zeros(5)
    print(arr_np_zero)
    
    print("Creating numpy array of length 5, all filled with value 1.")
    arr_np_ones =  np.ones(5)
    print(arr_np_ones)
    
    print("Creating empty array wity elements 5")
    arr_np_empty = np.empty(10)
    print(arr_np_empty) #this can be vary based on data in memory
    
    print("Creating numpy array using arange method, it is similar to range buit-in method")
    arr_np_arange = np.arange(3,17,2)
    print(arr_np_arange)
    
    print("Creating value which are linearly distributed")
    arr_np_linspace =  np.linspace(10, 20, num=4)
    print(arr_np_linspace)

Jump to top of the page

OUTPUT:

Creating a basic numpy array
[1 2 3 4 5]
Creating numpy array of length 5, all filled with value 0.
[0. 0. 0. 0. 0.]
Creating numpy array of length 5, all filled with value 1.
[1. 1. 1. 1. 1.]
Creating empty array wity elements 5
[4.68572563e-310 0.00000000e+000 6.93171145e-310 6.93181424e-310
 6.93171145e-310 0.00000000e+000 6.93181424e-310 6.93181424e-310
 6.93181424e-310 0.00000000e+000]
Creating numpy array using arange method, it is similar to range buit-in method
[ 3  5  7  9 11 13 15]
Creating value which are linearly distributed
[10.         13.33333333 16.66666667 20.        ]

Jump to top of the page

Below is our previous articles

Reference:

10 thoughts on “Python array | numpy for python | How to create numpy or np array in python”

  1. Pingback: File Handling Python With Examples | Python Input-Output Awesome Tips - Mycloudplace

  2. Pingback: Python reporting libraries | Python reporting tools to generate interactive and beautiful reports - Mycloudplace

  3. vorbelutr ioperbir

    It’s perfect time to make some plans for the future and it is time to be happy. I have read this post and if I could I want to suggest you some interesting things or tips. Perhaps you could write next articles referring to this article. I wish to read even more things about it!

  4. gralion torile

    Thanks a lot for sharing this with all of us you really recognize what you are talking about! Bookmarked. Please also seek advice from my web site =). We can have a hyperlink change agreement among us!

Leave a Comment

Your email address will not be published. Required fields are marked *