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.
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
>>>
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'>
>>>
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']
>>>
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