Tuple()

Tuple is what only Python has.

It is similar to lists, but there are a few differences.

Your use () instead of [], and once you put the data in the tuple, the data cannot be edited. As lists, you can mix different datatypes in the tuples.

Example1

t1 = ()                         # An empty tuple
t2 = (1,)
t3 = (1,2,3)
t4 = 1, 2, 3
t5 = (30, 3.14, True, False, 'python')
# t5[1] = 42.195               # error: tuple cannot be edited.
t6 = ('a', 'b', 'c', ('ab', 'cd'))

print(type(t1))                 # <class 'tuple'>
print(type(t2))                 # <class 'tuple'>
print(type(t4))                 # <class 'tuple'>
print('t1:', t1)
print('t2:', t2)
print('t3:', t3)
print('t4:', t4)
print('t5:', t5)
print('t6:', t6)

indexing

t1 = (1, 2, 'a', 'b')
print(t1[0])                        # 1
print(t1[1])                        # 2
print(t1[2])                        # a
print(t1[3])                        # b
print(t1[-1])                       # b

slicing

# [ start index : end index ]
t2 = (10, 20, 30, 40, 50)
print(t2[1:3])                      # 1 - 2 : (20, 30)
print(t2[ :4])                      # 0 - 3  : (10, 20, 30, 40)
print(t2[1:])                       # 1 - end : (20, 30, 40, 50)

add

print(t1 + t2)                      # (1, 2, 'a', 'b', 10, 20, 30, 40, 50)
print(t2 + t1)                      # (10, 20, 30, 40, 50, 1, 2, 'a', 'b')

multiply

print(t2 * 3)            # (10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50)

len() with tuple

t1 = (10, 20, 30, 40, 50)
print(len(t1))                      # len : 5
print(t1)                           # (10, 20, 30, 40, 50)

count() with tuple

t2 = (1,100,2,100,3,100,4,100,5,100)
print(t2.count(100))

index() with tuple

t3 = ('java','jsp','python','spring','R','tensorflow','keras')
print(t3.index('spring'))

tuple packing

To bind multiple data into the tuple

t1 = 10, 20, 30
print(type(t1))                     # <class 'tuple'>
print(t1)                           # (10, 20, 30)

tuple unpacking

10 -> one / 20 -> two / 30 -> three

one, two, three = t1
print('one:', one)                  # 10
print('two:', two)                  # 20
print('three:', three)              # 30

 

set()

You can't save overlapped data in set(). It is not an ordered data structure so you can't print the data out in order.

s1 = set([1,2,3,4])
print(type(s1))                     # <class 'set'>
print(s1)                           # {1, 2, 3, 4}

s2 = set('Hello')
print(type(s2))                     # <class 'set'>
print(s2)                           # {'e', 'H', 'o', 'l'}

set() -> list() 

list1 = list(s3)
print(type(list1))                  # <class 'list'>
print(list1)                        # [1, 2, 3]
print(list1[0])                     # 1
print(list1[1])                     # 2
print(list1[2])                     # 3

set() -> tuple()

t1 = tuple(s3)
print(type(t1))                     # <class 'tuple'>
print(t1)                           # (1, 2, 3)
print(t1[0])                        # 1
print(t1[1])                        # 2
print(t1[2])                        # 3

intersection(), union(), and difference()

s1 = set([1,2,3,4,5,6])
s2 = set([4,5,6,7,8,9])

# 1. intersection()
print(s1 & s2)                          # {4, 5, 6}
print(s1.intersection(s2))              # {4, 5, 6}

# 2. union()
print(s1 | s2)                          #  {1, 2, 3, 4, 5, 6, 7, 8, 9}
print(s1.union(s2))                     #  {1, 2, 3, 4, 5, 6, 7, 8, 9}

# 3. difference()
print(s1 - s2)                          # {1, 2, 3}
print(s1.difference(s2))                # {1, 2, 3}

print(s2 - s1)                          # {8, 9, 7}
print(s2.difference(s1))                # {8, 9, 7}

add(), update(), and remove()

# 1. add() 
s1 = set([1, 2, 3])
s1.add(3)                          # Overlapped data cannot be saved.
s1.add(4)
# s1.add([4, 5])                   # Only one data can be saved.
print(s1)                          # {1, 2, 3, 4}

# 2. update() : to add multiple data
s2 = set([1, 2, 3])
s2.update([4, 5, 6])
print(s2)                           # {1, 2, 3, 4, 5, 6}

# 3. remove() 
s3 = set([1, 2, 3])
s3.remove(2)                       
s3.remove(3)                       
print(s3)                           # {1}

'Python' 카테고리의 다른 글

Python) Control statements  (0) 2022.11.06
Python) Dictionary  (0) 2022.11.04
Python) Datatypes - Str  (0) 2022.11.03
Python) Variables and Operators  (0) 2022.10.29
Python) Configuration and Basics  (0) 2022.10.27

+ Recent posts