Categories: Softwares

How to create an array of zeros in Python?

[ad_1]

In this article, we will discuss how to create an array of zeros in Python. In array items are stored at contiguous memory locations and here we will try to add only Zeros into the array with different methods.

Here we will cover different approaches to creating a zero’s element array. The different approaches that we will cover in this article are:

Example 1: Using simple multiplication 

In this example, we are multiplying the array of zero to 9. In return, we will get an array with 9 elements of 0’s.

Python3

arr0 = [0] * 9

print (arr0)

Output:

[0, 0, 0, 0, 0, 0, 0, 0, 0]

Example 2: Using a loop

In this example, we are creating a 0’s array using a loop of range from 0 to 10.

Python3

arr1 = []

for i in range(0,10):

    arr1.append(0)

      

print(arr1)

Output:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Example 3: Using List comprehension

In this example, we are creating a 2-D array using a list comprehension in python to create 0’s of 5 rows and 10 columns.

Python3

arr2 = [[0 for col in range(5)] for row in range(10)]

print(arr2)

Output:

[[0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0]]

Example 4: Using the In-Build method numpy.zeros() method

In this example, we are creating a NumPy array with zeros as the numpy.zeros() function is used which returns a new array of given shape and type, with zeros. 

Python3

import numpy as np

  

arr3 = np.zeros((2, 2, 3), dtype=[('x', 'int'), ('y', 'float')])

print(arr3)

print(arr3.dtype)

Output:

In the output, i4 specifies 4 bytes of integer data type, whereas f8 specifies 8 bytes of float data type.

[[[(0, 0.) (0, 0.) (0, 0.)]
  [(0, 0.) (0, 0.) (0, 0.)]]

 [[(0, 0.) (0, 0.) (0, 0.)]
  [(0, 0.) (0, 0.) (0, 0.)]]]
[('x', '<i4'), ('y', '<f8')]

[ad_2]

Source link

Taylor Swift

Taylor Swift is a pro-level blogger with years of experience in writing for multiple industries. She has extensive knowledge in Technology, healthcare, business, sports, fashion, and many other popular niches.

Recent Posts

How To Choose the Right Sales Automation Solution

In the relentless pace of today's business landscape, where time is money and efficiency is… Read More

11 hours ago

Benefits of Group Dog Training: Why Socializing Your Pup Matters

When it comes to raising a well-rounded and well-behaved canine companion, socialization plays a crucial… Read More

1 day ago

All Characters Who Trained Luke Skywalker

Luke Skywalker, the legendary Jedi Knight who ignited a new hope in the galaxy, didn't… Read More

2 days ago

Top 8 Tips to Find the Best Dental Clinic in Dubai [2024]

When it comes to your dental health, choosing the right clinic is crucial for receiving… Read More

1 week ago

Essentials for Effective Multifamily Marketing

In the dynamic world of real estate, multifamily properties present unique marketing challenges and opportunities.… Read More

2 weeks ago

How Couples Salsa Classes Can Strengthen Your Relationship

In the bustling world we inhabit, finding meaningful ways to connect with our partners can… Read More

3 weeks ago