Answers for "how to create tuple in python"

52

what is a tuple in python

# A tuple is a sequence of immutable Python objects. Tuples are
# sequences, just like lists. The differences between tuples
# and lists are, the tuples cannot be changed unlike lists and
# tuples use parentheses, whereas lists use square brackets.
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = "a", "b", "c", "d";

# To access values in tuple, use the square brackets for
# slicing along with the index or indices to obtain value
# available at that index.
tup1[0] # Output: 'physics'
Posted by: Guest on March-03-2020
4

tuples in python

my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a)      # 3
print(b)      # 4.6
print(c)      # dog
Posted by: Guest on October-12-2020
2

tuples in python

my_tuple = ("hello")
print(type(my_tuple))  # <class 'str'>

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # <class 'tuple'>

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # <class 'tuple'>
Posted by: Guest on October-12-2020
0

create tuples python

values = [a, b, c, 1, 2, 3]

values = tuple(values)

print(values)
Posted by: Guest on December-15-2020
4

tuple in python

#a tuple is basically the same thing as a
#list, except that it can not be modified.
tup = ('a','b','c')
Posted by: Guest on March-14-2020
0

how to create tuple in python

# Let's have a look here first.
coffee = (3.14,)  # coffee is a VARIABLE, 3.14 is a FLOAT NUMBER

# What is the TYPE inside the VARIABLE coffee?

#Let's see
print(type(coffee)) #It will print what TYPE the VARIABLE it is

# It will print:  <class 'tuple'>



# Why it printed <class 'tuple'> and not <class 'float'>???


# ANSWER: # 

# Because we put the comma --> , <-- after the 3.14
# That's why it became a tuple type. (not a float type)


# HOW TO CREATE a TUPLE?

# To create a tuple, create a Parenthesis --> () <--
# Then add value inside, for example I add 4 inside the parenthesis --> (4) <--
# Last, add comma --> , <-- like this --> (4,) <--
# Then add more like this --> (4, 5) <--
# If you want to add more, just use --> , <-- like this --> (4, 5, 6) <--


# Wait, I don't understand, what's the purpose of TUPLE?
# it let you store an ordered sequence of items.
# It's similar to LIST but data inside the TUPLE can't be change.

# NOTES
# STRING is also possible in TUPLE
food = ('cake', 'cupcake', 1997, 2000);
# As you can see, the 'cake' and 'cupcake' are STRINGS, while 1997 and 2000 are INTEGER NUMBERS.
Posted by: Guest on September-05-2021

Code answers related to "how to create tuple in python"

Python Answers by Framework

Browse Popular Code Answers by Language