한 걸음씩

[python] 리스트 본문

Python

[python] 리스트

winter17 2023. 2. 6. 10:46

🌱  파이썬 리스트

# 리스트 표현 방법
squares = [1, 4, 9, 16, 25]
print(squares)
>> [1, 4, 9, 16, 25]
  • 리스트의 인덱싱과 슬라이싱
# 리스트 인덱싱
print(squares[0]) 
>> 1
print(squares[-1])
>> 25

# 리스트 슬라이싱
print(squares[-3:])
>> [9, 16, 25]
print(squares[:])
>> [1, 4, 9, 16, 25]
  • 리스트의 이어붙이기
print(squares + [36, 49, 64, 81, 100])
>> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  • 리스트는 가변(내용 변경 가능) but, 문자열은 불변
cubes = [1, 8, 27, 65, 125]  

cubes[3] = 64  # 세 번째 위치의 내용 변경
print(cubes)
>> [1, 8, 27, 64, 125]
  • 리스트의 끝에 새 항목 추가 메서드 append( )
cubes.append(216)
print(cubes)
>> [1, 8, 27, 64, 125, 216, 343]
  • 리스트의 길이 내장함수 len( )
letters = ['a', 'b', 'c', 'd']
print(len(letters))
>> 4
  • 이중 리스트
a = ['a', 'b', 'c']
n = [1, 2, 3]
x = [a, n]

# 이중리스트
print(x)
>> [['a', 'b', 'c'], [1, 2, 3]]

# 이중리스트 인덱싱 
print(x[0])
>> ['a', 'b', 'c']

print(x[0][1])
>> b

https://docs.python.org/ko/3/tutorial/introduction.html

 

3. An Informal Introduction to Python

In the following examples, input and output are distinguished by the presence or absence of prompts (>>> and …): to repeat the example, you must type everything after the prompt, when the prompt ap...

docs.python.org

 

'Python' 카테고리의 다른 글

[python] 자료구조  (0) 2023.02.09
[python] 제어문, 반복문, 함수  (0) 2023.02.08
[python] 내장 함수, 용어 공식문서 확인하기  (0) 2023.02.07
[python] 문자열  (1) 2023.02.06
[python] 연산자  (0) 2023.02.06