Not surprisingly known! ?? What about the arguments of built-in functions? What school are you from? [Python]

Summary of this article

Introducing arguments that seem to be less well known personally and arguments that are specified in vain in Python's built-in functions.

There is no girlfriend or school in Python.

Operating environment

CPython 3.8.1

range

Official documentation: https://docs.python.org/ja/3/library/functions.html#func-range

According to the official, "range is not a function but an immutable sequence type", but since it is in the function list, it will be treated as a function.

step

The range function is often written as range (start, stop), but there is a step argument.

The default is set to 1.

You can use it when you want only odd numbers or even numbers.

>>> for i in range(1, 10, 2):
...     print(i)
...
1
3
5
7
9
>>>

print

Official documentation: https://docs.python.org/ja/3/library/functions.html#print

It's a function that everyone has used. It will output it.

sep

By default, one half-width space is set.

You can specify print (1, 2, 3) and the characters that will be separated when multiple objects are passed as arguments.

You don't have to type " {arbitrary string} ". join () just to print.

>>> print("Higuchi", "Aiba")
Higuchi Aiba
>>> print("Higuchi", "Aiba", sep="When")
Higuchi and Aiba
>>>

end

\ N is set by default

When using printf in C language etc., I write \ n at the end, but it makes it unnecessary to write it.

I feel that there is almost no use. I've never used it, but I know.

>>> print("Kaede Higuchi")
Kaede Higuchi
>>> print("Kaede Higuchi", end="Mr.")
Kaede Higuchi>>>

file

By default, sys.stdout is set.

By specifying a stream such as a file, it will be output to the specified destination.

Is it a simple log to use? I get angry in all directions when I use the logging module. I think it's okay to do some stdout.write (). I don't know the difference.

>>> with open("hoge.txt", "w") as f:
...     print("Uihara", file=f)
...
>>> with open("hoge.txt") as f:
...     print(f.read())
...
Uihara

>>>

It would be nice to use end here. There was a use.

flush

False is set by default.

Although sys.stdout is not done, it seems that it is generally buffered when outputting to other places. I don't know much in detail.

Is it used every time flush is specified when another stream is specified in the file argument?

Normally, the buffer is flushed and written at the timing of f.close (), but when flush = True, it is written at the timing when print is executed.

>>> f = open("fuga.txt", "w")
>>> print("Deron Deron Konderon", file=f, flush=True)
>>> f.close()
>>>

enumerate

Official documentation: https://docs.python.org/ja/3/library/functions.html#enumerate

It is a function that returns index together when iterable object such as list is turned by for.

start

The default is set to 0.

The usage is when you want to start the index from any number. Recommended for those who increase the index by 1 every time.

>>> for index, item in enumerate(["Higuchi", "Aiba", "Suzuka", "Akabane", "County road", "Yumetsuki"], start=1):
...     print(index, item)
...
1 Higuchi
2 Aiba
3 Suzuka
4 Akabane
5 county road
6 Yumetsuki
>>>

min, max

Official documentation (min): https://docs.python.org/ja/3/library/functions.html#min Official documentation (max): https://docs.python.org/ja/3/library/functions.html#max

It is used when you want to get the maximum and minimum values.

key

By default, None is set.

You can specify what to key compare with min and max.

The usage is when you want to compare the value of dict.

>>> d = {"a": 100, "b": 50, "c": 10}
>>> max(d)
'c'
>>> max(d, key=d.get)
'a'
>>> min(d)
'a'
>>> min(d, key=d.get)
'c'
>>>

open

Official documentation: https://docs.python.org/ja/3/library/functions.html#open

Used when opening a file.

mode

By default, r is set.

I think everyone has used it. This is the argument with the argument ranking number 1 that is specified in vain.

I don't have to specify ʻopen (file," r ")`, but ...

pow

Official documentation: https://docs.python.org/ja/3/library/functions.html#pow

It will calculate the power.

mod

By default, None is set.

It returns the remainder of the {first argument} raised to the {second argument} power divided by the number specified by mod.

Is it the time to use it for big calculations?

>>> pow(5, 5, 10)
5
>>>

round

Official documentation: https://docs.python.org/ja/3/library/functions.html#round

Use when you want to round a decimal.

ndigits

The default is set to None.

You can specify any number of digits after the decimal point.

>>> round(1.3333333333, 2)
1.33
>>>

sum

Official documentation: https://docs.python.org/ja/3/library/functions.html#sum

If all the iterable objects are int type, the total can be calculated.

start

The default is set to 0.

It can be counted from any number.

I don't know where to use it.

>>> sum([1, 2, 3, 4, 5], start=10)
25
>>>

It's a small story, but if all the inside is str type and start is also str type, TypeError will appear, but use ''. Join (seq) instead! I will tell you with a message.

I've never seen such a gentle error message.

>>> sum(["b", "c", "d"], start="a")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sum() can't sum strings [use ''.join(seq) instead]
>>>

type

Official documentation: https://docs.python.org/ja/3/library/functions.html#type

I use it when I don't know what the type of this object is.

bases, dict

There is no default.

~~ You can make something like a class. ~~

** Addendum (from Comment): ** ** A class object is created for those defined in the class syntax, and a class object can also be created with the type function. ** **

You need to specify the class to be inherited by bases with tuple and specify the instance variable with dict.

There seems to be no place to use it. Can it be used when playing something tied up?

>>> x = type("abc", (), dict(a=1, b=2, c=3))
>>> x
<class '__main__.abc'>
>>>

Extra: random.choices

Official documentation: https://docs.python.org/ja/3/library/random.html#random.choices

A function that receives a sliceable object (str type, sequence type) passed as the first argument, randomly selects it, and returns it.

k

The default is set to 1.

You can choose the number to be selected at random. Eliminates the need to write " ". Join ([random.choice (string.ascii_letters) for i in range (10)]) when generating a random string.

>>> import random
>>> import string
>>> random.choices(string.ascii_letters, k=10)
['C', 'H', 'I', 'O', 'a', 'G', 's', 'E', 'f', 'c']
>>>

Summary

How was it? (Beat

Like this, there are quite a few lesser-used arguments in the built-in functions alone.

You can get a lot of (not all) knowledge by reading the official documentation.

It's not that it's strong because you know the arguments, and it's weak because you don't know it, but if you have a lot of knowledge, you may gain something, so I think it's worth knowing.

I am still immature, so please feel free to point out any mistakes in the explanation in the text.

Recommended Posts

Not surprisingly known! ?? What about the arguments of built-in functions? What school are you from? [Python]
[Python] What are the two underscore (underscore) functions before?
Not surprisingly known !? Python standard library functions groupby
About the matter that the contents of Python print are not visible in docker logs
About the ease of Python
What beginners learned from the basics of variables in python
About the features of Python
Consider what you can do with Python from the Qiita article
[Python] What is @? (About the decorator)
Existence from the viewpoint of Python
About the basics list of Python basics
Extension of Python by C or C ++ (when there are multiple arguments, when passing a list from the Python side)
Learning notes from the beginning of Python 1
About the virtual environment of python version 3.7
About the development environment you are using
About the arguments of the setup function of PyCaret
Learning notes from the beginning of Python 2
What are the characteristics of an AV actress? I guessed from the title of the work! (^ _ ^) / ~~
About the problem that the python version of Google App Engine does not mesh
What you want to memorize with the basic "string manipulation" grammar of python
About the handling of ZIP files including Japanese files when upgrading from Python2 to Python3
How to return to the command from the state where you can not enter interactive mode with python of git bash
Get the contents of git diff from python
Check the type of the variable you are using
The contents of the Python tutorial (Chapter 5) are itemized.
When do python functions affect the caller's arguments?
The contents of the Python tutorial (Chapter 4) are itemized.
The contents of the Python tutorial (Chapter 2) are itemized.
The contents of the Python tutorial (Chapter 8) are itemized.
The contents of the Python tutorial (Chapter 1) are itemized.
The contents of the Python tutorial (Chapter 10) are itemized.
A note about the python version of python virtualenv
What are you comparing with Python is and ==?
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
The contents of the Python tutorial (Chapter 6) are itemized.
The contents of the Python tutorial (Chapter 3) are itemized.
Useful Python built-in functions that you use occasionally
About the * (asterisk) argument of python (and itertools.starmap)
What are you using when testing with Python?
What happens if you graph the number of views and ratings/comments of the video of "Flag-chan!" [Python] [Graph]
What happens if you graph the video of the "Full power avoidance flag-chan!" Channel? [Python] [Graph]
What I thought about in the entrance exam question of "Bayesian statistics from the basics"
What to do if the progress bar is not displayed in tqdm of python
What to do if Python does not switch from the System version in pyenv