Python Easy-to-use functions

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

__ 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

Functions of the imported module

__ 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]

Function definition

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.

reference

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

Python Easy-to-use functions
Python functions
#Python basics (functions)
[Beginner] Python functions
Python basics: functions
Python Beginner's Guide (Functions)
Python basic course (12 functions)
[Python] Memo about functions
# 4 [python] Basics of functions
Python built-in functions ~ Zip ~
Curry arbitrary functions with Python ....
Python> lambda> tiny functions / callback functions
Getting Started with Python Functions
Python3 programming functions personal summary
Python
Study from Python Hour3: Functions
Overriding library functions in Python
Keyword arguments for Python functions
Python for super beginners Python #functions 1
Python 3 sorted and comparison functions
Python functions learned in chemoinformatics
Python higher-order functions and comprehensions
[python] Manage functions in a list
About python dict and sorted functions
5 Easy-to-Use Python Tools | Increase Work Efficiency
Using global variables in python functions
10 functions of "language with battery" python
Dynamically define functions (methods) in Python
kafka python
Python3> Functions> Function Renaming Mechanism> f = fib
[Python3] Dynamically define global variables in functions
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
python tips
python function ①
Python basics
Python memo
Conquer 69 Python built-in functions 6th p ~ r
ufo-> python (3)
install python
Python Singleton
Python basics ④
Python Memorandum 2
[Introduction to Udemy Python3 + Application] 55. In-function functions
python memo
Easily use your own functions in Python
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
Pharmaceutical company researchers summarized functions in Python
python tips
Installing Python 3.4.3.
Try python