continue 구문
continue 구문은 현재 반복을 끝내고 루프의 시작으로 점프해서 다음 반복을 실행한다.

while 루프
불확정 루프이다. (=indefinite roop)
while 루프와 for 루프가 있을 때, 모두를 사용할 수 있고 모든 조건이 같다면 무엇을 선호할까?
for 루프이다. 유한되기 때문이다.con
None 활용
빈 값을 대입하고 싶을 때 숫자도 문자도 아닌 None을 지정할 수 있다.
예를 들면,
smallest = None
for value in [9, 41, 12] :
if smallest is None :
smallest = value
elif value < smallest :
smallest = value
is 와 is not 연산자
매우 강력해서 조심해서 사용해야 한다. 자료열의 형식과 값이 모두 동일해야 하기 때문이다.
'==' 보다 강력하다.
Immutable Spring
What would the following Python code print out?
fruit = 'Banana'
fruit[0] = 'b'
print(fruit)
Nothing would print - the program fails with a traceback error
리스트와 달리 문자열은 수정이 불가능하다.
내용 탐색
방법 1: startswith 사용하기
if line.startswith('From: ') :
print(line)
if line.startswith('From: ') :
print(line)
방법 2 : in 사용하기
for line in fhand :
line = line.rstrip()
if not '@uct.ac.za' in line :
continue
print(line)
이중으로 나누는 패턴
From stephen.marquard@uct.ac.za Sat Jan
words = line.split()
email = words[1]
pieces = email.split('@')
print(pieces[1])
list 와 Dictionary 의 차이
List : A linear collection of values that stay in order. (aka. Pringles)
Dictionary : A "bag" of values, each with its own label. (aka. Bag / Key=Post-it)
Dictionary 에서 get() 이용하기
counts = dict()
names = ['csev', 'cwen', 'csev', 'cwen']
for name in names :
counts[name] = counts.get(name, 0) + 1
print(counts)
숫자를 손쉽게 셀 수 있도록 해주는 method 이다.
튜플 내장함수
'count'와 'index'만 사용 가능하다.
임시 변수를 선언할 때 리스트를 쓰는 것보다 좋다.
Tuple Lists의 Sorting
sorted(d.items())
가장 많이 쓰이는 단어 열 개 찾기
counts = {}
1st = []
for line in fhand :
words = line.split()
for word in words :
counts[word] = counts.get(word, 0) + 1
for k, v in counts.items() :
newtup = (v, k)
1st.append(newtup)
1st = sorted(1st, reverse=True)
for v,k in 1st[:10] :
print(k, v)'Programming > Python' 카테고리의 다른 글
| Review >> PY4E (1) | 2023.12.20 |
|---|---|
| 점프 투 파이썬 (9) 실습 (1) | 2023.12.05 |
| 점프 투 파이썬 (8) 모듈, 패키지, 예외 처리, 라이브러리 (1) | 2023.12.04 |
| 점프 투 파이썬 (7) 클래스 (3) | 2023.12.03 |
| 점프 투 파이썬 (6) 입출력 (2) | 2023.12.03 |