FREE INTERACTIVE ONLINE COURSE

Python 3

FOR BEGINNERS


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)


ALL REVIEWS
LESSON 14
PAGE 4 / 4
Tuples
Home >>> Free Online Lessons, Python 3

Modifying a Tuple - An Interesting Trick!

Suppose we have entered the values of a tuple's elements incorrectly or, within the program, we simply need modifications. The object is inherently immutable, so we will use a trickexplicit conversion.

Analyze the code below:
Editor - lesson_14.py
       
Console/Output done
Step 1. Lists are mutable objects, so we create one named temp that contains all the elements of the tuple pers1.



Step 2. Thus, we can rewrite the element at the second position in the temp list accordingly.

Step 3. Now that everything is correct and as we wanted, we store the data in the pers1 variable, explicitly converting the temp list to a tuple using its constructor.

Done, we're finished!
The lesson is now over.
 home   list  CONTENTS   perm_identity   arrow_upward