[파이썬] Tuples 간의 연산, 합치기, 반복, 검색 예제

Tuples 간의 연산 및 다양한 작업에 대한 예제를 제공하겠습니다.

1. Tuples 합치기 (Concatenation):

두 개의 Tuples를 합칠 때는 + 연산자를 사용합니다.

fruits = ('apple', 'banana')
vegetables = ('carrot', 'potato')

# Tuples 합치기
combined = fruits + vegetables
print(combined)  # ('apple', 'banana', 'carrot', 'potato')

2. Tuples 반복:

Tuples의 내용을 반복하려면 * 연산자를 사용합니다.

fruits = ('apple', 'banana')

# Tuples 반복
repeated_fruits = fruits * 3
print(repeated_fruits)  # ('apple', 'banana', 'apple', 'banana', 'apple', 'banana')

3. 요소 검색:

특정 요소가 Tuple 안에 있는지 확인할 때 in 연산자를 사용합니다.

fruits = ('apple', 'banana', 'cherry')

# 요소 검색
print('banana' in fruits)  # True
print('orange' in fruits)  # False

4. 최댓값 및 최솟값 찾기:

Tuple의 최댓값과 최솟값을 찾을 때 max()min() 함수를 사용합니다.

numbers = (3, 1, 4, 1, 5, 9, 2, 6)

# 최댓값 찾기
maximum = max(numbers)
print(maximum)  # 9

# 최솟값 찾기
minimum = min(numbers)
print(minimum)  # 1

5. 요소의 인덱스 검색:

특정 요소의 인덱스를 찾을 때 index() 메서드를 사용합니다.

fruits = ('apple', 'banana', 'cherry')

# 'banana'의 인덱스 찾기
index = fruits.index('banana')
print(index)  # 1

이러한 연산과 검색을 사용하여 Tuples를 효과적으로 다룰 수 있습니다.