Python Tutorial

Introduction Python Features Python Applications System requirements for Python Python Installation Python Basics Python Variables Python Data Types Python IDE Python Keywords Python Operators Python Comments Python Pass Statement

Python Conditional Statements

Python if Statement Python elif Statement Python If-else statement Python Switch Case

Python Loops

Python for loop Python while loop Python Break Statement Python Continue Statement Python Goto Statement

Python Arrays

Python Array Python Matrix

Python Strings

Python Strings Python Regex

Python Built-in Data Structure

Python Lists Python Tuples Python Lists vs Tuples Python Dictionary Python Sets

Python Functions

Python Function Python min() function Python max() function Python User-define Functions Python Built-in Functions Anonymous/Lambda Function in Python

Python File Handling

Python File Handling Python Read CSV Python Write CSV Python Read Excel Python Write Excel Python Read Text File Python Write Text File Read JSON File in Python

Python Exception Handling

Python Exception Handling Python Errors and exceptions Python Assert

Python OOPs Concept

OOPs Concepts in Python Classes & Objects in Python Inheritance in Python Polymorphism in Python Python Encapsulation Python Constructor Static Variables in Python Abstraction in Python

Python Iterators

Iterators in Python Yield Statement In Python

Python Generators

Python Generator

Python Decorators

Python Decorator

Python Functions and Methods

Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods

Python Modules

Python Modules Python Datetime Module Python Calendar Module  

Python MySQL

Python MySQL Python MySQL Update Operation Python MySQL Delete Operation

Python MongoDB

Python MongoDB

Python Data Structure Implementation

Python Stack Python Queue Python Hash Table Python Graph

Python Advance Topics

Speech Recognition in Python Face Recognition in Python Python Rest API Python Command Line Arguments Python JSON Python Virtual Environment Type Casting in Python Collections in python Python Enumerate Python Debugger Python DefaultDict

Misc

Python PPTX Python Pickle Python Seaborn Python Coroutine Python EOL Python Infinity Python math.cos and math.acos function Python Project Ideas Based On Django Reverse a String in Python Reverse a Number in Python Python Word Tokenizer Python Trigonometric Functions Python try catch exception GUI Calculator in Python Implementing geometric shapes into the game in python Installing Packages in Python Python Try Except Python Sending Email Socket Programming in Python Python CGI Programming Python Data Structures Python abstract class Python Compiler Python K-Means Clustering List Comprehension in Python3 NSE Tools In Python Operator Module In Python Palindrome In Python Permutations in Python Pillow Python introduction and setup Python Functionalities of Pillow Module Python Argmin Python whois Python JSON Schema Python lock Return Statement In Python Reverse a sentence In Python tell() function in Python Why learn Python? Write Dictionary to CSV in Python Write a String in Python Binary Search Visualization using Pygame in Python Latest Project Ideas using Python 2022 Closest Pair of Points in Python ComboBox in Python Python vs R Python Ternary Operators Self in Python Python vs Java Python Modulo Python Packages Python Syntax Python Uses Python Logical Operators Python Multiprocessing Python History Difference between Input() and raw_input() functions in Python Conditional Statements in python Confusion Matrix Visualization Python Python Algorithms Python Modules List Difference between Python 2 and Python 3 Is Python Case Sensitive Method Overloading in Python Python Arithmetic Operators Design patterns in python Assignment Operators in Python Is Python Object Oriented Programming language Division in Python Python exit commands Continue And Pass Statements In Python Colors In Python Convert String Into Int In Python Convert String To Binary In Python Convert Uppercase To Lowercase In Python Convert XML To JSON In Python Converting Set To List In Python Covariance In Python CSV Module In Python Decision Tree In Python Difference Between Yield And Return In Python Dynamic Typing In Python Abstract design pattern in python Builder design pattern in python Prototype design pattern in Python Creational design patterns in Python

How to

How to convert integer to float in Python How to reverse a string in Python How to take input in Python How to install Python in Windows How to install Python in Ubuntu How to install PIP in Python How to call a function in Python How to download Python How to comment multiple lines in Python How to create a file in Python How to create a list in Python How to declare array in Python How to clear screen in Python How to convert string to list in Python How to take multiple inputs in Python How to write a program in Python How to compare two strings in Python How to create a dictionary in Python How to create an array in Python How to update Python How to compare two lists in Python How to concatenate two strings in Python How to print pattern in Python How to check data type in python How to slice a list in python How to implement classifiers in Python How To Print Colored Text in Python How to develop a game in python How to print in same line in python How to create a class in python How to find square root in python How to import numy in python How to import pandas in python How to uninstall python How to upgrade PIP in python How to append a string in python How to open a file in python

Sorting

Python Sort List Sort Dictionary in Python Python sort() function Python Bubble Sort

Programs

Factorial Program in Python Prime Number Program in Python Fibonacci Series Program in Python Leap Year Program in Python Palindrome Program in Python Check Palindrome In Python Calculator Program in Python Armstrong Number Program in Python Python Program to add two numbers Anagram Program in Python Even Odd Program in Python GCD Program in Python Python Exit Program Python Program to check Leap Year Operator Overloading in Python Pointers in Python Python Not Equal Operator Raise Exception in Python Salary of Python Developers in India What is a Script in Python Singleton design pattern in python

Python Combination

Python Combination

Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python and how we can use them in our Python program.

Python Combination

In this tutorial, we will learn that how we can calculate combinations from a given data with the help of Python program. For this, we are going to use the inbuilt packages to find the combinations of the given data in the program.

In Python, we have libraries such as itertools which have the in-built features to calculate the combinations of the data or number.

Importing the pre required library (itertools library):

To calculate the combinations of a number or data with the help of our Python program, we have to firstly import the pre required library i.e., itertools library in the program. To import this library in our Python program, we have to use the following command:

> - import itertools

The above command will import the itertools library and all the inbuilt features in the program. It will also form a pathway through which we can use all the functions of itertools in the Python program.

We have to remember the following points before importing the itertools library and using its functions in the Python program:

  • Firstly, we have to create a list of the sequence of the given data as the input of the program.
  • After that, the list we have created will return the output of the program in the form of Python tuples that consists of required number of permutations and combinations.
  • We can also check and set the length of permutations and combinations required in the output of the program.

Calculating Python Combinations of Different Data

As we all know that, combination is the set of data created with the help of given data where a specific order doesn't matter. In Python itertools library, we have provided with a combination() function. With the help of this combination() function, we can calculate all the different possible combinations of the given data.

In the Python program, we will use the different itertools functions to calculate the different combinations of data provided to us. Following are the different ways or method by which we can calculate the combinations of any data provided to us:

1. Combinations of a given string

2. Combinations with the help of replacement

3. Combinations of a given numeric set

Let's learn about each method with the help of some Python programs and understand how we can use them to calculate combinations of different type of data provided to us.

1. Combinations of a given string:

In Python, with the help of combination() function provided to us, we can calculate the different combinations of a string type data provided to us as input. Let's look at some examples of Python program and understand this in a better way.

Example 1:

 # importing the itertools library
 import itertools
 # define a sequence
 sequen = "PythonDeveleopers"
 # calculate possible combinations with letter 2
 combi = itertools.combinations(sequen, 2)
 # printing all combinations in output using for loop
 for m in combi: 
     print(m) 

Output of the program:

 ('P', 'y')
 ('P', 't')
 ('P', 'h')
 ('P', 'o')
 ('P', 'n')
 ('P', 'D')
 ('P', 'e')
 ('P', 'v')
 ('P', 'e')
 ('P', 'l')
 ('P', 'e')
 ('P', 'o')
 ('P', 'p')
 ('P', 'e')
 ('P', 'r')
 ('P', 's')
 ('y', 't')
 ('y', 'h')
 ('y', 'o')
 ('y', 'n')
 ('y', 'D')
 ('y', 'e')
 ('y', 'v')
 ('y', 'e')
 ('y', 'l')
 ('y', 'e')
 ('y', 'o')
 ('y', 'p')
 ('y', 'e')
 ('y', 'r')
 ('y', 's')
 ('t', 'h')
 ('t', 'o')
 ('h', 'r')
 ('h', 's')
 ('o', 'n')
 ('o', 'D')
 ('o', 'e')
 ('o', 'v')
 ('o', 'e') …. Continued …. 

Example 2::

 # importing the itertools library
 import itertools
 # define a sequence
 sequen = "TUTORIALANDEXAMPLE"
 # calculate possible combinations with letter 2
 combi = itertools.combinations(sequen, 2)
 # printing all combinations in output using for loop
 for m in combi: 
     print( m) 

Output of the program:

 ('A', 'P')
 ('A', 'L')
 ('A', 'E')
 ('N', 'D')
 ('N', 'E')
 ('N', 'X')
 ('N', 'A')
 ('N', 'M')
 ('N', 'P')
 ('N', 'L')
 ('N', 'E')
 ('D', 'E')
 ('D', 'X')
 ('D', 'A')
 ('D', 'M')
 ('D', 'P')
 ('D', 'L')
 ('D', 'E')
 ('E', 'X')
 ('E', 'A')
 ('E', 'M')
 ('E', 'P')
 ('E', 'L')
 ('E', 'E')
 ('X', 'A')
 ('X', 'M')
 ('X', 'P')
 ('X', 'L')
 ('X', 'E')
 ('A', 'M')
 ('A', 'P')
 ('A', 'L')
 ('A', 'E')
 ('M', 'P')
 ('M', 'L')
 ('M', 'E')
 ('P', 'L')
 ('P', 'E')
 ('L', 'E') …. Continued …. 

Explanation: In the above programs, we have firstly imported the itertools library. Then, we have defined a sequence in both programs. After that, we have calculated a particular set of combination with length 4 and 5 in example 1 & 2 respectively. We calculated the combinations using the itertools.combinations() function. After that, we have used a for loop in each example to print the calculated number of combinations in the output of the program. As we can see in the output, we have printed all different combinations.

2. Combinations with the help of replacement:

In itertools library, there is one more function present which we can use to find the all possible combinations of given input data. This method us called as combination_with_replacement(). It is very useful as it takes into consideration about the combination of a number itself while calculating all possible number of combinations in the given data. Let's look at some examples of Python program and understand this combination_with_replacement() method in a better way.

Example 1:

 # importing the itertools library
 import itertools
 # define a sequence
 sequen = ['T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'A', 'N', 'D', 'E', 'X', 'A', 'M', 'P', 'L', 'E']
 # calculate possible combinations with letter 2
 combi = itertools.combinations_with_replacement(sequen, 2)
 # printing all combinations in output using for loop
 for m in combi: 
     print(m) 

Output of the program:

 ('T', 'T')
 ('T', 'U')
 ('T', 'T')
 ('T', 'O')
 ('T', 'R')
 ('T', 'I')
 ('T', 'A')
 ('T', 'L')
 ('T', 'A')
 ('T', 'N')
 ('T', 'D')
 ('T', 'E')
 ('T', 'X')
 ('T', 'A')
 ('T', 'M')
 ('T', 'P')
 ('T', 'L')
 ('T', 'E')
 ('U', 'U')
 ('U', 'T')
 ('U', 'O')
 ('U', 'R')
 ('U', 'I')
 ('U', 'A')
 ('U', 'L')
 ('U', 'A')
 ('U', 'N')
 ('U', 'D')
 ('U', 'E')
 ('U', 'X')
 ('U', 'A')
 ('U', 'M')
 ('U', 'P')
 ('U', 'L')
 ('U', 'E')
 ('T', 'T')
 ('T', 'O')
 ('T', 'R')
 ('T', 'I')
 ('T', 'A')
 ('T', 'L')
 ('T', 'A')
 ('T', 'N')
 ('T', 'D')
 ('T', 'E')
 ('T', 'X')
 ('T', 'A')
 ('T', 'M')
 ('T', 'P')
 ('T', 'L')
 ('T', 'E')
 ('O', 'O')
 ('O', 'R')
 ('O', 'I')
 ('O', 'A')
 ('O', 'L')
 ('O', 'A')
 ('O', 'N')
 ('O', 'D')
 ('O', 'E')
 ('O', 'X')
 ('O', 'A')
 ('O', 'M')
 ('O', 'P')
 ('O', 'L')
 ('O', 'E')
 ('R', 'R')
 ('R', 'I')
 ('R', 'A')
 ('R', 'L')
 ('R', 'A')
 ('R', 'N')
 ('R', 'D') …. Continued … 

Explanation: In the above given program, we have used combination_with_replacement() to find all the possible combinations and then, we used the for loop to print all these combinations in the output of the program. The Python printed all these combinations in the form of tuple in the output.

3. Combinations of a given numeric set:

If the input data is in the form of numeric set and in the sorted order, the combinations_with_replacement() function of itertools library will return the output in the form of tuples. These Python tuples will be the all-possible combinations of the numeric data provided to us. We can set the length of the combination that we want in the output. Let's look at some examples of Python program and understand this in a better way.

Example 1:

 # importing the itertools library
 import itertools
 # define a sequence of numbers
 sequen = [1, 3, 5, 7, 9, 11]
 # calculate possible combinations with letter 2
 combi = itertools.combinations_with_replacement(sequen, 2)
 # printing all combinations in output using for loop
 for m in combi: 
     print(m) 

Output of the program:

 (1, 1)
 (1, 3)
 (1, 5)
 (1, 7)
 (1, 9)
 (1, 11)
 (3, 3)
 (3, 5)
 (3, 7)
 (3, 9)
 (3, 11)
 (5, 5)
 (5, 7)
 (5, 9)
 (5, 11)
 (7, 7)
 (7, 9)
 (7, 11)
 (9, 9)
 (9, 11)
 (11, 11) 

Example 2:

 # importing the itertools library
 import itertools
 # define a sequence of numbers
 sequen = [2, 4, 6, 8, 10, 12, 14] 
 # calculate possible combinations with letter 3
 combi = itertools.combinations_with_replacement(sequen, 3)
 # printing all combinations in output using for loop
 for m in combi: 
     print(m) 

Output of the program:

 (2, 2, 2)
 (2, 2, 4)
 (2, 2, 6)
 (2, 2, 8)
 (2, 2, 10)
 (2, 2, 12)
 (2, 2, 14)
 (2, 4, 4)
 (2, 4, 6)
 (2, 4, 8)
 (2, 4, 10)
 (2, 4, 12)
 (2, 4, 14)
 (2, 6, 6)
 (2, 6, 8)
 (2, 6, 10)
 (2, 6, 12)
 (2, 6, 14)
 (2, 8, 8)
 (2, 8, 10)
 (2, 8, 12)
 (2, 8, 14)
 (2, 10, 10)
 (2, 10, 12)
 (2, 10, 14)
 (2, 12, 12)
 (2, 12, 14)
 (2, 14, 14)
 (4, 4, 4)
 (4, 4, 6)
 (4, 4, 8)
 (4, 4, 10)
 (4, 4, 12)
 (4, 4, 14)
 (4, 6, 6)
 (4, 6, 8)
 (4, 6, 12) … continued … 

Explanation: In the above examples, we have provided with two different sets of numeric data. To find all the possible combinations of the given numeric data, we have imported itertools library in the program and then used the combination function on the numeric data provided to us. We used for loop to print the all-possible combinations in the output of the program. The all-possible combinations of the numeric data will be printed in the form of Python tuples.



ADVERTISEMENT
ADVERTISEMENT