Created
February 8, 2017 05:14
-
-
Save saboyutaka/029eecffb3fc5a4b9d73ca3b4973fa9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## メソッド method\n", | |
"\n", | |
"* 処理をひとまとまりにして使いやすくする、再利用する。\n", | |
"* メソッドを利用する時は必ず `method_name()` のように最後に`()`が来る。引数がなくても必要。\n", | |
"* methodには引数と返り値というものがある。argumentsとreturn 。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"60\n", | |
"90\n" | |
] | |
} | |
], | |
"source": [ | |
"def total(arr):\n", | |
" total = 0\n", | |
" for n in arr:\n", | |
" total += n\n", | |
" return total\n", | |
"\n", | |
"print(total([10, 20, 30]))\n", | |
"print(total([10, 30, 50]))\n", | |
"# print(total(['a', 'b', 'c']))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"capitalize\n", | |
"casefold\n", | |
"center\n", | |
"count\n", | |
"encode\n", | |
"endswith\n", | |
"expandtabs\n", | |
"find\n", | |
"format\n", | |
"format_map\n", | |
"index\n", | |
"isalnum\n", | |
"isalpha\n", | |
"isdecimal\n", | |
"isdigit\n", | |
"isidentifier\n", | |
"islower\n", | |
"isnumeric\n", | |
"isprintable\n", | |
"isspace\n", | |
"istitle\n", | |
"isupper\n", | |
"join\n", | |
"ljust\n", | |
"lower\n", | |
"lstrip\n", | |
"maketrans\n", | |
"partition\n", | |
"replace\n", | |
"rfind\n", | |
"rindex\n", | |
"rjust\n", | |
"rpartition\n", | |
"rsplit\n", | |
"rstrip\n", | |
"split\n", | |
"splitlines\n", | |
"startswith\n", | |
"strip\n", | |
"swapcase\n", | |
"title\n", | |
"translate\n", | |
"upper\n", | |
"zfill\n" | |
] | |
} | |
], | |
"source": [ | |
"# strのmethodの一覧\n", | |
"for method in dir(str):\n", | |
" if not method.startswith('__'):\n", | |
" print(method)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# 使う時は、class_instance.method() のようにする。\n", | |
"s = 'Hello World'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"upper() 引数: なし、 返り値: str\n", | |
"HELLO WORLD\n" | |
] | |
} | |
], | |
"source": [ | |
"print('upper() 引数: なし、 返り値: str')\n", | |
"print(s.upper())\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"lower() 引数:なし 戻り値: str\n", | |
"hello world\n" | |
] | |
} | |
], | |
"source": [ | |
"print('lower() 引数:なし 戻り値: str')\n", | |
"print(s.lower())\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"replace() 引数:1: str, 2: str 戻り値: str\n", | |
"HellX WXrld\n" | |
] | |
} | |
], | |
"source": [ | |
"print('replace() 引数:1: str, 2: str 戻り値: str')\n", | |
"print(s.replace('o', 'X'))\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python [conda root]", | |
"language": "python", | |
"name": "conda-root-py" | |
}, | |
"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.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment