I used Python with the minimum necessary knowledge, but I bought a reference book and studied again to summarize what I learned new and what I found useful.

background

――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.

Usage

--Data processing --Data analysis

Summarized contents (updated from time to time)

――The order will be different.

That example that everyone writes on the first line

――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 ~~

Variadic argument

--Variable length arguments can be passed in two types, tuple type and dictionary type.

Tuple 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

Dictionary type

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

Decorator

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

Notation

assert conditional expression,message

Description

--If the conditional expression is not satisfied, the program is forcibly terminated. --Useful for testing and debugging programs.

raise

Notation

raise error class(message)

Description

--Intentionally generate an error. --Useful for testing and debugging programs.

with

Notation

with open('test,tsv', 'r') as f:
    print(f.write('test'))

Description

--When you exit the with statement, it will automatically do f.colse (). --The readability of the code is improved.

reference

Recommended Posts

I used Python with the minimum necessary knowledge, but I bought a reference book and studied again to summarize what I learned new and what I found useful.
A story that I wanted to do a function like before_action used in rails with django [Beginner learns python with a reference book]
I also tried to imitate the function monad and State monad with a generator in Python
I want to make a voice changer using Python and SPTK with reference to a famous site
What I did to welcome the Python2 EOL with confidence
I tried to solve the ant book beginner's edition with python
I tried to make a periodical process with Selenium and Python
I wanted to solve the ABC164 A ~ D problem with Python
I tried to get and analyze the statistical data of the new corona with Python: Data of Johns Hopkins University
What skills do I need to program with the FBX SDK Python?
order_by ('-created_at') ← What is "ー"? ?? ?? [Beginner learns python with a reference book]
I tried to streamline the standard role of new employees with Python
A story about converting HTML to PDF with WeasyPrint + matplotlib and embedding graphs [Beginners learn python with a reference book]
I tried it with SymPy Live, Wolfram Alpha and google with reference to "Algorithm learned with Python 4th: Prime numbers".