Skip to content
Advertisement

Creating a Tuple with a variable and Boolean Python

I’m supposed to add a variable and a boolean to a new Tuple – the actual assignment is below, with my code. I know tuples are immutable – this is the first time I’ve tried to make one. Additionally, I can’t find anything about inserting the variable and a boolean. Thanks in advance!

My code is just created a new list. This is the desired result:

JavaScript

Assignment:

The string module provides sequences of various types of Python characters. It has an attribute called digits that produces the string ‘0123456789’. Import the module and assign this string to the variable nums. Below, we have provided a list of characters called chars. Using nums and chars, produce a list called is_num that consists of tuples. The first element of each tuple should be the character from chars, and the second element should be a Boolean that reflects whether or not it is a Python digit.

JavaScript

Advertisement

Answer

You can use a list comprehension for this, which is like a more concise for loop that creates a new list

JavaScript

This would be equivalent to the follow loop

JavaScript

Note that the containment check is being done using in against string.digits

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement