{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Pythonインタプリタ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 算術計算" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 2 # 加算" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 - 2 # 減算" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 * 5 # 乗算" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.4" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 / 5 # 除算" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 ** 2 # 累乗 (3の2乗)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### データ型" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(10)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(2.718)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(\"hello\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 変数" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "x = 10 # 初期化\n", "print(x) # x を出力する" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\n" ] } ], "source": [ "x = 100 # 代入\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "314.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = 3.14\n", "x * y" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x * y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## リスト" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5]\n" ] } ], "source": [ "a = [1, 2, 3, 4, 5] # リストの作成\n", "print(a) # リストの中身を出力する" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(a) # リストの長さを取得" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[4]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "a[5]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 99]\n" ] } ], "source": [ "a[4] = 99\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0:2] # インデックスの0番目から2番目まで" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 4, 99]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1:] # インデックスの1番目から最後まで" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:3] # 最初からインデックスの3番目まで" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:-1] # 最初から最後の要素の1つ前まで" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:-2] # 最初から最後の要素の2つ前まで" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ディクショナリ" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "180" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "me = {'height': 180} # ディクショナリを作成\n", "me['height'] # 要素にアクセス" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'height': 180, 'weight': 70}\n" ] } ], "source": [ "me['weight'] = 70 # 新しい要素を追加\n", "print(me)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ブーリアン" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hungry = True # お腹すいてる?\n", "sleepy = False # 眠い?\n", "type(hungry)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not hungry" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hungry and sleepy" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hungry or sleepy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## if文" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm hungry\n" ] } ], "source": [ "hungry = True\n", "if hungry:\n", " print(\"I'm hungry\")" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm not hungry\n", "I'm sleepy\n" ] } ], "source": [ "hungry = False\n", "if hungry:\n", " print(\"I'm hungry\")\n", "else:\n", " print(\"I'm not hungry\")\n", " print(\"I'm sleepy\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## for文" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for i in [1, 2, 3]:\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 関数" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\n" ] } ], "source": [ "def hello():\n", " print(\"Hello World!\")\n", "hello()" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello cat!\n" ] } ], "source": [ "def hello(object):\n", " print(\"Hello \" + object + \"!\")\n", "hello(\"cat\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.3" } }, "nbformat": 4, "nbformat_minor": 4 }