본문 바로가기
developStudy

[Python] Sum

by holaf 2021. 9. 11.
반응형

sum 함수

1. sum함수의 인자는 iterable한 데이터타입이어야 하고, 숫자여야합니다. 

sum(iterable, /, start=0)
Sums start and the items of an iterable from left to right and returns the total. The iterable’s items are normally numbers, and the start value is not allowed to be a string.

 

파이썬 공식문서는 iterable을 이렇게 설명합니다: An object capable of returning its members one at a time.  그 외에도 무언가 구구절절 써있는데, 핵심은 '보유한 값을 하나씩 반환해주는' 기능을 가진 데이터타입을 의미합니다. 반복문을 돌렸을때 보유한 값을 하나씩 출력해주면 iterable입니다. 그 대표적인 예시는 list와 str입니다.

 

for i in [1,2]:
	print(i)
    
#
1
2

for i in 'dog':
	print(i)
    
#
d
o
g

 

sum함수에 수를 넣으면 에러가 납니다.

sum(1,2)
#TypeError

sum([1,2])
#3

li=[]

for i in range(1,3):
    li.append(i)

#li=[1,2]
#sum(li)=3

sum(range(1,3))
#3

sum 함수를 써서 풀 수 있는 문제

2021.01.17 - [solve] - 두 정수의 합

 

두 정수의 합

*매일 코딩 문제 1개 풀고, 관련 함수 기본 익히기 챌린지! #range #sum programmers.co.kr/learn/courses/30/lessons/12912 코딩테스트 연습 - 두 정수 사이의 합 두 정수 a, b가 주어졌을 때 a와 b 사이에 속한..

yiyudesign.tistory.com

 

반응형

'developStudy' 카테고리의 다른 글

[Python] Slicing, Set, Sorted  (0) 2021.09.12
[Python] Range  (0) 2021.09.11
[Python] Map, Lambda, Zip  (0) 2021.02.18
[Javascript] 상도덕편 🚮  (0) 2021.02.16
앱 버전 쓰는 방법 (Semantic Versioning)  (0) 2021.01.19

댓글