Python-basic: Difference between revisions
No edit summary |
|||
Line 100: | Line 100: | ||
print("Last number") | print("Last number") | ||
</source> | </source> | ||
== See also == | |||
* https://docs.python.org/3/tutorial/datastructures.html - python3 data structures | |||
== References == | == References == |
Revision as of 08:19, 17 June 2016
Overview
Python 프로그래밍 내용 정리
Data structures
list
다음의 method 들을 지원한다.
- list.append(x)
- list 의 마지막에 item 을 추가한다. a[len(a):] = [x] 와 같다.
- list.extend(L)
- L 의 모든 item 을 list 의 마지막에 추가한다. a[len(a):] = L 과 같다.
- list.insert(i, x)
- Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
- list.remove(x)
- Remove the first item from the list whose value is x. It is an error if there is no such item.
- list.pop([i])
- Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
- list.clear()
- Remove all items from the list. Equivalent to del a[:].
- list.index(x)
- Return the index in the list of the first item whose value is x. It is an error if there is no such item.
- list.count(x)
- Return the number of times x appears in the list.
- list.sort(key=None, reverse=False)
- Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).
- list.reverse()
- Reverse the elements of the list in place.
- list.copy()
- Return a shallow copy of the list. Equivalent to a[:].
<source lang=python>
</source>
sets
sets 는 리스트와 유사하지만, 중복된 개체를 허용하지 않는다는 점이 다르다.
단, 주의할 점은 빈 set 을 만들기 위해서는 set() 를 사용해야 한다는 점이다. {}를 사용하면 안된다. <source lang=python> >>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>> print(basket) # show that duplicates have been removed {'orange', 'banana', 'pear', 'apple'} >>> 'orange' in basket # fast membership testing True >>> 'crabgrass' in basket False
>>> # Demonstrate set operations on unique letters from two words ... >>> a = set('abracadabra') >>> b = set('alacazam') >>> a # unique letters in a {'a', 'r', 'b', 'c', 'd'} >>> a - b # letters in a but not in b {'r', 'd', 'b'} >>> a | b # letters in either a or b {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'} >>> a & b # letters in both a and b {'a', 'c'} >>> a ^ b # letters in a or b but not both {'r', 'd', 'b', 'm', 'z', 'l'} </source>
Statement
for
- 구구단 예제
<source lang=python> >>> for i in range(2,10): ... for j in range(1, 10): ... print(i*j, end=" ") ... print() </source>
while
while 문은 지정된 조건이 참일 경우에만 문장을 수행한다.
while <조건문>: <수행할 문장1> <수행할 문장2>
Sample <source lang=python> num = 0 while num < 10:
num = num +1 print("Check value. num[%d]" % num) if num == 10: print("Last number")
</source>
See also
- https://docs.python.org/3/tutorial/datastructures.html - python3 data structures
References
<references />