Python Program to Display All Combinations of Picking Two Numbers from 10
Python is a high-level, general-purpose programming language, widely used in various fields, including data science, machine learning, and web development. In this blog post, we will learn how to write a Python program that prompts the user to enter 10 integers and displays all the combinations of picking two numbers from the 10.
Table of Contents
- Prompting the User for Input
- Displaying All Combinations of Picking Two Numbers without Combination Library
- Sample Code without Combination Library
- Prompting the User for Input
- Displaying All Combinations of Picking Two Numbers Using Combination Library
- Sample Code with Combination Library
| Learn Python with " Computer Courses - All in One "
Python Program: Picking Two Numbers Without Combination Library
Prompting the User for Input
We will use the input() function in Python to prompt the user for input. The input() function reads a line from the console and returns it as a string. Since we need to accept integers as input, we will perform explicit type-conversion using the int() function.
numbers = []
for i in range(10):
num = int(input("Enter an integer: "))
numbers.append(num)
In the code above, we use a for loop to prompt the user for 10 integers and store them in a list called numbers.
Displaying All Combinations of Picking Two Numbers
To show all combinations of picking two numbers from the list of 10 integers, we will use nested loops. The outer loop will iterate through each number in the list, and the inner loop will iterate through each number after the current number in the outer loop.
for i in range(len(numbers)):
for j in range(i+1, len(numbers)):
print(numbers[i], numbers[j])
In the code above, we use two for loops to iterate through each number in the list and print all combinations of picking two numbers.
Sample Python Code
Here's the complete code for our program:
numbers = []
for i in range(10):
num = int(input("Enter an integer: "))
numbers.append(num)
for i in range(len(numbers)):
for j in range(i+1, len(numbers)):
print(numbers[i], numbers[j])
Output:
Enter an integer: 1Enter an integer: 2Enter an integer: 3Enter an integer: 4Enter an integer: 5Enter an integer: 6Enter an integer: 7Enter an integer: 8Enter an integer: 9Enter an integer: 101 21 31 41 51 61 71 81 91 10
.
.
.
We learned how to write a Python program that prompts the user to enter 10 integers and displays all combinations of picking two numbers from those 10 integers. We used nested loops to iterate through each number in the list and print all combinations of picking two numbers. By following these steps, you can easily create your own Python programs that prompt users for input and perform various operations on that input.
| Practice Python MCQ with "Python MCQ Programs Interview " Android App.
Python Program: Picking Two Numbers using Combination Library
Prompting the User for Input
We will use the input() function in Python to prompt the user for input. The input() function reads a line from the console and returns it as a string. Since we need to accept integers as input, we will perform explicit type-conversion using the int() function.
numbers = []
for i in range(10):
num = int(input("Enter an integer: "))
numbers.append(num)
In the code above, we use a for loop to prompt the user for 10 integers and store them in a list called numbers.
Displaying All Combinations of Picking Two Numbers Using Combination Library
To display all combinations of picking two numbers from the list of 10 integers using the combination library, we will import the combinations function from the itertools module.
from itertools import combinations
comb = combinations(numbers, 2)
for c in comb:
print(c[0], c[1])
In the code above, we use the combinations function to generate all possible combinations of picking two numbers from the list of 10 integers. We then use a for loop to iterate through each combination and print it.
Sample Code
Here's the complete code for our program:
from itertools import combinations
numbers = []
for i in range(10):
num = int(input("Enter an integer: "))
numbers.append(num)
comb = combinations(numbers, 2)
for c in comb:
print(c[0], c[1])
Output:
Enter an integer: 1
Enter an integer: 2
Enter an integer: 3
Enter an integer: 4
Enter an integer: 5
Enter an integer: 6
Enter an integer: 7
Enter an integer: 8
Enter an integer: 9
Enter an integer: 10
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
.
.
.
| Write a program to read n numbers from users and calculate the average of those n numbers.
We learned how to write a Python program that prompts the user to enter 10 integers and displays all combinations of picking two numbers from those 10 integers using the combination library. We used the combinations function from the itertools module to generate all possible combinations of picking two numbers from the list of 10 integers. By following these steps, you can easily create your own Python programs that prompt users for input and perform various operations on that input.
| Practical List - Python [ 4330701 ] [ PRACTICAL EXERCISES ]
FAQ: Python Program To displays all the combinations of picking two numbers from the 10.
1. What is the combination library in Python and how does it work
A: The combination library in Python is a part of the itertools module that provides various functions for efficient looping. The combinations() function of the itertools module takes two arguments - an iterable input and a positive integer n - and produces an iterator over tuples of all combinations of n elements in the input iterable. The elements in the tuples are assumed to be unique on the basis of their positions which are distinct for all. The combinations() function returns successive n-length combinations of elements in the iterable. The itertools module also provides other combinatoric iterators such as permutations(), combinations_with_replacement(), and product(). These functions are used to produce more complex iterators and are used to make memory-efficient and precise code. By using these functions, we can solve complex problems easily with the help of different sub-functions of itertools.
2. What are some other functions in the itertools library
A. The itertools library in Python provides various functions that work on iterators to produce complex iterators. Some of the other useful functions in the itertools library are:
chain(): This function is used to print all the values in iterable targets one after another.
repeat(): This iterator repeatedly prints the passed value an infinite number of times. If the optional keyword num is mentioned, then it repeatedly prints num number of times.
islice(): This function returns an iterator which returns selected items from the input iterator, by index.
count(): This function returns evenly spaced values given an input number until infinity. Thus, it is known as an “infinite iterator”. By default, the values will be evenly spaced by 1 but this can be set using the step parameter.
permutations(): This function returns successive r-length permutations of elements in an iterable.
combinations_with_replacement(): This function returns successive r-length combinations of elements in an iterable, allowing individual elements to have successive repeats.
These functions are used to make memory-efficient and precise code and are used either by themselves or in combination to form iterator algebra.
0 Comments