Answers for "ways 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
0

create tuples python

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

values = tuple(values)

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

ways to create tuple in python

# There are more ways to create a TUPLE in Python.

# First of all, a TUPLE is usually created by:
# (STEP 1) creating a Parethesis ()
# (STEP 2) putting a value in Parenthesis. Example: (3)
# (STEP 3) putting more value by adding Comma. Example: (3, 4)
# (STEP 4) Repeat Step 3 if u want to add more value. Example: (3, 4, coffee)
# That's how to create TUPLE

# EXAMPLE
adsdfawea = (123, 56.3, 'cheese')

# But did you know, you can create TUPLE without any value inside the () ???
wallet = ()
print(type(wallet))

# But did you know, you can create TUPLE without parenthesis () ???
plate = 'cake',
print(type(plate))

# As you can see, STRING is possible in TUPLE, not just INTEGERS or FLOATS.
Posted by: Guest on September-05-2021
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 "ways to create tuple in python"

Python Answers by Framework

Browse Popular Code Answers by Language