In this series, in recent years, we have introduced how to use Python, which attracts attention in AI, science and technology calculations, and statistical analysis.In the first place, why Python is attracting attention in such specialized fields is that there is a fulfilling library for scientific calculations.This time, I will introduce how to use Python with the finest calculator.
In the first time in this series, we introduced how to install Python3 (Anaconda).In an environment where Python is installed, run the "Python" command ("Python3" in MacOS in MacOS) at the Windows PowerShell, command prompts, and MacOS terminals, and the interactive Python execution environment will be launched.This execution environment is often called REPL, so it is also called REPL in this series.
Calculated by the dialogue -type execution environment (REPL) |
And REPL can be used as a high -performance calculator.However, after starting the terminal, it can be used for the first time by type "python (python3)", so if it is a one -time calculation, it will lose to the standard calculator application.However, of course, it is possible to calculate more than a calculator, so I do not care (I think) that one action will increase (I think).
例えば、1111×1111を計算してみよう。REPLを起動すると、「>>>」と表示される。これは、REPLに入力できることを表す記号だ。そこで、「>>>」に続けて、「1111 * 1111」と入力して[Enter]キーを押す。すると、以下のように表示される。
>>> 1111 * 1111 1234321
それで、REPLは計算を終え結果を表示すると、すぐに「>>>」と表示する。これは、再度別の計算を行わせることができることを意味する。
Earlier, Python introduced that a fairly large number of calculations can be done.Let's calculate the 1111 square of 1111 again.The calculation of the should be calculated is described as "V ** n" if it is a N -square of V.Let's execute the following calculations.Of course, so many numbers are listed.
>>> 1111 ** 1111 614213677620841231318558872403334094191..(省略)...
Then, to end the REPL, do the "quit ()" type.
By the way, in order to end the command line execution, the [Ctrl]+[C] key is to be pressed.Of course, the [Ctrl]+[C] key is valid to forcibly terminate the program executed in Python on the way.However, when the REPL is terminated, it is necessary to type "Quit ()".
However, REPL has a drawback.REPL has low visibility and low design to operate in the command line.Also, from my previous work experience, many people hate black screens.
What I want to use is "Idle (Python Shell)".This is a tool that will be installed at the same time when you install Python.To start iDlee, run the "IDLE" command ("Idle3" command in MacOS) in Windows.
How to use is almost the same as REPL.However, the execution result is colored, making it a little easier to see than the command line.To end the idle, press the closed button in the window.
Where the shell idle attached to Python is a calculator |
By the way, in the second time in this series, we introduced how to install Jupyter notebooks.The Jupyter notebook has a good design and is easy to use.And because it is basically a dialogue -type execution environment, it is perfect for using it as a calculator.
Above all, not only the calculation results, but also the notes that are the basis for calculation and calculation can be left.Because it can be easily stored in a file, it is perfect for calculating while thinking.
However, if you use it as a calculator, you will feel it is troublesome to start the Jupyter notebook.After launching PowerShell, you need to type "Jupyter Notebook".Therefore, it is a good idea to create a batch file so that you can easily start Jupyter.
If it is Windows, "Jupyter starts the following file.Let's save it under the name BAT.Then you can start by double -click when using Jupyter.
rem Jpyterノートブックを起動する jupyter notebook
I started the jupyter notebook from the batch file |
In MacOS, you can start jupyter by double -clicking.As a method, first, the following files are "jupyter-run..Save under the name "sh" and "chmod u+x jupyter-run" from the terminal..sh」で実行権限を付ける。次に、Finderから「情報を見る > このアプリケーションで開く」で、「ターミナル」を指定する。
#!/bin/sh jupyter notebook
Already, the detailed usage of Jupyter has been introduced in the second series, but here are some useful key operations when used as a calculator.
When Jupyter starts safely, specify "Python 3" to create a notebook.
New > Python 3で新規ノートブックを作ろう |
Next, write the formula in the text box (cell) of "IN [1]:", then press the execution button at the top of the screen.Then, the calculation result is displayed in OUT [1]:.
However, it is inefficient to press the execution button with the mouse every time.The Jupyter notebook provides shortcuts for various functions for heavy users.
After entering the formula in the cell, press the [Ctrl]+[Enter] key.Then, the formula is evaluated and the calculation result is displayed.Furthermore, after confirming the result, press the [B] key.Then, a new cell is added under the calculation result.
つまり、[Ctrl]+[Enter]キーでセルを評価、そして[b]キーで下にセルを追加する。うまくショートカットーキーを使えば、作業効率アップ間違いなしだ。なお、メニューの[Help > Keybord Shortcut]をクリックすると、いろいろな機能へのショートカットが表示される。
Finally, let's check the operating symbols and functions that are indispensable for using Python as a calculator.The first is a list of four arithmetic operations.
演算子 | 意味 |
---|---|
a + b | a と b を足す |
a - b | a から b を引く |
a * b | a に b を掛ける |
a / b | a を b を割る |
a // b | a を b で割って小数点以下を切り捨てる |
a % b | a を b で割った余りを求める |
a ** b | べき乗。aのb乗を求める |
In addition, the following embedded functions can be used as standard.
関数 | 意味 |
---|---|
abs(x) | xの絶対値を返す |
min(a,b,c...) | 引数の中で最も小さな値を返す |
max(a,b,c...) | 引数の中で最も大きな値を返す |
int(x) | 数値または文字列を整数にして返す |
round(v [,n]) | 値vの小数部をn桁に丸めた値を返す |
Also, if you describe "Import Math" at the beginning of the program, you can use the following various arithmetic functions.
関数 | 意味 |
---|---|
math.ceil(x) | 切り上げ |
math.gcd(a, b) | aとbの最大公約数を返す |
math.log(x [,base]) | xの自然対数(baseを底とする)を返す |
math.sqrt(x) | xの平方根を返す |
math.cos(x) | xの余弦を返す |
math.sin(x) | xの正弦を返す |
math.tan(x) | xの正接を返す |
The above function is an appropriate excerpt, and all functions are described in the following Python manual.Both are Japanese, so they are safe even if you are not good at English.
●組み込み関数について --- https://docs.python.jp/3/library/functions.html●mathモジュールについて --- https://docs.python.jp/3/library/math.htmlIn this way, if you use Python, which has various functions, you can easily do quite advanced calculations.The idea of using an interpreter -type programming language such as Python as a calculator has been around for a long time, but if you use Python, which has a library specialized in science and technology calculation and statistical processing, you can make more advanced calculations.Another advantage is that using Python as a calculator will make programming more familiar and improve programming skills.
自由型プログラマー。くじらはんどにて、プログラミングの楽しさを伝える活動をしている。代表作に、日本語プログラミング言語「なでしこ」 、テキスト音楽「サクラ」など。2001年オンラインソフト大賞入賞、2004年度未踏ユース スーパークリエータ認定、2010年 OSS貢献者章受賞。技術書も多く執筆している。