The Blog

Consider a module to be the same as a code library. Considers a file containing a set of functions that you want to include in the program.

For, creating a module just save the code with extension .py:-

Example :- Save this code in a file named

def display(name):
print(“Hello, ” + name)

How to use module in program

We can use the module by using the import statement:-

Example : – import the module named module_demo and call the function define inside it.

import module_demo

module_demo.diaplay(“Python Programming”)

Array – How to create array and assigning value

Arrays are used to store multiple values in one single variable:

fruits = [“Orange”,”Mango”,”Apple”]

Advantages of Array

Array is a special variable, which can have more than one value at a time or we can say multiple value in single variable.


If you have a list of items (for an example fruit names), storing the fruits in single variables could look like this:

fruits1 = “Orange”

fruits1 = “Mango”

fruits1 = “Apple”

Access the Elements of an Array

We access the element of an array element by its index number.

X = fruits[0]

len() function is use to check the length of an array.

Array Indexing and Slicing

For array indexing python use library called NumPy. On other side slice, a copy of the array is returned but in index array a copy of the original array is returned.

NumPy arrays can be indexed with other arrays or any other sequence with the exception of tuples. The last element is indexed by -1 second last by -2 and so on.

Example :-

import numpy as np

# create a sequence of integers from 10 to 1 with a step of -1

a = np.arange(10,1,-1)

print(“\n A sequence with negative step : ”,a)

# Indexes are specified by method np.array

newarr = a[np.array([3, 1, 2 ])]

print(“\n Elements at these indices are:\n”,newarr)

Array Slicing

Contents of ndarray object can be accessed and modified by indexing or slicing, just like Python’s in-built container objects.

As mentioned earlier, items in array object follows zero-based index. Three different types of indexing methods are available − field access, basic slicing and advanced indexing.

Basic slicing is an extension of Python’s basic concept of slicing to n dimensions. A Python slice object is constructed by giving start, stop, and step parameters to the built-in slice function. This slice object is passed to the array to extract a part of array.

Example : –

importnumpyas np

a =np.arange(10)

s =slice(2,7,2)

print a[s]

Multiple values stored within an array can be accessed at the same time with array slicing. For slicing of an array use colon operator :

when calling the index. The general form is:

<slice> = <array>[start:stop]

Where <slice> is the slice or section of the array object <array>. The index of the slice is specified in [start:stop]. Must Remember that Python counting starts at 0 and ends at n-1. The index [0:3] denoted the first three values out of an array. The index [2:4] tells the third and fourth values out of an array.

An example of slicing the first two elements out of an array is below.






Output : –  [2 4 6]