[GO] Python functions ~ max, min, sum, len, all, any

Introduction

In this article, I'd like to cover the list-related functions that are often used in paiza's skill check. We will cover six items: max, min, sum, len, all, and any. Let's go.

max The max function gets the maximum value in the list.


numbers = [23, 25, 21, 20, 26, 28, 22, 29, 27, 24, 30]

print(max(numbers))

# 30

I was able to get the maximum value in the list in an instant.

Let's also look at the strings.


names = ["B", "K", "C", "Z", "A"]

print(max(names))

# Z

For strings I was able to get Z using the max function. In the alphabet, it's easier to understand if you think that the back is bigger.

min The min function gets the minimum value in the list.


numbers = [23, 25, 21, 20, 26, 28, 22, 29, 27, 24, 30]

print(min(numbers))

# 20

I was able to get the minimum value in the list in an instant.

Let's also look at the strings.


names = ["B", "K", "C", "Z", "A"]

print(min(names))

# A

For strings, I was able to get A using the max function. In the alphabet, it's easier to understand if you catch it as small as it is in front.

sum The sum function outputs the sum of the numbers not in the list.

numbers = [23, 25, 21, 20, 26, 28, 22, 29, 27, 24, 30]

print(sum(numbers))
# 275

In this way, it outputs the total of the numbers not listed.

There is a caveat here. An error will occur if any of the elements in the list contains at least one string.

numbers = ['23', 25, 21, 20, 26, 28, 22, 29, 27, 24, 30]

print(sum(numbers))

#TypeError: unsupported operand type(s) for +: 'int' and 'str'

Make sure all the elements in the list are numbers before using them.

len The len function outputs the number of elements in the list.

numbers = [23, 25, 21, 20, 26, 28, 22, 29, 27, 24, 30]

print(len(numbers))
# 11

It outputs the number of elements in the list like this.

all The all function returns True if all the elements in the list are True, and False if there is at least one False. It's hard to understand by explaining the words, so let's take a look at the code.

numbers = [1, 0, 1, 1, 1, 1, 1]

print(all(numbers))
# False
numbers = [1, 1, 1, 1, 1, 1, 1]

print(all(numbers))
# True
words = ["B", "K", "C", "Z", "A"]

print(all(words))
# True
print(all([])

# True

In this way, it was confirmed that False is returned when everything is other than True.

any The all function returns True if all the elements in the list are True, and False if there is at least one False.

numbers = [0, 1, 0, 0, 0, 0, 0]

print(any(numbers))
# True
numbers = [0, 0, 0, 0, 0, 0, 0]

print(any(numbers))
# False

I was able to confirm that if there is even one that is True, it will return True.

Finally

This time we've covered list-related functions. In the future, I would like to improve how to use paiza for skill checks. Also, I will post more Python functions in the future, so I hope you find it helpful.

Recommended Posts

Python functions ~ max, min, sum, len, all, any
[Python] Let's master all and any
Python functions
[python] Check the elements of the list all, any
[Beginner] Python functions
Python Easy-to-use functions
Python basics: functions