1my_tuple = ("hello")
2print(type(my_tuple)) # <class 'str'>
3
4# Creating a tuple having one element
5my_tuple = ("hello",)
6print(type(my_tuple)) # <class 'tuple'>
7
8# Parentheses is optional
9my_tuple = "hello",
10print(type(my_tuple)) # <class 'tuple'>
1t = 12345, 54321, 'hello!'
2print(t[0])
3# output 12345
4print(t)
5# output (12345, 54321, 'hello!')
6# Tuples may be nested:
7u = t, (1, 2, 3, 4, 5)
8print(u)
9# output ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
10# Tuples are immutable:
11# assigning value of 12345 to 88888
12t[0] = 88888
13Traceback (most recent call last):
14 File "<stdin>", line 1, in <module>
15TypeError: 'tuple' object does not support item assignment
16# but they can contain mutable objects:
17v = ([1, 2, 3], [3, 2, 1])
18print(v)
19# output ([1, 2, 3], [3, 2, 1])
1#a tuple is basically the same thing as a
2#list, except that it can not be modified.
3tup = ('a','b','c')
1# There are more ways to create a TUPLE in Python.
2
3# First of all, a TUPLE is usually created by:
4# (STEP 1) creating a Parethesis ()
5# (STEP 2) putting a value in Parenthesis. Example: (3)
6# (STEP 3) putting more value by adding Comma. Example: (3, 4)
7# (STEP 4) Repeat Step 3 if u want to add more value. Example: (3, 4, coffee)
8# That's how to create TUPLE
9
10# EXAMPLE
11adsdfawea = (123, 56.3, 'cheese')
12
13# But did you know, you can create TUPLE without any value inside the () ???
14wallet = ()
15print(type(wallet))
16
17# But did you know, you can create TUPLE without parenthesis () ???
18plate = 'cake',
19print(type(plate))
20
21# As you can see, STRING is possible in TUPLE, not just INTEGERS or FLOATS.
22
23
1example = [1, 2, 3, 4]
2# Here is a list above! As we both know, lists can change in value
3# unlike toples, which are not using [] but () instead and cannot
4# change in value, because their values are static.
5
6# list() converts your tuple into a list.
7tupleexample = ('a', 'b', 'c')
8
9print(list(tupleexample))
10
11>> ['a', 'b', 'c']
12
13# tuple() does the same thing, but converts your list into a tuple instead.
14
15print(example)
16
17>> [1, 2, 3, 4]
18
19print(tuple(example))
20
21>> (1, 2, 3, 4)