×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python program for creating N element incremental tuple

In this program, we are given a number N and a range (L, R). We need to create Python Program for creating N element incremental tuple.
Submitted by Shivang Yadav, on December 15, 2021

In Python, there are a lot of implementations where storing and creating repetitive values as elements of a collection is required. Learning to perform such a simple task can be useful in your programming carrier. Here, we are given a number N and a range (L, R). We need to create Python Program for creating N element incremental tuple.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

N element incremental Tuples

In this program, we are given a number N and a range (L, R). We need to create Python program for creating N element incremental tuple. We will create a tuple for each element of the range, with i [in the range (L, R)] repeatedly N times.

Input:
N = 4, [L, R] = [3, 6]

Output:
((3, 3, 3, 3), (4, 4, 4, 4), (5, 5, 5, 5), (6, 6, 6, 6), )

For this, we will loop for the range and for each number of range create repeat the given number N time and create a tuple with these elements.

In Python, there are multiple methods to perform the task.

Method 1:

One method to create N incremental tuples is by using a generator expression in which we will iterate through all elements in the range [L, R] and create a tuple with N element of that range. And finally, encapsulate it in a tuple using the tuple() method.

# Python program for creating 
# N element incremental tuple

# Initializing and printing N and range
N = 4
L = 2
R = 7

print("The Value of N is " + str(N))
print("The range is (" + str(L) + ", " + str(R) + ")")

# creting N element incremental tuples
incrTuple = tuple((value, ) * N for value in range(L, R))

print("Tuple consisting of N element incremental Tuple is " + str(incrTuple))

Output:

The Value of N is 4
The range is (2, 7)
Tuple consisting of N element incremental Tuple is ((2, 2, 2, 2), (3, 3, 3, 3), (4, 4, 4, 4), (5, 5, 5, 5), (6, 6, 6, 6))

Method 2:

Another method to solve the problem is by using the repeat method to repeat the value of range N times. And create a tuple using the generator expression only.

# Python program for creating 
# N element incremental tuple

import itertools

# Initializing and printing N and range
N = 4
L = 2
R = 7

print("The Value of N is " + str(N))
print("The range is (" + str(L) + ", " + str(R) + ")")

# creting N element incremental tuples
incrTuple = tuple(tuple(itertools.repeat(ele, N)) for ele in range(L, R))

print("Tuple consisting of N element incremental Tuple is " + str(incrTuple))

Output:

The Value of N is 4
The range is (2, 7)
Tuple consisting of N element incremental Tuple is ((2, 2, 2, 2), (3, 3, 3, 3), (4, 4, 4, 4), (5, 5, 5, 5), (6, 6, 6, 6))

Python Tuple Programs »



Related Programs

Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.