Home » Python

Reinitializing a tuple in Python

Python | Reinitializing tuple: Here, we are going to learn about the various methods to reinitialize a tuple in Python programming language?
Submitted by IncludeHelp, on April 08, 2020

Python | Reinitializing tuple

In this tutorial, we will learn how can we reinitialize a tuple with a new set of elements/objects?

To reinitialize a tuple, we can use tuple() or a pair of round brackets (), the steps are,

  1. Reinitialize the tuple using () or tuple()
  2. Reassign the new set of objects/elements

Consider the below program,

# Reinitializing a tuple in Python

# tuple creation
x = ("Shivang", 21, "Indore", 9999867123)

# printing original tuple
print("x: ", x)
print("len(x): ", len(x))
print("type(x): ", type(x))
print()

# Reinitializing tuple using tuple()
x = tuple()
# assigning new  objects
x = (10, 20, 30, 40, 50)
print("x: ", x)
print("len(x): ", len(x))
print("type(x): ", type(x))
print()

# Reinitializing tuple using ()
x = ()
# assigning new  objects
x = ("Amit", 18, "Jaipur", 98.24, 8888888888)
print("x: ", x)
print("len(x): ", len(x))
print("type(x): ", type(x))
print()

Output

x:  ('Shivang', 21, 'Indore', 9999867123)
len(x):  4
type(x):  <class 'tuple'>

x:  (10, 20, 30, 40, 50)
len(x):  5
type(x):  <class 'tuple'>

x:  ('Amit', 18, 'Jaipur', 98.24, 8888888888)
len(x):  5
type(x):  <class 'tuple'>



Comments and Discussions!

Load comments ↻






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