Python Exercise: Add an item in a tuple
Python tuple: Exercise-5 with Solution
Write a Python program to add an item in a tuple.
#create a tuple tuplex = (4, 6, 2, 8, 3, 1) print(tuplex) #tuples are immutable, so you can not add new elements #using merge of tuples with the + operator you can add an element and it will create a new tuple tuplex = tuplex + (9,) print(tuplex) #adding items in a specific index tuplex = tuplex[:5] + (15, 20, 25) + tuplex[:5] print(tuplex) #converting the tuple to list listx = list(tuplex) #use different ways to add items in list listx.append(30) tuplex = tuple(listx) print(tuplex)
(4, 6, 2, 8, 3, 1) (4, 6, 2, 8, 3, 1, 9) (4, 6, 2, 8, 3, 15, 20, 25, 4, 6, 2, 8, 3) (4, 6, 2, 8, 3, 15, 20, 25, 4, 6, 2, 8, 3, 30)
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Decapitalizes the first letter of a string:
def tips_decapitalize(s, upper_rest=False): return s[:1].lower() + (s[1:].upper() if upper_rest else s[1:]) print(tips_decapitalize('PythonTips')) print(tips_decapitalize('PythonTips', True))
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework