[toc]
Problem Statement
How to take Set as an Input from the User?
Method 1: Using a Set Comprehension
Approach: The simplest way to take a set as an input from the user is to use a set comprehension.
Set Comprehension is quite similar to a list comprehension. It is a concise way to create a set in Python with the help of curly braces.
Syntax:{expression for element in context}
Example: {a for a in range(5)}
will create a set = {0, 1, 2, 3, 4}
To learn more about set comprehensions refer to this article: “A Simple Introduction to Set Comprehension in Python“
Solution:
x = input('some text : ') x = {a for a in x.split(",")} print(x)
Output:
some text : apple, mango, orange
{' orange', 'apple', ' mango'}
Method 2: Using set.add()
In the previous approach, we had to enter the entire set all at once. But now we will have a look at another method using which we can ask the user to enter the values one by one. The user can feed in values as long as he wants and type exit once he wants to stop feeding in more values. The values can be appended to a tuple one by one using the add() method.
Solution: Please follow the comments mentioned in the script to understand how it works.
# Prompt user to enter a value set_val = input('Enter a value (enter exit to stop!): ') # initialize the resultant set set_x = {} # if value entered by user is not exit then store the value in set_x if set_val != 'exit': set_x = set(set_val) # Iterate as long as user keeps entering values while True: # evaluate if block if value entered by user is not exit if set_val != 'exit': # prompt the user to enter next value set_val = input('Enter a value (enter exit to stop!): ') # evaluate if block if value entered by user is not exit if set_val != 'exit': # append/add value entered by user to set_val set_x.add(set_val) # if value entered by user is exit then break out of the loop else: break # print the resultant set print(set_x)
Output:
Enter a value (enter exit to stop!): 1
Enter a value (enter exit to stop!): 2
Enter a value (enter exit to stop!): 3
Enter a value (enter exit to stop!): finxter
Enter a value (enter exit to stop!): exit
{'2', '1', 'finxter', '3'}
How to take Tuple as an Input from the User?
Method 1: Typecasting a List Comprehension to a Tuple
In Python, comprehensions can be performed upon dictionaries, lists and sets but tuple comprehensions do not produce feasible outputs. The result of applying comprehension to a tuple is a generator object. Thus, the work-around to this problem is to use a list comprehension to solve our problem and then typecast the output to a tuple.
Thus, we will use a list comprehension to accept values from the user and then typecast it to a tuple.
Solution:
x = input('Enter the tuple : ') x = tuple(int(a) for a in x.split(",")) print(x)
Output:
Enter some text : 1,2,3,4,5
('1', '2', '3', '4', '5')
Method 2: By Appending Value to a Tuple
If you want the user to enter values one by one and store them in a tuple then the following method will help you to do that. You can prompt the user to enter a value. As long as the user wants to enter a value, the program allows him/her to do so with the help of a loop. You can keep storing the values one by one in a tuple and finally display the contents of this tuple as an output.
Solution:
# Prompt user to enter a value tup_val = input('Enter a value (enter exit to stop!): ') # initialize the resultant tuple tup_x = () # if value entered by user is not exit then store the value in tup_x if tup_val != 'exit': tup_x = tuple(tup_val) # Iterate as long as user keeps entering values while True: # evaluate if block if value entered by user is not exit if tup_val != 'exit': # prompt the user to enter next value tup_val = input('Enter a value (enter exit to stop!): ') # evaluate if block if value entered by user is not exit if tup_val != 'exit': # append/add value entered by user to tup_val tup_x = tup_x + (tup_val,) # if value entered by user is exit then break out of the loop else: break # print the resultant set print(tup_x)
Output:
Enter a value (enter exit to stop!): 1
Enter a value (enter exit to stop!): 3
Enter a value (enter exit to stop!): 5
Enter a value (enter exit to stop!): Mercury
Enter a value (enter exit to stop!): Venus
Enter a value (enter exit to stop!): Earth
Enter a value (enter exit to stop!): exit
('1', '3', '5', 'Mercury', 'Venus', 'Earth')
Related Questions
How to Add Item to a Tuple in Python?
To add an item to a tuple you can simply use the + operator as shown in the following example.
Example:
tup = ('Python', 'Java') tup = tup + ('C++',) print(tup) # ('Python', 'Java', 'C++')
How to Append Values to a Set in Python?
You can append values to a set using the add() function. The add() function effectively appends single values to a set. To append multiple values from another list, tuple, frozen set to the set, you can use the update method.
Example:
# append single value my_set = {'Brock', 'Rock'} my_set.add('John') print(my_set) # append multiple values my_set = {'One', 'Two', 'Three'} my_set.update(['Four', 'Five']) print(my_set) # append an entire tuple as a single item my_set = {1, 2, 3} my_set.add(('One', 'Two', 'Three')) print(my_set)
Output:
('Python', 'Java', 'C++')
{'Brock', 'John', 'Rock'}
{'Two', 'Four', 'One', 'Five', 'Three'}
{('One', 'Two', 'Three'), 1, 2, 3}
Related Video
Conclusion
Phew! That was it for this article. We unearthed the answers to some of the frequently asked questions here –
- How to take Set as an Input from the User?
- How to take Tuple as an Input from the User?
- How to Add Item to a Tuple in Python?
- How to Append Values to a Set in Python?
I hope this article has helped you in your coding journey. Please stay tuned and subscribe to keep getting soutions and enhance your Python skills. Happy learning!
One of the most sought-after skills on Fiverr and Upwork is web scraping .
Make no mistake: extracting data programmatically from web sites is a critical life-skill in todayβs world thatβs shaped by the web and remote work.
This course on Finxter Academy teaches you the ins and outs of Pythonβs BeautifulSoup library for web scraping.