티스토리 뷰
sum 함수
sum 함수에 딕셔너리를 파라미터로 넘기면 key들의 합을 리턴합니다.
int_list = [1, 2, 3, 4, 5]
int_tuple = (4, 3, 6, 1, 2)
int_dict = {1: "one", 2: "two", 3: "three"}
print(sum(int_list)) # => 15
print(sum(int_tuple)) # => 16
print(sum(int_dict)) # => 6
ternary expression
불린(Boolean) 값에 따라 다른 값을 리턴하는 구문을 ternary expression이라고 합니다. ternary expression을 사용하면 if, else로 복잡하게 표현해야 하는 구문을 간단하게 나타낼 수 있습니다.
condition = True
if condition:
condition_string = "nice"
else:
condition_string = "not nice"
print(condition_string) # => nice
condition = True
condition_string = "nice" if condition else "not nice"
print(condition_string) # => nice
def set(self, new_value):
"""
파라미터가 0 이상, 최댓값 미만이면 value에 설정한다.
아닐 경우 value에 0을 설정한다.
"""
self.value = new_value if 0 <= new_value < self.limit else 0
# if new_value >= 0 and new_value < self.limit:
# self.value = new_value
# else:
# self.value = 0
list comprehension
list comprehension은 새로운 리스트를 만드는 간편한 방법입니다.
int_list = [1, 2, 3, 4, 5, 6]
squares = []
for x in int_list:
squares.append(x**2)
print(squares) # [1, 4, 9, 16, 25, 36]
int_list = [1, 2, 3, 4, 5, 6]
squares = [x**2 for x in int_list]
print(squares) # [1, 4, 9, 16, 25, 36]
zfill 메소드
이 메소드는 문자열을 최소 몇 자리 이상을 가진 문자열로 변환시켜줍니다. 이때 만약 모자란 부분은 왼쪽에 “0”을 채워주는데요. 예를 들어 만약 "1".zfill(2)을 하면 "01"을 리턴합니다.
print("1".zfill(6))
print("333".zfill(2))
print("a".zfill(8))
print("ab".zfill(8))
print("abc".zfill(8))
000001
333
0000000a
000000ab
00000abc
'Python' 카테고리의 다른 글
파이썬 소수점 이하 자리수 0으로 맞추기 (0) | 2021.12.06 |
---|---|
slicing, startswith (0) | 2021.12.06 |
객체지향 - 여러가지 메서드 (0) | 2021.11.29 |
조건문이 들어있는 List comprehension 으로 for문과 if문을 한번에 처리하기 (0) | 2021.10.25 |
2차원 리스트 대각선으로 뒤집기 (0) | 2021.10.25 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- intj여자
- 깃허브계정2개
- 브왈라
- 개발자도서추천
- 개발언어추천
- ssafy6기
- 싸피6기
- 상업용무료폰트
- 개발자책추천
- 임대차3법
- 맥과윈도우로깃허브
- 개발도서추천
- SSAFY
- 클린코드
- 디즈니얼굴
- 폰트추천
- 개발자
- 한글무료폰트추천
- 코딩도서
- ssafy합격후기
- 싸피
- 깃허브계정
- 개발자커리
- 개발언어순위
- 개발자로드맵
- 무료폰트추천
- 클린코더
- ssafy후기
- ssafy결과
- 폰트
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함