Python tuple() Built in Function

The tuple() function in Python is a built-in function that creates a tuple object from an iterable such as a list, set, string or dictionary. A tuple is a collection of data that is ordered and unchangeable.

Here are some examples of using the tuple() function:

# Example 1: Creating a tuple from a list
my_list = [1, 2, 3]
my_tuple = tuple(my_list)
print(my_tuple)
# Output: (1, 2, 3)

# Example 2: Creating a tuple from a set
my_set = {4, 5, 6}
my_tuple = tuple(my_set)
print(my_tuple)
# Output: (4, 5, 6)

# Example 3: Creating a tuple from a string
my_string = "hello"
my_tuple = tuple(my_string)
print(my_tuple)
# Output: ('h', 'e', 'l', 'l', 'o')

# Example 4: Creating a tuple from a dictionary
my_dict = {"a":10, "b":20}
my_tuple = tuple(my_dict)
# only keys are taken
print(my_tuple) # Output: ('a', 'b')

If you have any questions about this code, you can drop a line in comment.

Comments

Popular posts from this blog

Python chr() Built in Function

Stock Market Predictions with LSTM in Python

Collections In Python

Python Count Occurrence Of Elements

Python One Liner Functions