코딩박사/python

[13회차] Python List (Array)

잇잇쌤 2023. 2. 14. 09:18
728x90
반응형
SMALL

안녕하세요!

잇잇쌤입니다~ 그동안 진도가 많이 늦었는데, 3월까지 반복문/제어문/함수를 꾸준히 나갔으면 좋겠네요

'

나름 코딩박사, 유튭 해본다고 이것 저것 해보다보니 , 블로그에는 정작 소홀하지 않았나, 깊은 반성을 해봅니다.

자 본론으로 들어가서, 우리가 파이썬을 배우는 이유는 바로 Tuple 과 List를 모르면 , 파이썬을 모른다고 할수 있을 정도로, 굉장히 중요한 부분입니다!?

 

그래서 열심히 오늘도 해보도록 하겠습니다!

 

Python Lists

c언어나 java로 따지면 사실 배열이거든요!

저는 설명할 때 기차길로 설명합니다.

 

용산행 열차가 들어섭니다~~

1-1 1-2 2-1 2-2 3-1 3-2 4-1 4-2 5-1 5-2
3명 2명 5명 2명 7명 8명 9명 4명 2명 3명

이렇게 지하철 칸 번호안에 몇명이 들었는지 데이터를 구조화 해서 볼 수 있는데요,

이렇게 만들어진게 바로 리스트라고 보시면 됩니다.

thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)

위의 과일을 thislist 에 넣는다고하면, thislist 를 출력하면 전부가 다 출력이 되겠죠!

 

그러면, cherry 만 출력하고 싶을경우?

0 1 2 3 4
사과 바나나 체리 사과 체리

2번째에 해당하는 것과 4번째에 해당하는 것을 출력하면 되겠죠!

print(thislist[2])

print(thislist[4])

이렇게 출력하면 되겠습니다.

 

여기서 전체 몇개의 값이 있는지 확인하려면? 바로 끝에 len()을 붙여주면 됩니다.

print(len(thislist))

그리고, 문자열 외 숫자 boolean 까지 다양한 데이터 타입이 담기게 됩니다.

list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
list1 = ["abc", 34, True, 40, "male"]

len 말고도 type이라는 걸로 데이터의 타입을 확인할 수 있어요.

mylist = ["apple", "banana", "cherry"]
print(type(mylist))
thislist = list(("apple", "banana", "cherry")) # note the double round-brackets
print(thislist)

 

 

그렇다면, 파이썬 배열은

어떤것들이 있을까요? 다음과 같이 list외, tuple, set, dictionary 가 있습니다.

Python Collections (Arrays)

There are four collection data types in the Python programming language:

  • List is a collection which is ordered and changeable. Allows duplicate members.
  • Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
  • Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
  • Dictionary is a collection which is ordered** and changeable. No duplicate members.

단 ,주의사항이 dictionary 는 3.7 부터는 순서가 있는데, 3.6 이전부터는 정렬 없이 데이터가 담기게 됩니다.

**As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

 

 

728x90
반응형