Recently, I'm learning Python.
I'm still learning the basics, but I've learned more functions, so I'll summarize them as a memorandum.
__ Built-in functions = Functions that can be used as Python functions from the beginning __
We will use some built-in functions for the list below.
nums = [100, 200, 500, 1000, 500]
sum () → Returns the total number
print(sum(nums))
→ 2300
max () → returns the maximum value
print(max(nums))
→ 1000
min () → returns the minimum value
print(min(nums))
→ 100
len () → Returns the number of elements (number of characters)
print(len(nums))
→ 5
str () → Convert number to string
print(str(nums))
→ [100, 200, 500, 1000, 500] #Output as a character string
__ Module = File containing Python definitions and statements __
You can use the defined functions by importing the module.
Use the random module as an example.
import random
nums = [199, 288, 56, 82, 99, 1, 538, 499]
randint (n, m) → Returns an integer in the specified range (n ~ m) at random
print(random.randint(1, 100))
→ 17
choice () → Returns one element in the specified list at random
random.choice(nums)
→ 288
shuffle () → Randomly returns the order of the specified list
random.shuffle(nums)
→ [56, 499, 538, 199, 99, 288, 1, 82]
Defined in the def statement.
As an example, I will make my own function to calculate the average. (Although the average value can be calculated by mean () in the statistics module.)
def average(nums):
return sum(nums) / len(nums)
nums = [10, 100, 30, 43, 57, 34, 90, 76]
result = average(nums)
print(result)
→ 55.0
Only the indented part is processed by the function.
https://docs.python.org/ja/3/library/functions.html https://docs.python.org/ja/3/library/numeric.html https://docs.python.org/ja/3/library/random.html
Recommended Posts