

{"id":123512,"date":"2023-11-04T11:27:31","date_gmt":"2023-11-04T05:57:31","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123512"},"modified":"2024-02-28T11:59:28","modified_gmt":"2024-02-28T06:29:28","slug":"python-program-on-view-vs-copy","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-on-view-vs-copy\/","title":{"rendered":"Python Program on View vs Copy"},"content":{"rendered":"<p>In this article, we will explore a Python program that delves into the concepts of copying, referencing, and viewing arrays using the NumPy library. Understanding these concepts is crucial when working with arrays, as it directly impacts how changes to one array affect others. The program showcases different methods of creating copies, views, and references to NumPy arrays, providing insights into their behaviour and memory management.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Fundamental Python Knowledge (Variables, Data Types, Syntax)<\/li>\n<li>Basic Familiarity with the NumPy Library (Numerical Computing, Basic Array Operations)<\/li>\n<\/ul>\n<h3>Topic Explanation<\/h3>\n<p>This program initiates by importing the NumPy library as &#8216;np&#8217; and generates an array using the &#8216;arange()&#8217; function. It embarks on an exploration of multiple methods for copying arrays, encompassing &#8216;copy()&#8217;, &#8216;view()&#8217;, and straightforward reference assignment.<\/p>\n<p>Within this exploration, the article delves into the distinctions between these copying techniques and elucidates how modifications made to one array can propagate to others. Readers gain a profound understanding of array copying mechanisms and their implications, enabling them to make informed choices when manipulating data using NumPy arrays.<\/p>\n<h4>Code:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\n\r\n# Create an array using numpy.arange with values from 0 to 9\r\nmyar = np.arange(0,20, 3)\r\n\r\n# Use numpy.logical_not to compute the element-wise logical NOT of the array\r\nnewar = np.logical_not(myar)\r\n\r\n# Print the result of the logical NOT operation\r\nprint(newar)\r\n\r\n# Create a copy of the original array using the copy() method\r\nar = myar.copy()\r\n\r\n# Print the IDs of the original and copied arrays\r\nprint(\"Id of myarray \", id(myar))\r\nprint(\"Id of copy  \", id(ar))\r\n\r\n# Print the elements of the original and copied arrays\r\nprint(\"Elements of MYarray :\", myar)\r\nprint(\"Elements of copied array :\", ar)\r\n\r\n# Modify an element in the original array\r\nmyar[2] = 500\r\n\r\n# Print the elements of the arrays after the modification\r\nprint(\"-------------After Change----------\")\r\nprint(\"Elements of Myarray :\", myar)\r\nprint(\"Elements of copied array :\", ar)\r\n\r\n# Creates a view `ar` on `myar`\r\nar = myar.view()\r\n\r\n# Print the IDs of the original array and its view\r\nprint(\"Id of myarray \", id(myar))\r\nprint(\"Id of view  \", id(ar))\r\n\r\n# Print the elements of the original array and its view\r\nprint(\"Elements of MYarray :\", myar)\r\nprint(\"Elements of View :\", ar)\r\n\r\n# Modify an element in the original array\r\nmyar[2] = 500\r\n\r\n# Print the elements of the arrays after the modification\r\nprint(\"-------------After Change----------\")\r\nprint(\"Elements of MYarray :\", myar)\r\nprint(\"Elements of View :\", ar)\r\n\r\n# Assign the reference of the original array to a new variable\r\nar = myar\r\n\r\n# Modify elements in both the original and referenced arrays\r\nar[3] = 100\r\nmyar[6] = 500\r\n\r\n# Print the elements of the arrays after the modification\r\nprint(\"-------------After Change----------\")\r\nprint(\"My Array(myar): \", myar)\r\nprint(\"Reference(Ar): \", ar)\r\n\r\n# Print the IDs of the original array and the reference\r\nprint(\"Id of myarray \", id(myar))\r\nprint(\"Id of reference \", id(ar))<\/pre>\n<div class=\"df-code-out\">\n<p><strong>Output:<\/strong><\/p>\n<p>[ True False False False False False False]<br \/>\nId of myarray 140674929030800<br \/>\nId of copy 140674572829488<br \/>\nElements of MYarray : [ 0 3 6 9 12 15 18]<br \/>\nElements of copied array : [ 0 3 6 9 12 15 18]<br \/>\n&#8212;&#8212;&#8212;&#8212;-After Change&#8212;&#8212;&#8212;-<br \/>\nElements of Myarray : [ 0 3 500 9 12 15 18]<br \/>\nElements of copied array : [ 0 3 6 9 12 15 18]<br \/>\nId of myarray 140674929030800<br \/>\nId of view 140674572829680<br \/>\nElements of MYarray : [ 0 3 500 9 12 15 18]<br \/>\nElements of View : [ 0 3 500 9 12 15 18]<br \/>\n&#8212;&#8212;&#8212;&#8212;-After Change&#8212;&#8212;&#8212;-<br \/>\nElements of MYarray : [ 0 3 500 9 12 15 18]<br \/>\nElements of View : [ 0 3 500 9 12 15 18]<br \/>\n&#8212;&#8212;&#8212;&#8212;-After Change&#8212;&#8212;&#8212;-<br \/>\nMy Array(myar): [ 0 3 500 100 12 15 500]<br \/>\nReference(Ar): [ 0 3 500 100 12 15 500]<br \/>\nId of myarray 140674929030800<br \/>\nId of reference 140674929030800<\/p>\n<h4>Code Explanation:<\/h4>\n<ul>\n<li>Import numpy library and assign alias np<\/li>\n<li>Create ndarray myar with integers<\/li>\n<li>Create boolean ndarray newar that inverts truth value of myar<\/li>\n<li>Print newar<\/li>\n<li>Create ar as a copy of myar<\/li>\n<li>Print ids showing myar and ar have different ids<\/li>\n<li>Print elements showing myar and ar have same contents<\/li>\n<li>Modify myar by changing value at index 2 to 500<\/li>\n<li>Print updated myar and unchanged ar, showing copy was not affected<\/li>\n<li>Create ar as a view into myar<\/li>\n<li>Print ids showing view shares data with myar<\/li>\n<li>Print elements showing current identical contents<\/li>\n<li>Modify myar at index 2 to 500<\/li>\n<li>Print updated myar and also updated ar, showing changes reflected since ar views same data<\/li>\n<li>Make ar a reference to myar by assigning myar to ar<\/li>\n<li>Print ar showing it references same array contents<\/li>\n<li>Modify index 3 and 6 of ar and myar<\/li>\n<li>Print updated myar and ar, showing changes reflected in both since ar references myar<\/li>\n<li>Print ids confirming myar and ar reference same array<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>In summary, this Python program serves as a valuable resource for readers aiming to master the intricacies of array copying, referencing, and viewing within the NumPy framework. By delving into the subtle nuances of how alterations in arrays affect copies, views, and references, it equips individuals with a profound comprehension of array behavior in Python.<\/p>\n<p>This knowledge is not only pivotal for data manipulation but also crucial for ensuring data integrity and optimal performance in various computational tasks, making it an essential skill for both novice and experienced Python programmers alike.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will explore a Python program that delves into the concepts of copying, referencing, and viewing arrays using the NumPy library. Understanding these concepts is crucial when working with arrays, as&#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":[28694,28693,10333,28626,28692,28691],"class_list":["post-123512","post","type-post","status-publish","format-standard","hentry","category-python","tag-comparison-between-view-and-copy","tag-difference-between-view-vs-copy","tag-python","tag-python-practical","tag-python-program-on-view-vs-copy","tag-view-vs-copy-in-python"],"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 View vs Copy - DataFlair<\/title>\n<meta name=\"description\" content=\"Python program serves as a valuable resource for readers aiming to master the intricacies of array copying, referencing, and viewing within the NumPy framework.\" \/>\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-view-vs-copy\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program on View vs Copy - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Python program serves as a valuable resource for readers aiming to master the intricacies of array copying, referencing, and viewing within the NumPy framework.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-on-view-vs-copy\/\" \/>\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-04T05:57:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-28T06:29:28+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=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Program on View vs Copy - DataFlair","description":"Python program serves as a valuable resource for readers aiming to master the intricacies of array copying, referencing, and viewing within the NumPy framework.","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-view-vs-copy\/","og_locale":"en_US","og_type":"article","og_title":"Python Program on View vs Copy - DataFlair","og_description":"Python program serves as a valuable resource for readers aiming to master the intricacies of array copying, referencing, and viewing within the NumPy framework.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-on-view-vs-copy\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-04T05:57:31+00:00","article_modified_time":"2024-02-28T06:29:28+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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-view-vs-copy\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-view-vs-copy\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program on View vs Copy","datePublished":"2023-11-04T05:57:31+00:00","dateModified":"2024-02-28T06:29:28+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-view-vs-copy\/"},"wordCount":494,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["comparison between view and copy","difference between view vs copy","Python","python practical","python program on view vs copy","view vs copy in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-on-view-vs-copy\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-view-vs-copy\/","url":"https:\/\/data-flair.training\/blogs\/python-program-on-view-vs-copy\/","name":"Python Program on View vs Copy - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-04T05:57:31+00:00","dateModified":"2024-02-28T06:29:28+00:00","description":"Python program serves as a valuable resource for readers aiming to master the intricacies of array copying, referencing, and viewing within the NumPy framework.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-view-vs-copy\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-on-view-vs-copy\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-view-vs-copy\/#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 View vs Copy"}]},{"@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\/123512","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=123512"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123512\/revisions"}],"predecessor-version":[{"id":134145,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123512\/revisions\/134145"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}