

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Initialize tuples with parameters in Python
When it is required to initialize the tuples with certain parameters, the 'tuple' method and the '*' operator can be used.
The 'tuple' method will convert the iterable passed to it as parameter into a tuple class type.
The * operator can be used to get the product of two values. It can also be used to multiple a single value multiple times and display it on the console.
Below is a demonstration for the same −
Example
N = 6 print("The value of N has been initialized to "+str(N)) default_val = 2 print("The default value has been initialized to " +str(default_val)) indx = 3 print("The index value has been initialized to "+ str(indx)) val_to_add = 6 print("The value to be added is initialized to " +str(val_to_add)) my_result = [default_val] * N my_result[indx] = val_to_add my_result = tuple(my_result) print("The tuple formed is : ") print(my_result)
Output
The value of N has been initialized to 6 The default value has been initialized to 2 The index value has been initialized to 3 The value to be added is initialized to 6 The tuple formed is : (2, 2, 2, 6, 2, 2)
Explanation
- The value for 'N', 'index', 'value to be added' and a default value are initialized and displayed on the console.
- The default value is multiplied with 'N' and assigned to a variable.
- This operation is assigned to a variable.
- That variable's 'index' is assigned the value that needs to be added.
- Next, the variable is converted into a tuple, and stored in a variable.
- This variable is the output that is displayed on the console.
- Related Questions & Answers
- How to easily initialize a list of Tuples in C#?
- How do we initialize an array within object parameters in java?
- Python - Ways to initialize list with alphabets
- Python – Filter Tuples with Integers
- Initialize Matrix in Python
- Python program to find Tuples with positive elements in List of tuples
- Python program to find Tuples with positive elements in a List of tuples
- Combining tuples in list of tuples in Python
- Python – Extract tuples with elements in Range
- Updating Tuples in Python
- Compare tuples in Python
- Count tuples occurrence in list of tuples in Python
- How to Concatenate tuples to nested tuples in Python
- Remove duplicate tuples from list of tuples in Python
- Python Group tuples in list with same first value
Advertisements