Thursday, September 10, 2020

creating arrays in python using zeros() and ones() function

 creating arrays in python using zeros() and ones() function


 

                          creating arrays in python using

                     zeros() and ones() function

           ===============================

 

We can use the zeros() function to create an array with

zeros . The ones() function is useful to create an array

with all 1's.

zeros(n , datatype)

ones(n , datatype)

where , 'n' represents the number of elements .

 

If we do not specify the 'datatype' , then the default

datatype used by numpy is 'float'.

 

Examples

  • >>> zeros(5)

This will create an array with 5 elements and all are

zeros and in floating datatype. This is represented as :

[ 0.0 , 0.0 , 0.0 , 0.0 , 0.0 ].

 

 

  • If we want this array in integer format , we can use 'int'

as datatype .

>>> zeros(10,int)

This will create an array as :

[ 0 0 0 0 0 0 0 0 0 0]

 

  • If we want to use the ones() function , it will create an

array with all elements 1

>>> ones(5,float)

This will create an array with 5 float elements and all

are 1's :

[ 1.0 1.0 1.0 1.0 1.0]

 

 

 


No comments:

Post a Comment

Durga Puja in Odisha

 Durga Puja in Odisha