{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 基本的な計算" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pythonことはじめ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "文字列はシングルクォーテーション(`'`)、またはダブルクォーテーション(`\"`)で囲む。出力するには`print()`を用いる。改行文字`'\\n'`を末尾に付けなくても改行される。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n" ] } ], "source": [ "print('Hello, world!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Jupyter Notebookなどの対話的シェルでは、最後に書かれた式を評価した結果が出力される。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello, world!!!'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello, world!!!'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`に続く記述はコメントとして扱われ、実行されない。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n" ] } ], "source": [ "# 'Hello, world!'という文字列を表示する。\n", "print('Hello, world!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 四則演算\n", "\n", "加算は`+`、減算は`-`、乗算は`*`、除算は`/`である。除算`/`の計算結果は浮動小数点数となる。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9 + 2" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9 - 2" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9 * 2" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.5" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9 / 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "除算の結果を整数で求めたいときは`//`を使う。" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9 // 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "演算子`//`の間に空白を入れると文法エラー(syntax error)となる。" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (333318335.py, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"/tmp/ipykernel_2497/333318335.py\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 9 / / 2\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "9 / / 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "剰余算(割ったときの余り)は`%`で求める。" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9 % 2" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 % 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "累乗は`**`で求める。" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "128" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ** 7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "演算子`**`の間に空白を入れると文法エラーとなる。" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (3091056297.py, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"/tmp/ipykernel_2497/3091056297.py\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 2 * * 7\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "2 * * 7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "計算(演算子)の優先順位は数学の慣習に近い。優先順位は`**` > `-` (負の数を表現) > `*`, `/`, `//`, `%` > `+`, `-`" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.5" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 + 7 / -2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "括弧`(` `)`を使って演算子の優先順位を変更できる。" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-5.0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(3 + 7) / -2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 浮動小数点数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "数に小数点が含まれると、その数は自動的に浮動小数点数で表現される(浮動小数点数を評価したので、出力に小数点が付いている)。" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "指数を用いた浮動小数点 $a \\times 10^b$ は`e`を用いて表現できる(C言語と同様)。例えば、$6.02 \\times 10^{23}$は、" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6.02e+23" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6.02e23" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$1.0 \\times 10^{-4}$は、" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0001" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1e-4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "式の中に浮動小数点数が含まれる場合、計算結果も浮動小数点数になる。" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-7.0" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "-9.0 + 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "浮動小数点数に対して`//`を使うと、結果は整数ではなく浮動小数点数となる。" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-5.0" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "-9.0 // 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2進数と16進数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2進数は先頭に`0b`を付ける。" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "44" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0b00101100" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "16進数は先頭に`0x`を付ける。" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "44" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0x2C" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "remove-cell" ] }, "source": [ "---\n", "\n", "[Python早見帳](https://chokkan.github.io/python/) © Copyright 2020-2022 by [岡崎 直観 (Naoaki Okazaki)](https://www.chokkan.org/). この作品はクリエイティブ・コモンズ 表示 - 非営利 - 改変禁止 4.0 国際 ライセンスの下に提供されています。\"クリエイティブ・コモンズ・ライセンス\"" ] } ], "metadata": { "@context": { "CreativeWork": "http://schema.org/CreativeWork", "Organization": "http://schema.org/Organization", "Person": "http://schema.org/Person", "author": "http://schema.org/author", "copyrightHolder": "http://schema.org/copyrightHolder", "copyrightYear": "http://schema.org/copyrightYear", "license": "http://schema.org/license", "name": "http://schema.org/name", "title": "http://schema.org/name", "url": "http://schema.org/url" }, "@type": "CreativeWork", "author": [ { "@type": "Person", "name": "Naoaki Okazaki", "url": "https://www.chokkan.org/" } ], "copyrightHolder": [ { "@type": "Person", "name": "Naoaki Okazaki", "url": "https://www.chokkan.org/" } ], "copyrightYear": 2022, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" }, "license": "https://creativecommons.org/licenses/by-nc-nd/4.0/deed.ja", "title": "Python早見帳" }, "nbformat": 4, "nbformat_minor": 4 }