

{"id":123255,"date":"2023-11-02T17:34:58","date_gmt":"2023-11-02T12:04:58","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123255"},"modified":"2024-02-28T11:52:58","modified_gmt":"2024-02-28T06:22:58","slug":"python-program-on-numpy-array-creation","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-on-numpy-array-creation\/","title":{"rendered":"Python Program on NumPy Array Creation"},"content":{"rendered":"<p>In this article, we will explore a Python program that demonstrates various methods of the array module. The array module provides an efficient way to store and manipulate arrays in Python. The program covers essential methods like pop(), tolist(), append(), index(), remove(), count(), reverse(), and insert(). Understanding these methods is crucial for effective array manipulation in Python.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Basic Python Knowledge (Variables, Data Types, Syntax)<\/li>\n<li>Familiarity with the NumPy Library<\/li>\n<\/ul>\n<h3>Topic Explanation<\/h3>\n<p>In this program, an array of integers is created using the array module as a foundation for demonstrating its diverse functionalities. The demonstration covers methods like pop(), which removes the array&#8217;s last element, and tolist(), converting the array into a list. It also includes append(), which adds new elements to the array, enhancing its utility.<\/p>\n<p>The exploration extends to methods like index(), which identifies the position of an element, and remove(), which deletes an element&#8217;s first appearance in the array. For quantifying the frequency of elements, the count() method is employed. Additionally, the program showcases reverse(), for inverting the array&#8217;s order, and insert(), allowing for the placement of elements at specific locations, thus broadening the array&#8217;s adaptability.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Importing the NumPy library with an alias 'np' for ease of use\r\nimport numpy as np\r\n\r\n# Constructing an integer array 'myar' with a customized set of values\r\nmyar = np.array([5, 15, 25, 35, 10, 20, 30, 40], int)\r\n\r\n#Outputting the mean value of all elements in the array\r\nprint(\"Average:\", myar.mean())\r\n\r\n# Generating an array 'myar' with 4 values linearly spaced between 1 and 4\r\nmyar = np.linspace(1, 4, 4)\r\n\r\n# Creating a sequence 'myar' with values from 5 to 15 in ascending order\r\nmyar = np.arange(5, 16)\r\n\r\n# Outputting the created ascending sequence\r\nprint(\"Ascending sequence:\", myar)\r\n\r\n# Forming an array 'myar' of 5 elements, all set to 2 and of float type\r\nmyar = np.full(5, 2.0)\r\n\r\n# Displaying the array with all elements set to 2\r\nprint(\"Array of twos:\", myar)\r\n\r\n# Constructing an array 'myar' of 5 elements, all set to 3 and of float type\r\nmyar = np.full(5, 3.0)\r\n\r\n# Showing the array with all elements set to 3\r\nprint(\"Array of threes:\", myar)<\/pre>\n<div class=\"df-code-out\">\n<p><strong>Output:<\/strong><\/p>\n<p>Average: 22.5<br \/>\nAscending sequence: [ 5 6 7 8 9 10 11 12 13 14 15]<br \/>\nArray of twos: [2. 2. 2. 2. 2.]<br \/>\nArray of threes: [3. 3. 3. 3. 3.]<\/p>\n<h4>Code Explanation:<\/h4>\n<ul>\n<li>Imports numpy library and aliases it as np<\/li>\n<li>Creates an integer numpy array &#8216;myar&#8217; with elements: 10, 3, 40, 50, 20, 10, 6, 7 using np.array()<\/li>\n<li>Prints the sum of all elements in myar<\/li>\n<li>Creates a float numpy array &#8216;myar&#8217; with 5 evenly spaced elements from 0 to 10 using np.linspace()<\/li>\n<li>Creates a numpy array &#8216;myar&#8217; with values from 10 to 0 in steps of -1 (descending) using np.arange()<\/li>\n<li>Prints myar<\/li>\n<li>Creates a float numpy array &#8216;myar&#8217; with 10 zeroes using np.zeros()<\/li>\n<li>Prints myar<\/li>\n<li>Creates a float numpy array &#8216;myar&#8217; with 10 ones using np.ones()<\/li>\n<li>Prints myar<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>In summary, this Python program offers a hands-on introduction to creating arrays using the versatile NumPy library, a fundamental tool in Python&#8217;s data science ecosystem. By demonstrating various NumPy array creation functions and showcasing their practical applications, this program equips readers with essential skills for manipulating data efficiently.<\/p>\n<p>Proficiency in NumPy is highly valuable across diverse domains, such as scientific computing, data analysis, and machine learning, where it forms the foundation for advanced data manipulation and mathematical operations, empowering individuals to tackle complex real-world challenges effectively.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will explore a Python program that demonstrates various methods of the array module. The array module provides an efficient way to store and manipulate arrays in Python. The program covers&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[28684,28682,28683,10333,28626,28681],"class_list":["post-123255","post","type-post","status-publish","format-standard","hentry","category-python","tag-create-an-array-in-numpy","tag-numpy-array-creation","tag-numpy-array-creation-in-python","tag-python","tag-python-practical","tag-python-program-on-numpy-array-creation"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Program on NumPy Array Creation - DataFlair<\/title>\n<meta name=\"description\" content=\"Python program offers a hands-on introduction to creating arrays using the versatile NumPy library, a fundamental tool in Python&#039;s data science ecosystem.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-program-on-numpy-array-creation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program on NumPy Array Creation - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Python program offers a hands-on introduction to creating arrays using the versatile NumPy library, a fundamental tool in Python&#039;s data science ecosystem.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-on-numpy-array-creation\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-02T12:04:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-28T06:22:58+00:00\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Program on NumPy Array Creation - DataFlair","description":"Python program offers a hands-on introduction to creating arrays using the versatile NumPy library, a fundamental tool in Python's data science ecosystem.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-program-on-numpy-array-creation\/","og_locale":"en_US","og_type":"article","og_title":"Python Program on NumPy Array Creation - DataFlair","og_description":"Python program offers a hands-on introduction to creating arrays using the versatile NumPy library, a fundamental tool in Python's data science ecosystem.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-on-numpy-array-creation\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-02T12:04:58+00:00","article_modified_time":"2024-02-28T06:22:58+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-numpy-array-creation\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-numpy-array-creation\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program on NumPy Array Creation","datePublished":"2023-11-02T12:04:58+00:00","dateModified":"2024-02-28T06:22:58+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-numpy-array-creation\/"},"wordCount":387,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["create an array in numpy","numpy array creation","numpy array creation in python","Python","python practical","python program on numpy array creation"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-on-numpy-array-creation\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-numpy-array-creation\/","url":"https:\/\/data-flair.training\/blogs\/python-program-on-numpy-array-creation\/","name":"Python Program on NumPy Array Creation - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-02T12:04:58+00:00","dateModified":"2024-02-28T06:22:58+00:00","description":"Python program offers a hands-on introduction to creating arrays using the versatile NumPy library, a fundamental tool in Python's data science ecosystem.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-numpy-array-creation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-on-numpy-array-creation\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-numpy-array-creation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Program on NumPy Array Creation"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=123255"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123255\/revisions"}],"predecessor-version":[{"id":134140,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123255\/revisions\/134140"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}