{"id":3064,"date":"2023-05-23T20:51:17","date_gmt":"2023-05-23T15:21:17","guid":{"rendered":"https:\/\/ciphertrick.com\/?p=3064"},"modified":"2023-05-23T20:51:17","modified_gmt":"2023-05-23T15:21:17","slug":"permutation-and-combination-in-python","status":"publish","type":"post","link":"https:\/\/thrivemyway.com\/permutation-and-combination-in-python\/","title":{"rendered":"Permutation and Combination in Python"},"content":{"rendered":"\n<p>Permutation and combination are the mathematical principles that help in counting the number of possible outcomes of an event. These principles are widely used in various fields like statistics, mathematics, computer science, etc. In this article, we will discuss permutation and combination in Python, their formulas, and how to implement them in Python programming language.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/thrivemyway.com\/permutation-and-combination-in-python\/#Permutation\" >Permutation:<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/thrivemyway.com\/permutation-and-combination-in-python\/#Formula_for_permutation\" >Formula for permutation:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/thrivemyway.com\/permutation-and-combination-in-python\/#Example_of_permutation\" >Example of permutation:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/thrivemyway.com\/permutation-and-combination-in-python\/#Python_program_for_permutation\" >Python program for permutation:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/thrivemyway.com\/permutation-and-combination-in-python\/#Combination\" >Combination:<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/thrivemyway.com\/permutation-and-combination-in-python\/#Formula_for_combination\" >Formula for combination:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/thrivemyway.com\/permutation-and-combination-in-python\/#Example_of_combination\" >Example of combination:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/thrivemyway.com\/permutation-and-combination-in-python\/#Python_program_for_combination\" >Python program for combination:<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Permutation\"><\/span>Permutation:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Permutation is a way of selecting and arranging objects from a set of objects where the order of selection matters. It is expressed as nP r, where n represents the total number of objects, and r denotes the number of objects that are selected from n objects.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Formula_for_permutation\"><\/span>Formula for permutation:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>nP r = n! \/ (n-r)!<\/strong><\/p>\n\n\n\n<p>where,<\/p>\n\n\n\n<p>n! represents factorial of n, which is the product of all positive integers from 1 to n, and 0! = 1.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_of_permutation\"><\/span>Example of permutation:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Suppose we have five balls numbered 1,2,3,4,5. How many different ways can we select and arrange two balls from them?<\/p>\n\n\n\n<p>Here, n = 5 and r = 2.<\/p>\n\n\n\n<p>To calculate the number of permutations, we can use the formula,<\/p>\n\n\n\n<p>nP r = n! \/ (n-r)!<\/p>\n\n\n\n<p>nP r = 5! \/ (5-2)!<\/p>\n\n\n\n<p>nP r = 5! \/ 3!<\/p>\n\n\n\n<p>nP r = (5*4*3*2*1) \/ (3*2*1)<\/p>\n\n\n\n<p>nP r = 20<\/p>\n\n\n\n<p>Therefore, there are 20 different ways of selecting and arranging two balls from five balls.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python_program_for_permutation\"><\/span>Python program for permutation:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We can implement the formula for permutation using Python programming language. Here is the Python code for permutation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">import math\nn = int(input(\"Enter total number of objects n: \"))\nr = int(input(\"Enter number of objects selected r: \"))\n# Formula for permutation\npermutation = math.factorial(n) \/ math.factorial(n-r)\n\nprint(\"Number of permutations: \", permutation)<\/code><\/pre>\n\n\n\n<p>In this code, we have imported the math module, which provides access to the mathematical functions. We have taken inputs for n and r from the user, and then we have applied the formula for permutation formula to calculate the number of permutations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Combination\"><\/span>Combination:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Combination is a way of selecting objects from a set of objects where the order of selection does not matter. It is expressed as nC r, where n represents the total number of objects, and r denotes the number of objects that are selected from n objects.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Formula_for_combination\"><\/span>Formula for combination:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>nCr = n! \/ (r! * (n-r)!)<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_of_combination\"><\/span>Example of combination:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Suppose we have five balls numbered 1,2,3,4,5. How many different ways can we select two balls from them?<\/p>\n\n\n\n<p>Here, n = 5 and r = 2.<\/p>\n\n\n\n<p>To calculate the number of combinations, we can use the formula,<\/p>\n\n\n\n<p>nCr = n! \/ (r! * (n-r)!)<\/p>\n\n\n\n<p>nCr = 5! \/ (2! * (5-2)!)<\/p>\n\n\n\n<p>nCr = 5! \/ (2! * 3!)<\/p>\n\n\n\n<p>nCr = (5*4*3*2*1) \/ (2*1 * 3*2*1)<\/p>\n\n\n\n<p>nCr = 10<\/p>\n\n\n\n<p>Therefore, there are 10 different ways of selecting two balls from five balls.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python_program_for_combination\"><\/span>Python program for combination:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We can implement the formula for combination using Python programming language. Here is the Python code for combination.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">import math\nn = int(input(\"Enter total number of objects n: \"))\nr = int(input(\"Enter number of objects selected r: \"))\n# Formula for combination\ncombination = math.factorial(n) \/ (math.factorial(r) * math.factorial(n-r))\n\nprint(\"Number of combinations: \", combination)<\/code><\/pre>\n\n\n\n<p>In this code, we have imported the math module, which provides access to the mathematical functions. We have taken inputs for n and r from the user, and then we have applied the formula for combination formula to calculate the number of combinations.<\/p>\n\n\n\n<p><strong>FAQs:<\/strong><\/p>\n\n\n\n<p><strong><em>Q1. What is the difference between permutation and combination?<\/em><\/strong><\/p>\n\n\n\n<p>Ans. Permutation is a way of selecting and arranging objects from a set of objects where the order of selection matters. Whereas, combination is a way of selecting objects from a set of objects where the order of selection does not matter.<\/p>\n\n\n\n<p><strong><em>Q2. What is the formula for permutation?<\/em><\/strong><\/p>\n\n\n\n<p>Ans. The formula for permutation is nP r = n! \/ (n-r)!, where n represents the total number of objects, and r denotes the number of objects that are selected from n objects.<\/p>\n\n\n\n<p><strong><em>Q3. What is the formula for combination?<\/em><\/strong><\/p>\n\n\n\n<p>Ans. The formula for combination is nCr = n! \/ (r! * (n-r)!), where n represents the total number of objects, and r denotes the number of objects that are selected from n objects.<\/p>\n\n\n\n<p><strong><em>Q4. How do we implement permutation and combination in Python?<\/em><\/strong><\/p>\n\n\n\n<p>Ans. We can implement permutation and combination using Python programming language by applying the respective formulas using math module in Python.<\/p>\n\n\n\n<p><strong>Conclusion:<\/strong><\/p>\n\n\n\n<p>In this article, we have discussed permutation and combination in Python, their formulas, and how to implement them in Python programming language. These principles are important in various fields like statistics, mathematics, and computer science. The formulas for permutation and combination can be easily implemented in Python programming language to calculate the number of possible outcomes of an event.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Permutation and combination are the mathematical principles that help in counting the number of possible outcomes of an event. These [&hellip;]<\/p>\n","protected":false},"author":3000066,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3001965,3001966],"tags":[],"class_list":["post-3064","post","type-post","status-publish","format-standard","hentry","category-programming-languages","category-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/posts\/3064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/users\/3000066"}],"replies":[{"embeddable":true,"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/comments?post=3064"}],"version-history":[{"count":0,"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/posts\/3064\/revisions"}],"wp:attachment":[{"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/media?parent=3064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/categories?post=3064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/tags?post=3064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}