――It was about four years ago that I first touched Python. (There is a blank and Python history is less than 2 years) ――At that time, there was no time to systematically learn because of the necessity in business, and it was a study method of deciphering the inherited code. ――I've done it with the minimum necessary knowledge so far, but when my colleague started studying Python, I decided to study with this as an opportunity. ――I'll expose my ignorance, but I hope it helps someone like me who started Python.
--Data processing --Data analysis
――The order will be different.
――You often see either of the following. (I was taught that it was magic. W)
- #!/usr/bin/python
- #!/usr/bin/env python
--This is the code that allows you to execute Python without python in front of the script.
--However, you need to grant execute permission to the script with chmod 744
. (The owner grants all permissions, and only the viewing permission for the group to which he belongs)
--I think it's a good idea to run / usr / bin / python
and / usr / bin / env python
on the terminal to see if you can call the intended Python.
sys.argv[0]
--sys.argv [0]
contains your script name.
--Example of results below.
test.py
import sys
print(sys.argv[0])
print(sys.argv[1])
terminal
$ python test.py 1
test.py
1
$ python ./test.py 1
./test.py
1
―― ~~ If you add information to the script name, you can use it as a split. ~~ -~~ It seems that it is not necessary to pass information as an argument in vain ~~
--Variable length arguments can be passed in two types, tuple type and dictionary type.
>>> def taple_func(*args):
... for i in args:
... print(i)
...
>>> args=(1,2,3,4)
>>> taple_func(*args)
1
2
3
4
>>> taple_func(1,2,3,4)
1
2
3
4
>>> def dict_func(**kwds):
... for i in kwds:
... print(i)
...
>>> kwds={'a':1, 'b':2, 'c':3, 'd':4}
>>> dict_func(**kwds)
b
d
a
c
>>> dict_func(a=1, b=2, c=3, d=4)
b
d
a
c
--A mechanism called qualifying a function.
>>> def decorator_a(seq_id):
... def _decorator_a(func):
... import functools
... @functools.wraps(func)
... def __decorator_a(*args,**kwargs):
... res = '<' + seq_id + '>'
... res = res + func(*args,**kwargs)
... res = res + '<' + seq_id + '>'
... return res
... return __decorator_a
... return _decorator_a
...
>>> @decorator_a('A')
... @decorator_a('B')
... def test():
... return 'decorator test'
...
>>> print(test())
<A><B>decorator test<B><A>
――I understand the mechanism, but I don't understand the usage scene.
assert
assert conditional expression,message
--If the conditional expression is not satisfied, the program is forcibly terminated. --Useful for testing and debugging programs.
raise
raise error class(message)
--Intentionally generate an error. --Useful for testing and debugging programs.
with
with open('test,tsv', 'r') as f:
print(f.write('test'))
--When you exit the with statement, it will automatically do f.colse ()
.
--The readability of the code is improved.
Recommended Posts