{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 変数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 基本\n", "\n", "代入文を書くと変数が作成され、その変数に値がセットされる。型は自動的に推定されるので、変数作成時に型を明示しなくてもよい(Python 3.5以降では[型ヒント](https://docs.python.org/ja/3/library/typing.html)を使って変数の型を明示することが可能)。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "x = 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "このように作成された変数は、Pythonのインタプリタが終了するまで保持される。変数の値を確認するには、その変数を評価させればよい。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "変数の型は[type](https://docs.python.org/ja/3/library/functions.html#type)関数で確認できる。整数は[int](https://docs.python.org/ja/3/library/stdtypes.html#numeric-types-int-float-complex)型で表現される。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$x \\leftarrow x + 1$といった変数の値の更新にも、代入演算子`=`が使える。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "x = x + 1" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "上の更新処理は次のように書くこともできる(`+=`演算子はC言語にもあるが、C言語の`++`演算子に相当するものはない)。" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "x += 1" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "同様に、`-=`, `*=`, `/=`という演算子が用意されている。なお、以下のコードでは変数の値の更新と、更新後の変数の評価(表示)を一つのセルで行っている。" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x -= 2\n", "x" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x *= 2\n", "x" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x /= 3\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "浮動小数点型の変数を作成するには、値を小数点付きで表現すればよい。" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 1.5\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "浮動少数点数は[float](https://docs.python.org/ja/3/library/stdtypes.html#numeric-types-int-float-complex)型で表現される。" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "浮動小数点型から整数型に変換するには、[int](https://docs.python.org/ja/3/library/functions.html#int)関数を用いる。以下の例では`x`の値が切り捨てられる。" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "整数型から浮動小数点型に変換するには、[float](https://docs.python.org/ja/3/library/functions.html#float)関数を用いる。" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "変数(オブジェクト)の削除は[del](https://docs.python.org/ja/3/reference/simple_stmts.html#the-del-statement)文で行う。ただ、Pythonでは使わなくなったメモリ領域は自動的に開放される(ガーベージコレクション)ため、明示的にdel文で変数を削除することは稀である。" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "del x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "変数`x`を削除すると、評価できなくなる。" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "NameError", "evalue": "name 'x' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_2526/32546335.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'x' is not defined" ] } ], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 変数の再評価" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$x = 1$として$y \\leftarrow x^2 + 2x + 1$を計算してみる。" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "x = 1.\n", "y = x ** 2 + 2 * x + 1" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "変数`x`の値を`2`に変更してから`y`を評価しても、変数`y`の値は再計算されない(元の値から変化しない)。" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "x = 2" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "再計算をするには、変数`y`の代入文を再度実行する必要がある。" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = x ** 2 + 2 * x + 1\n", "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 複数の変数への代入" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "一行で複数の変数にまとめて値を代入できる(厳密には後で説明するタプル型を介した代入である)。" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "x, y = 3, 1" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "変数の値の入れ替えも簡単に書ける(一時的な変数を使わずに書ける)。" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "x, y = y, x" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "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 }