PY4E >> 중요하고 헷갈리는 개념들의 정리

2023. 12. 20. 02:56·Programming/Python

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
'Programming/Python' 카테고리의 다른 글
  • Review >> PY4E
  • 점프 투 파이썬 (9) 실습
  • 점프 투 파이썬 (8) 모듈, 패키지, 예외 처리, 라이브러리
  • 점프 투 파이썬 (7) 클래스
Ctrl_engineer
Ctrl_engineer
Ctrl 키는 혼자일 때보다 다른 키와 함께할 때 진짜 힘을 발휘합니다. 데이터도, 사람도 마찬가지입니다. 연결되고 흐를 때, 세상은 더 나은 방향으로 움직입니다. 저는 데이터의 흐름을 설계하고, 신뢰를 심는 엔지니어가 되고자 합니다. 이곳은, 그 여정의 작은 흔적들을 기록하는 공간입니다.
  • Ctrl_engineer
    Ctrl the flow
    Ctrl_engineer
  • 전체
    오늘
    어제
    • 분류 전체보기 (61)
      • Research (9)
        • Raspberry Pi (9)
      • Data Enginnering (24)
        • Cloud (3)
        • Elastic (6)
        • Database (9)
        • Pipeline (3)
      • CS STUDY (0)
        • Computer Science (0)
        • DataStructure & Algorithm (0)
      • Programming (13)
        • Python (13)
      • Mathematics and Statistics (10)
      • Data Science (3)
        • Data Insight (2)
        • Learning (0)
        • ML & DL (0)
      • DIARY (0)
      • TIL (Today I Learned) (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    라즈베리파이5
    climb-mates
    Statistics and Probability
    SQL
    shellyplugs
    heap size
    py4e
    3blue1brown
    spark
    점프투파이썬
    elasticSearch
    라즈베리파이 네트워크 설정
    티스토리챌린지
    오블완
    Khan Academy
    ssh 비밀번호 없이 접속
    Khan
    부스트코스
    linear algebra
    proxyjump 설정
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
Ctrl_engineer
PY4E >> 중요하고 헷갈리는 개념들의 정리
상단으로

티스토리툴바