← Back to Python Course | Chapter 13: Data Science & Web | Lesson 1 of 14

Python NumPy परिचय

NumPy numbers की बड़ी grids के साथ बहुत तेज़ी से काम करने का toolkit है, जैसे पूरी rows एक साथ संभालने वाला super calculator। Scientists और data वाले लोग इसे हर समय इस्तेमाल करते हैं।
Syntax
python
import numpy as np

array = np.array([item1, item2, item3])

NumPy क्या है?

NumPy Python में numerical computing के लिए foundational library है, जो ndarray type देती है -- एक fixed-type, multi-dimensional array जो numeric काम के लिए nested Python lists से काफी तेज़ और memory-efficient है, क्योंकि इसके elements अलग-अलग Python objects की बजाय memory में लगातार (contiguously) store होते हैं।

उदाहरण: What is NumPy?

python
import numpy as np
arr = np.array([1, 2, 3])  # fixed-type, contiguous ndarray
print(type(arr))

NumPy Array के Attributes

हर NumPy array के साथ built-in metadata आता है: .shape इसके dimensions एक tuple के रूप में देता है, .ndim dimensions की संख्या देता है, और .size total element count देता है।

इन attributes को पढ़ना यह जल्दी confirm करने का तरीका है कि किसी calculation को चलाने से पहले array की structure उसकी अपेक्षा से मेल खाती है।

उदाहरण: NumPy Array Attributes

python
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.shape, arr.ndim, arr.size)  # dimensions, number of dimensions, total elements

Basic Mathematical Operations

NumPy पूरे array पर arithmetic को एक single vectorized operation में element-by-element apply करता है (a + b हर मेल खाते जोड़े के elements को एक साथ जोड़ देता है), जो अंदर से compiled C code में implement है।

यही कारण है कि बड़े arrays पर NumPy math किसी equivalent Python for loop से कई गुना तेज़ चलता है।

उदाहरण: Basic Mathematical Operations

python
import numpy as np
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
print(a + b)  # element-wise addition, vectorized in one call

Array के Data Types

Python की list के विपरीत, जो आज़ादी से types मिला सकती है, हर NumPy array का एक fixed dtype (जैसे int32 या float64) होता है जो उसके सभी elements साझा करते हैं, जो या तो अपने-आप चुना जाता है या dtype argument से explicit रूप से।

Precision इजाज़त दे तो एक छोटा dtype चुनना array के memory footprint को काफी कम कर सकता है।

उदाहरण: Array Data Types

python
import numpy as np
arr = np.array([1, 2, 3], dtype="float64")  # explicit fixed dtype for every element
print(arr.dtype)

Summary Statistics

NumPy arrays के साथ built-in aggregate methods आते हैं -- .sum(), .mean(), .max(), .min(), और अन्य -- जो पूरे array पर (या multi-dimensional data के लिए किसी चुने axis पर) Python में manually loop चलाकर running total जोड़ने से कहीं तेज़ गणना करते हैं।

उदाहरण: Summary Statistics

python
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr.sum(), arr.mean(), arr.max())  # built-in aggregate methods
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.