- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
N element incremental tuples in Python
When it is required to create 'N' element incremental tuples, generator expression and 'tuple' method can be used.
Below is a demonstration of the same −
Example
N = 3 print("The value of 'N' has been initialized") print("The number of times it has to be repeated is : ") print(N) my_result = tuple((elem, ) * N for elem in range(1, 6)) print("The tuple sequence is : ") print(my_result)
Output
The value of 'N' has been initialized The number of times it has to be repeated is : 3 The tuple sequence is : ((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5))
Explanation
- The value of 'N' is initialized, and is displayed on the console.
- A tuple with a specific element, multiplied 'N' times in a given range is assigned to a variable.
- This sequence is generated, and is displayed as output on the console.
Advertisements