printf("ho_tari\n");

리스트 본문

Python

리스트

호타리 2023. 10. 12. 14:59

<indexing.py>

colors = ['red', 'blue', 'green']

print(colors[0])
print(colors[2])
print(len(colors))

<compile 결과>

 

<slicing.py>

cities = ['서울', '부산', '인천', '대구', '대전', '광주', '울산', '수원']

print(cities[0:6])  # 파이썬의 리스트에서 마지막 인덱스값은 출력되지 않는다
print(cities[0:5])
print(cities[5:])

<compile 결과>

 

<reverse_index.py>

cities = ['서울', '부산', '인천', '대구', '대전', '광주', '울산', '수원']

print(cities[-8:])	# -8의 인덱스값부터 끝까지
print(cities[:])    # cities 변수의 처음부터 끝까지
print(cities[-50:50])   # 범위를 넘어갈 결우 자동으로 최대 범위를 지정

<compile 결과>

 

<list.py>

color1 = ['red', 'blue', 'green']
color2 = ['orange', 'black', 'white']

print(color1 + color2)  # 리스트 합치기

print(len(color1))  # 리스트 길이

total_color = color1 + color2

print(total_color)

print(color1 * 2)   # color1 리스트 2회 반복

print('blue' in color2) # color2 변수에서 문자열 'blue'의 존재 여부 반환

color3 = ['red', 'blue', 'green']
color3.append('white')   # 리스트에 'white' 추가
print(color3)

color4 = ['red', 'blue', 'green']
color4.extend(['black', 'purple'])  # 리스트에 새로운 리스트 추가
print(color4)

color5 = ['red', 'blue', 'green']
color5.insert(0, 'orange')  # 리스트의 특정 위치에 값 추가 (0번째 인덱스)
print(color5)

color6 = ['red', 'blue', 'green']
color6.remove('red')    # 리스트에 있는 특정 값 삭제
print(color6)

color7 = ['red', 'blue', 'green']
color7[0] = 'orange'    # 0번쨰 인덱스를 'orange'로 변경
print(color7)
del color7[0]   # 0번째 인덱스 값 삭제
print(color7)

<compile 결과>

 

<list2.py>

kor_score = [49, 79, 20, 100, 80]
math_score = [43, 59, 85, 30, 90]
eng_score = [49, 79, 48, 60, 100]
midterm_score = [kor_score, math_score, eng_score]

print(midterm_score)

print(midterm_score[0][2])  # [0]은 행, [2]는 열

math_score[0] = 1000
print(midterm_score)

<compile 결과>

 

<list_memory.py>

a = ["color", 1, 0.2]
color = ['yellow', 'blue', 'green', 'black', 'purple']
a[0] = color    # 리스트 안에 리스트도 입력 가능
print(a)

b = [5, 4, 3, 2, 1]
c = [1, 2, 3, 4, 5]
c = b
print(c)

b.sort()    # 리스트에 있는 값들의 순서를 오름차순으로 변환
print(c)

d = [6, 7, 8, 9, 10]
print(b, d)

<compile 결과>

'Python' 카테고리의 다른 글

문자열  (0) 2023.10.16
함수  (0) 2023.10.16
반복문  (0) 2023.10.12
조건문  (0) 2023.10.12
입출력 함수, 화씨온도 변환기  (0) 2023.10.12