- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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 −
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)
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)
- 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 Articles
- How to easily initialize a list of Tuples in C#?
- Python parameters with variable length
- How do we initialize an array within object parameters in java?
- Python – Filter Tuples with Integers
- Python – Extract tuples with elements in Range
- 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
- Python - Ways to initialize list with alphabets
- Different ways to initialize list with alphabets in python
- Initialize Matrix in Python
- Combining tuples in list of tuples in Python
- Python Group tuples in list with same first value
- Count tuples occurrence in list of tuples in Python
- Combinations of sum with tuples in tuple list in Python
- Updating Tuples in Python