- 인터프리터 기반의 객체지향 언어
- 플랫폼에 구애받지 않는 언어
- 동적 타이핑 방식의 언어
- 리플렉션을 지원하는 언어
- 확장성이 뛰어난 언어
,
로 구분을 해줌list_1 = ['abc', 123, 3.14, ['edf', 456], ('gh', 'st')]
list_1
['abc', 123, 3.14, ['edf', 456], ('gh', 'st')]
list_1 = ['abc', 123, 3.14, ['edf', 456], ('gh', 'st')]
list_1
['abc', 123, 3.14, ['edf', 456], ('gh', 'st')]
del list_1
list_1
list_1
['abc', 123, 3.14, ['edf', 456], ('gh', 'st')]
list_1[1]
123
list_1[1] = 111
list_1
['abc', 111, 3.14, ['edf', 456], ('gh', 'st')]
list_1 = ['abc', 123, 3.14, ['edf', 456], ('gh', 'st')]
list_1
['abc', 123, 3.14, ['edf', 456], ('gh', 'st')]
list_1[0]
'abc'
list_1[0:3]
['abc', 123, 3.14]
list_1[3:]
[['edf', 456], ('gh', 'st')]
list_1 = ['abc', 123, 3.14, ['edf', 456], ('gh', 'st')]
list_1
['abc', 123, 3.14, ['edf', 456], ('gh', 'st')]
list_1[-1]
('gh', 'st')
list_1[-3:-1]
[3.14,['edf', 456]]
설명 | 파이썬 표현 | 결과 |
---|---|---|
리스트 길이 (length) | len([1,2,3]) | 3 |
리스트 합치기 (concatenation) | [1,2,3] + [4,5,6] | [1,2,3,4,5,6] |
반복 (repetition) | [1,2,3]*3 | [1,2,3,1,2,3,1,2,3] |
소속 여부 (membership) | 1 in [1,2,3] 4 in [1,2,3] | True False |
for loop 반복 (iteration) | for x in [1,2,3]: print (x); | 1 23 |
+
연산자*
연산자for loop
문list1 = [1,2,3]
list2 = ['a', 'b', 'c', 'd']
len(list1)
3
len(list2)
4
list1 = [1,2,3]
max(list1)
3
list2 = ['a', 'b', 'c', 'd']
max(list2)
'd'
list1 = [1,2,3]
min(list1)
1
list2 = ['a', 'b', 'c', 'd']
min(list2)
'a'
tup = ('aaa', 'bbb', 'ccc') #tuple
tup
('aaa', 'bbb', 'ccc')
list_tup = list(tup)
list_tup
['aaa', 'bbb', 'ccc']
type(list_tup)
<class 'list'>
list1 = [1,2,3]
list2 = ['a', 'b', 'c', 'd']
list3 = [1,2,3]
list1 == list2
False
list1 == list3
True
list1 = [1, 2, 'a', 'b']
list1.append('c')
list1
[1, 2, 'a', 'b', 'c']
append()
메소드를 사용할 때는 괄호 안에 추가하려는 요소를 1개만 써야 함list2 = [1, 2, 'a', 'b', 'c']
list3 = [3, 3, 4, 'd', 'd']
list3.extend(list2)
list3
[3, 3, 4, 'd', 'd', 1, 2, 'a', 'b', 'c']
list3 = [3, 3, 4, 'd', 'd', 'd', 'e']
list3.count(3)
2
list.count('d')
3
list4 = [1, 2, 'a', 'b', 'c', 'a']
list4.index('a')
2
list4.index('c')
4
list5 = [1, 2, 'a', 'b', 'c']
list5
[1, 2, 'a', 'b', 'c']
list.insert(3, 'kkkk')
list5
[1, 2, 'a', 'kkkk', 'b', 'c']
list6 = [1, 2, 'a', 'b', 'c']
list6.pop()
'c'
list6
[1, 2, 'a', 'b']
list6 = [1,2,'a','b','c']
list.pop(2)
'a'
list6
[1,2,'b','c']
list1 = [1,2,'a','b','c']
list1.remove(1)
list1
[2,'a','b','c']
list1.remove('a')
list1
[2,'b','c']
remove()
메소드는 괄호 안에 단 1개의 argument 만을 사용list1 = [1,2,'a','b','c']
list1.reverse()
list1
['c','b','a',2,1]
list1 = [3,1,9,4,2,8]
list1.sort()
list1
[1,2,3,4,8,9]
sort()
메소드의 디폴트는 오름차순이다list1 = [3,1,9,4,2,8]
list1.sort(reverse=True)
list1
[9,8,4,3,2,1]
list.sort(reverse=True)
처럼 옵션을 설정해주면 됨list1_1 = ['c','a','b']
list1_1.sort()
list1_1
['a','b','c']
sort()
메소드를 적용하면 TypeError 가 발생https://rfriend.tistory.com/329?category=695562
https://rfriend.tistory.com/330