[Python] 파이썬 1부터 n 까지의 합 구하기

소개

  • 안녕하세요. 오늘은 파이썬에서 최대값, 최소값을 찾는 방법에 대해서 알아 보려고 합니다.
  • 특정 리스트에 정수형 숫자들이 있고, 해당 리스트에서 가장 큰 값과, 작은 값을 추출하는 예제 코드를 작성해 보겠습니다.

예제코드

# 최대값
def max(list):
    list_count = len(list)
    maxValue = list[0]

    for index in range(0, list_count):
        if list[index] > maxValue:
            maxValue = list[index]
    return maxValue

# 최소값
def min(list):
    list_count = len(list)
    minValue = list[0]

    for index in range(0, list_count):
        if list[index] < minValue:
            minValue = list[index]
    return minValue

list = [4, 2, 3, 5, 12, 544, 231, 563, 1, -2, -56]

print("최대값 : %d" %max(list))
print("최소값 : %d" %min(list))

실행 결과

최대값 : 563
최소값 : -56
728x90

이 글을 공유하기

댓글

Designed by JB FACTORY