코드워즈 문제를 풀다가 모르는 부분들을 간단하게 정리해보려고 한다.
Formatting
파이썬을 사용하다 보면 출력 형식을 많이 제어해야되는 경우가 생긴다. 그럴 경우에 보통 formatting을 사용한다.
이번에 문제를 풀면서 사용했던 포매팅은 string format()이다.
format() method는 특정 값을 포맷하고 string의 placeholder 안에 그 값을 넣어주고 formatted string을 return한다.
placeholder는 {} 이런 형식으로 사용된다.
syntax) string.format(value1, value2...)
예를 들어서 이런식으로 사용하면 된다.
def high_and_low(numbers):
number = numbers.split(' ')
int_list = list(map(int, number))
return('{max} {min}'.format(max=max(int_list), min=min(int_list)))
Map
리스트의 요소를 지정된 함수로 처리한다. 여러개의 데이터를 한번에 다른 데이터의 형태로 바꿀때 사용한다.
예를 들면, 위의 예시 코드 처럼 number 리스트를 int로 바꾸기 위해서 사용한다.
for in 반복문
여러개의 값 혹은 리스트에서 값을 하나씩 꺼내서 실행한다.
예시
def get_count(sentence):
c_sentence = sum(sentence.count(x) for x in ('a', 'e', 'i', 'o', 'u'))
return c_sentence
sentence의 x 값(aeiou)을 카운트 해서 더한 값이 c_sentence이다.
Boolean type: bool
True와 False를 나타내는 자료형이다.
'L'etude' 카테고리의 다른 글
Javascript: 단항연산자++, 배열리터럴 (0) | 2021.09.08 |
---|---|
Javascript: == , === / 삼항 연산자 (0) | 2021.09.07 |
Javascript: 함수, 매개변수 parameter (0) | 2021.07.14 |
Dispatch, useSelector(), fragment (0) | 2021.07.14 |
Redux, Redux Toolkit, Reducers, actions (0) | 2021.07.13 |
댓글