

{"id":123622,"date":"2023-11-04T12:17:41","date_gmt":"2023-11-04T06:47:41","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123622"},"modified":"2024-02-28T12:07:55","modified_gmt":"2024-02-28T06:37:55","slug":"python-program-for-lower-upper-triangle-of-matrix","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-for-lower-upper-triangle-of-matrix\/","title":{"rendered":"Python Program for Lower &amp; Upper Triangle of Matrix"},"content":{"rendered":"<p>In this article, we will explore a Python program that focuses on extracting the lower and upper triangles, as well as the diagonal elements, from a given matrix. Matrices are fundamental in various scientific and computational applications, and understanding these triangular and diagonal components is essential for certain computations. The program demonstrates how to create a matrix with user input and then extracts its lower and upper triangles, along with the diagonal elements.<\/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 soliciting user input for the number of rows and columns, allowing dynamic matrix creation. It proceeds to collect runtime input for each matrix element, employing NumPy to transform the resulting array into a matrix. The program concludes this phase by displaying the original matrix, providing a practical demonstration of user-driven matrix generation.<\/p>\n<p>Subsequently, the program shifts its focus to matrix manipulation, specifically targeting the extraction and display of crucial matrix components. It showcases the extraction of both lower and upper triangles and highlights the presentation of diagonal elements within the matrix. This comprehensive exploration equips readers with essential skills for matrix manipulation in real-world scenarios, where precise data extraction and analysis are paramount.<\/p>\n<h4>Program<\/h4>\n<h4>Code:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Importing necessary libraries\r\nimport numpy as np  # NumPy library for array manipulation\r\nimport array as ar   # Array module for creating arrays\r\n\r\n# Taking input for the number of rows (m) and columns (n)\r\nm, n = [int(a) for a in input(\"Enter Values of Row and Column\").split()]\r\n\r\n# Calculating the total number of elements in the matrix\r\nx = m * n\r\n\r\n# Creating an empty array to store matrix elements\r\nmyar1 = ar.array('i', [])\r\n\r\n# Taking input for the matrix elements\r\nprint(\"Enter %d elements in matrix\" % x)\r\nfor i in range(x):\r\n    b = int(input())\r\n    myar1.append(b)\r\n\r\n# Reshaping the 1D array into a 2D matrix of shape (m, n)\r\nmyar2 = np.reshape(myar1, (m, n))\r\n\r\n# Printing the original matrix\r\nprint(\"Original Matrix:\")\r\nprint(myar2)\r\n\r\n# Printing the Lower Triangle of the Matrix\r\nprint(\"Lower Triangle of Matrix: \")\r\nfor r in range(m):\r\n    for c in range(n):\r\n        if r &gt;= c:\r\n            print(myar2[r][c], end=\" \")\r\n    print()\r\n\r\n# Printing the Upper Triangle of the Matrix\r\nprint(\"Upper Triangle of Matrix: \")\r\nfor r in range(m):\r\n    for c in range(n):\r\n        if r &lt;= c:\r\n            print(myar2[r][c], end=\" \")\r\n    print()\r\n\r\n# Printing the Diagonal of the Matrix\r\nprint(\"Diagonal of Matrix: \")\r\nfor r in range(m):\r\n    for c in range(n):\r\n        if r == c:\r\n            print(myar2[r][c], end=\" \")\r\n    print()<\/pre>\n<div class=\"df-code-out\">\n<p><strong>Output:<\/strong><\/p>\n<p>Enter Values of Row and Column2 2<br \/>\nEnter 4 elements in matrix<br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n[[1 2]<br \/>\n[3 4]]<br \/>\nLower Triangle of Matrix:<br \/>\n1<br \/>\n3 4<br \/>\nUpper Triangle of Matrix:<br \/>\n1 2<br \/>\n4<br \/>\nDiagonal of Matrix:<br \/>\n1<br \/>\n4<\/p>\n<\/div>\n<h4>Code Explanation:<\/h4>\n<ul>\n<li>Import numpy and array modules<\/li>\n<li>Take matrix dimension input (rows &amp; cols) from user and convert to ints<\/li>\n<li>Calculate total elements as rows*cols and store in x<\/li>\n<li>Create an empty integer array using array module<\/li>\n<li>Take x elements as input from user<\/li>\n<li>Append each element to the array<\/li>\n<li>Convert array to numpy matrix and reshape into input dimensions<br \/>\nPrint the full input matrix<br \/>\nPrint lower triangle by accessing array elements where row &gt;= col<br \/>\nPrint upper triangle by accessing elements where row &lt;= col<br \/>\nPrint diagonal by accessing elements where row == col<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>In summary, this Python program serves as a practical example of extracting the lower and upper triangles, as well as the diagonal elements, from a matrix. Understanding these components is crucial for various matrix-related computations in scientific and computational applications. Proficiency in working with these matrix elements empowers individuals to tackle complex tasks with precision and efficiency, making it a vital skill in the toolkit of Python programmers across diverse domains.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will explore a Python program that focuses on extracting the lower and upper triangles, as well as the diagonal elements, from a given matrix. Matrices are fundamental in various scientific&#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":[10333,28626,28702,28703],"class_list":["post-123622","post","type-post","status-publish","format-standard","hentry","category-python","tag-python","tag-python-practical","tag-python-program-for-upper-and-lower-triangle-in-matrix","tag-upper-and-lower-triangle-in-matrix-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 for Lower &amp; Upper Triangle of Matrix - DataFlair<\/title>\n<meta name=\"description\" content=\"Python program serves as a practical example of extracting the lower and upper triangles, as well as the diagonal elements, from a matrix.\" \/>\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-for-lower-upper-triangle-of-matrix\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program for Lower &amp; Upper Triangle of Matrix - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Python program serves as a practical example of extracting the lower and upper triangles, as well as the diagonal elements, from a matrix.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-for-lower-upper-triangle-of-matrix\/\" \/>\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-04T06:47:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-28T06:37:55+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 for Lower &amp; Upper Triangle of Matrix - DataFlair","description":"Python program serves as a practical example of extracting the lower and upper triangles, as well as the diagonal elements, from a matrix.","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-for-lower-upper-triangle-of-matrix\/","og_locale":"en_US","og_type":"article","og_title":"Python Program for Lower &amp; Upper Triangle of Matrix - DataFlair","og_description":"Python program serves as a practical example of extracting the lower and upper triangles, as well as the diagonal elements, from a matrix.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-for-lower-upper-triangle-of-matrix\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-04T06:47:41+00:00","article_modified_time":"2024-02-28T06:37:55+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-for-lower-upper-triangle-of-matrix\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-for-lower-upper-triangle-of-matrix\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program for Lower &amp; Upper Triangle of Matrix","datePublished":"2023-11-04T06:47:41+00:00","dateModified":"2024-02-28T06:37:55+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-for-lower-upper-triangle-of-matrix\/"},"wordCount":411,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["Python","python practical","python program for upper and lower triangle in matrix","upper and lower triangle in matrix in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-for-lower-upper-triangle-of-matrix\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-for-lower-upper-triangle-of-matrix\/","url":"https:\/\/data-flair.training\/blogs\/python-program-for-lower-upper-triangle-of-matrix\/","name":"Python Program for Lower &amp; Upper Triangle of Matrix - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-04T06:47:41+00:00","dateModified":"2024-02-28T06:37:55+00:00","description":"Python program serves as a practical example of extracting the lower and upper triangles, as well as the diagonal elements, from a matrix.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-for-lower-upper-triangle-of-matrix\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-for-lower-upper-triangle-of-matrix\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-for-lower-upper-triangle-of-matrix\/#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 for Lower &amp; Upper Triangle of Matrix"}]},{"@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\/123622","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=123622"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123622\/revisions"}],"predecessor-version":[{"id":134151,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123622\/revisions\/134151"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}