Python

[Python]List

sagesse2021 2021. 11. 14. 16:18
반응형
x = [1,2,3,4]
y = ["hello","world"]
z = ["hello", 1,2,3]

print(x)
print(y)
print(z)

//[1,2,3,4]

//["hello","world"]

//["hello", 1,2,3]

x = [1,2,3,4]

print(x[0])  # x의 0번째 자리 출력

//1

x = [1,2,3,4]

x[3] = 10  # x의 3번째 자리에 10넣기
print(x)

//[1,2,3,10]

# functions of list

x = [1,2,3,4]

num_elements = len(x) #리스트의 수를 출력하는 function
print(num_elements)

//4

x = [4,2,3,1]

y = sorted(x) # 리스트를 순서대로 정렬
print(y)

//[1,2,3,4]

x = [4,2,3,1]

z = sum(x) # 리스트의 합 
print(z)

//10

x = [4,2,3,1]
y = ["hello","there"]

# n과 c안에 리스트의 element가 차례대로 들어감
for n in x:
  print(n)

for c in y:
  print(c)

//4

//2

//3

//1

//hello

//there

x = [4,2,3,1]
y = ["hello","there"]

# element 위치 찾기
print(x.index(3))
print(y.index("hello"))

//2 (리스트의 두번째 자리를 의미)

//0

x = [4,2,3,1]
y = ["hello","there"]

print("hello" in y)  # y안에 hello가 있는지 확인하기
print("bye" in y)

//True

//False

x = [4,2,3,1]
y = ["hello","there"]

if "hello" in y:
  print("hello가 있어요")

//hello가 있어요

반응형

'Python' 카테고리의 다른 글

[Python]과일 숫자 세는 프로그램 만들기(list, 조건문 사용)  (0) 2021.11.15
[Python]Dictionary  (0) 2021.11.15
[Python]Tuple  (0) 2021.11.14
[Python]조건문  (0) 2021.11.13
[Python]Return  (0) 2021.11.13