Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# string & print fuction"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:02:02.383542Z",
- "start_time": "2019-07-05T14:02:02.369578Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hello, world!\n",
- "Hello, world!\n"
- ]
- }
- ],
- "source": [
- "print(\"Hello, world!\")\n",
- "print('Hello, world!')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 37,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:14:07.729480Z",
- "start_time": "2019-07-05T14:14:07.724517Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0123456789\n",
- "0\n"
- ]
- }
- ],
- "source": [
- "s = '0123456789'\n",
- "print(s)\n",
- "print(s[0])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 36,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:14:02.005765Z",
- "start_time": "2019-07-05T14:14:02.002740Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0123456\n",
- "789\n"
- ]
- }
- ],
- "source": [
- "print(s[:7])\n",
- "print(s[7:])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:14:41.380763Z",
- "start_time": "2019-07-05T14:14:41.376774Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "9\n",
- "789\n",
- "0123456\n"
- ]
- }
- ],
- "source": [
- "print(s[-1])\n",
- "\n",
- "print(s[-3:])\n",
- "print(s[:-3])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 43,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:15:20.891823Z",
- "start_time": "2019-07-05T14:15:20.887825Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0123456789\n"
- ]
- }
- ],
- "source": [
- "s1 = '012345'\n",
- "s2 = '6789'\n",
- "print(s1 + s2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# if"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:04:53.270628Z",
- "start_time": "2019-07-05T14:04:53.267635Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Yes\n"
- ]
- }
- ],
- "source": [
- "a = 10\n",
- "\n",
- "if a is 10:\n",
- " print('Yes')\n",
- "else:\n",
- " print('No')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:05:01.301839Z",
- "start_time": "2019-07-05T14:05:01.297851Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Yes\n"
- ]
- }
- ],
- "source": [
- "a = 10\n",
- "\n",
- "if a == 10:\n",
- " print('Yes')\n",
- "else:\n",
- " print('No')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:05:06.653557Z",
- "start_time": "2019-07-05T14:05:06.650565Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Yes\n"
- ]
- }
- ],
- "source": [
- "a = 10.55\n",
- "\n",
- "if a == 10.55:\n",
- " print('Yes')\n",
- "else:\n",
- " print('No')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 160,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T15:13:27.176536Z",
- "start_time": "2019-07-05T15:13:27.172546Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "No\n"
- ]
- }
- ],
- "source": [
- "a = 10.55\n",
- "\n",
- "if a is 10.55:\n",
- " print('Yes')\n",
- "else:\n",
- " print('No')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 159,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T15:13:25.309230Z",
- "start_time": "2019-07-05T15:13:25.306207Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Yes\n"
- ]
- }
- ],
- "source": [
- "a = 'python'\n",
- "\n",
- "if a is 'python':\n",
- " print('Yes')\n",
- "else:\n",
- " print('No')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 158,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T15:13:22.000877Z",
- "start_time": "2019-07-05T15:13:21.995889Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Yes\n"
- ]
- }
- ],
- "source": [
- "a = 'python'\n",
- "\n",
- "if a == 'python':\n",
- " print('Yes')\n",
- "else:\n",
- " print('No')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 154,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T15:12:54.737936Z",
- "start_time": "2019-07-05T15:12:54.734909Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Yes\n"
- ]
- }
- ],
- "source": [
- "a = None\n",
- "\n",
- "if a is None:\n",
- " print('Yes')\n",
- "else:\n",
- " print('No')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 157,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T15:13:17.836663Z",
- "start_time": "2019-07-05T15:13:17.832674Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Yes\n"
- ]
- }
- ],
- "source": [
- "a = None\n",
- "\n",
- "if a == None:\n",
- " print('Yes')\n",
- "else:\n",
- " print('No')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 155,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T15:13:01.884652Z",
- "start_time": "2019-07-05T15:13:01.880662Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "No\n"
- ]
- }
- ],
- "source": [
- "a = None\n",
- "\n",
- "if None:\n",
- " print('Yes')\n",
- "else:\n",
- " print('No')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# + - * / %"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:07:39.079192Z",
- "start_time": "2019-07-05T14:07:39.075205Z"
- }
- },
- "outputs": [
- {
- "ename": "SyntaxError",
- "evalue": "invalid syntax (<ipython-input-17-fd7858b5020a>, line 3)",
- "output_type": "error",
- "traceback": [
- "\u001b[1;36m File \u001b[1;32m\"<ipython-input-17-fd7858b5020a>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m a ++\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
- ]
- }
- ],
- "source": [
- "a = 1\n",
- "print(a)\n",
- "a ++"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:10:15.021490Z",
- "start_time": "2019-07-05T14:10:15.017502Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2\n"
- ]
- }
- ],
- "source": [
- "a = 1\n",
- "a += 1\n",
- "print(a)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:07:26.110306Z",
- "start_time": "2019-07-05T14:07:26.107314Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "19\n"
- ]
- }
- ],
- "source": [
- "a = 20\n",
- "a -= 1\n",
- "print(a)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:08:13.259955Z",
- "start_time": "2019-07-05T14:08:13.256960Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "5.0\n",
- "5\n"
- ]
- }
- ],
- "source": [
- "a = 10\n",
- "a = a / 2\n",
- "\n",
- "print(a)\n",
- "print(int(a))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 128,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:56:26.036475Z",
- "start_time": "2019-07-05T14:56:26.031489Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "5\n",
- "5.0\n"
- ]
- }
- ],
- "source": [
- "a = 10\n",
- "a = a // 2\n",
- "\n",
- "print(a)\n",
- "print(float(a))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 129,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:57:05.869745Z",
- "start_time": "2019-07-05T14:57:05.866753Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "20\n"
- ]
- }
- ],
- "source": [
- "a = 10\n",
- "a = a * 2\n",
- "print(a)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 130,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:57:06.483103Z",
- "start_time": "2019-07-05T14:57:06.479115Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "100\n"
- ]
- }
- ],
- "source": [
- "# pow\n",
- "\n",
- "a = 10\n",
- "a = a ** 2\n",
- "print(a)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 131,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:57:09.864065Z",
- "start_time": "2019-07-05T14:57:09.860077Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "100.0\n"
- ]
- }
- ],
- "source": [
- "# pow\n",
- "\n",
- "a = 10.0\n",
- "a = a ** 2\n",
- "print(a)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 132,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:57:23.498791Z",
- "start_time": "2019-07-05T14:57:23.494801Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1\n",
- "1.0\n"
- ]
- }
- ],
- "source": [
- "# mod\n",
- "\n",
- "a = 10\n",
- "print(a % 3)\n",
- "\n",
- "a = 10.0\n",
- "print(a % 3)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 140,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:59:56.413875Z",
- "start_time": "2019-07-05T14:59:56.409919Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2\n",
- "2.0\n",
- "1.0\n",
- "1.0\n",
- "25\n",
- "25.0\n"
- ]
- }
- ],
- "source": [
- "print(1 + 1)\n",
- "print(1.0 + 1)\n",
- "\n",
- "print(5 / 5) # *****\n",
- "print(5.0 / 5)\n",
- "\n",
- "print(5 * 5)\n",
- "print(5.0 * 5)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# not"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 61,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:20:34.620587Z",
- "start_time": "2019-07-05T14:20:34.616606Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n",
- "False\n"
- ]
- }
- ],
- "source": [
- "a = True\n",
- "\n",
- "print(a)\n",
- "print(not a)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 59,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:20:32.259551Z",
- "start_time": "2019-07-05T14:20:32.255541Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "No\n"
- ]
- }
- ],
- "source": [
- "a = False\n",
- "\n",
- "if a:\n",
- " print('Yes')\n",
- "else:\n",
- " print('No')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 64,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:20:55.832261Z",
- "start_time": "2019-07-05T14:20:55.829273Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Yes\n"
- ]
- }
- ],
- "source": [
- "a = True\n",
- "\n",
- "if a is (not False):\n",
- " print('Yes')\n",
- "else:\n",
- " print('No')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 63,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:20:54.054418Z",
- "start_time": "2019-07-05T14:20:54.050430Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Yes\n"
- ]
- }
- ],
- "source": [
- "a = True\n",
- "\n",
- "if a is not False:\n",
- " print('Yes')\n",
- "else:\n",
- " print('No')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Input"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 49,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:17:45.135771Z",
- "start_time": "2019-07-05T14:17:44.251136Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Enter: 7\n",
- "<class 'str'> 7\n"
- ]
- }
- ],
- "source": [
- "num = input('Enter: ')\n",
- "print(type(num), num)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 50,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:17:46.630272Z",
- "start_time": "2019-07-05T14:17:45.647578Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Enter: 7\n",
- "<class 'int'> 7\n"
- ]
- }
- ],
- "source": [
- "num = int(input('Enter: '))\n",
- "print(type(num), num)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 51,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:17:47.820032Z",
- "start_time": "2019-07-05T14:17:47.070242Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Enter: 7\n",
- "<class 'float'> 7.0\n"
- ]
- }
- ],
- "source": [
- "num = float(input('Enter: '))\n",
- "print(type(num), num)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# for"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 65,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:21:59.058138Z",
- "start_time": "2019-07-05T14:21:59.054122Z"
- }
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "range(0, 10)"
- ]
- },
- "execution_count": 65,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "range(10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 66,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:22:07.074757Z",
- "start_time": "2019-07-05T14:22:07.070731Z"
- }
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
- ]
- },
- "execution_count": 66,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "list(range(10))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 67,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:22:18.282889Z",
- "start_time": "2019-07-05T14:22:18.278899Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0\n",
- "1\n",
- "2\n",
- "3\n",
- "4\n",
- "5\n",
- "6\n",
- "7\n",
- "8\n",
- "9\n"
- ]
- }
- ],
- "source": [
- "for i in range(10):\n",
- " print(i)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 68,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:22:26.288043Z",
- "start_time": "2019-07-05T14:22:26.283057Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0 1 2 3 4 5 6 7 8 9 "
- ]
- }
- ],
- "source": [
- "for i in range(10):\n",
- " print(i, end=' ')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 70,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:22:51.423004Z",
- "start_time": "2019-07-05T14:22:51.417020Z"
- }
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
- ]
- },
- "execution_count": 70,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "[i for i in range(10)]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 69,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:22:43.246446Z",
- "start_time": "2019-07-05T14:22:43.241460Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0 1 2 3 4 5 6 7 8 9\n"
- ]
- }
- ],
- "source": [
- "print(*[i for i in range(10)])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 71,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:23:10.222165Z",
- "start_time": "2019-07-05T14:23:10.218176Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0\n",
- "1\n",
- "2\n",
- "3\n",
- "4\n",
- "5\n",
- "6\n",
- "7\n",
- "8\n",
- "9\n"
- ]
- }
- ],
- "source": [
- "print(*[i for i in range(10)], sep='\\n')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# while"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 73,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:25:07.200314Z",
- "start_time": "2019-07-05T14:25:07.196325Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0 1 2 3 4 5 6 7 8 9 "
- ]
- }
- ],
- "source": [
- "i = 0\n",
- "while i < 10:\n",
- " print(i, end=' ')\n",
- " i += 1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 75,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:25:36.396400Z",
- "start_time": "2019-07-05T14:25:36.392409Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0 1 2 3 4 5 6 7 8 9 "
- ]
- }
- ],
- "source": [
- "# do-while\n",
- "\n",
- "i = 0\n",
- "while True:\n",
- " print(i, end=' ')\n",
- " i += 1\n",
- " \n",
- " if i >= 10:\n",
- " break"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# list"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 78,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:29:10.683470Z",
- "start_time": "2019-07-05T14:29:10.680478Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
- "<class 'list'>\n"
- ]
- }
- ],
- "source": [
- "l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
- "print(l)\n",
- "print(type(l))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 79,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:29:21.575321Z",
- "start_time": "2019-07-05T14:29:21.571332Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0\n",
- "0123456\n",
- "789\n"
- ]
- }
- ],
- "source": [
- "print(l[0])\n",
- "print(s[:7])\n",
- "print(s[7:])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 80,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:29:26.777007Z",
- "start_time": "2019-07-05T14:29:26.774017Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "9\n",
- "789\n",
- "0123456\n"
- ]
- }
- ],
- "source": [
- "print(s[-1])\n",
- "\n",
- "print(s[-3:])\n",
- "print(s[:-3])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 88,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:37:02.761168Z",
- "start_time": "2019-07-05T14:37:02.757178Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3]\n",
- "[1, 2, 3]\n"
- ]
- }
- ],
- "source": [
- "l1 = [1, 2, 3]\n",
- "print(l1)\n",
- "\n",
- "l2 = l1\n",
- "print(l2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 90,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:37:25.945273Z",
- "start_time": "2019-07-05T14:37:25.942253Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2, 3]\n",
- "[2, 3]\n"
- ]
- }
- ],
- "source": [
- "l2.remove(1)\n",
- "\n",
- "print(l1)\n",
- "print(l2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 91,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:37:48.744341Z",
- "start_time": "2019-07-05T14:37:48.739357Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3]\n",
- "[1, 2, 3]\n"
- ]
- }
- ],
- "source": [
- "l1 = [1, 2, 3]\n",
- "print(l1)\n",
- "\n",
- "l2 = l1.copy()\n",
- "print(l2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 92,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:37:53.634445Z",
- "start_time": "2019-07-05T14:37:53.630456Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3]\n",
- "[2, 3]\n"
- ]
- }
- ],
- "source": [
- "l2.remove(1)\n",
- "\n",
- "print(l1)\n",
- "print(l2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 94,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:43:15.866660Z",
- "start_time": "2019-07-05T14:43:15.862670Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2]\n",
- "[1, 2, 3]\n"
- ]
- }
- ],
- "source": [
- "l = [1, 2]\n",
- "print(l)\n",
- "\n",
- "l.append(3)\n",
- "print(l)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 95,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:43:28.214474Z",
- "start_time": "2019-07-05T14:43:28.210486Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2]\n",
- "[1, 2, 3]\n"
- ]
- }
- ],
- "source": [
- "l = [1, 2]\n",
- "print(l)\n",
- "\n",
- "l += [3]\n",
- "print(l)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 96,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:44:12.911787Z",
- "start_time": "2019-07-05T14:44:12.907800Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2]\n",
- "[555, 2]\n"
- ]
- }
- ],
- "source": [
- "l = [1, 2]\n",
- "print(l)\n",
- "\n",
- "l[0] = 555\n",
- "print(l)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# tuple"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 98,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:44:38.668679Z",
- "start_time": "2019-07-05T14:44:38.657708Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "(1, 2)\n"
- ]
- },
- {
- "ename": "TypeError",
- "evalue": "'tuple' object does not support item assignment",
- "output_type": "error",
- "traceback": [
- "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
- "\u001b[1;32m<ipython-input-98-fed3ccab398e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0ml\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ml\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0ml\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m555\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
- ]
- }
- ],
- "source": [
- "l = (1, 2)\n",
- "print(l)\n",
- "l[0] = 555"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# dict"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 143,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T15:01:36.206173Z",
- "start_time": "2019-07-05T15:01:36.202150Z"
- }
- },
- "outputs": [],
- "source": [
- "d = {\n",
- " 'A': 1,\n",
- " 'B': 2,\n",
- " 'C': 3\n",
- "}"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 144,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T15:01:39.953717Z",
- "start_time": "2019-07-05T15:01:39.950725Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'A': 1, 'B': 2, 'C': 3}\n"
- ]
- }
- ],
- "source": [
- "print(d)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 145,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T15:01:51.853721Z",
- "start_time": "2019-07-05T15:01:51.850697Z"
- }
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 145,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'A' in d"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 146,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T15:02:01.276025Z",
- "start_time": "2019-07-05T15:02:01.272035Z"
- }
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "False"
- ]
- },
- "execution_count": 146,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'D' in d"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 147,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T15:02:22.935111Z",
- "start_time": "2019-07-05T15:02:22.932119Z"
- }
- },
- "outputs": [],
- "source": [
- "d['D'] = 4"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 148,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T15:02:26.885948Z",
- "start_time": "2019-07-05T15:02:26.882956Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'A': 1, 'B': 2, 'C': 3, 'D': 4}\n"
- ]
- }
- ],
- "source": [
- "print(d)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 149,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T15:02:34.317531Z",
- "start_time": "2019-07-05T15:02:34.313540Z"
- }
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 149,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'D' in d"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 150,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T15:02:45.231152Z",
- "start_time": "2019-07-05T15:02:45.227164Z"
- }
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "1"
- ]
- },
- "execution_count": 150,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "d['A']"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# def"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 87,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:34:46.448084Z",
- "start_time": "2019-07-05T14:34:46.443101Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "12\n"
- ]
- }
- ],
- "source": [
- "def foo(num):\n",
- " print(num + 5)\n",
- "\n",
- "\n",
- "foo(7)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 85,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:34:07.306928Z",
- "start_time": "2019-07-05T14:34:07.302939Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "12\n"
- ]
- }
- ],
- "source": [
- "def foo(num):\n",
- " return num + 5\n",
- "\n",
- "\n",
- "result = foo(7)\n",
- "print(result)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# file - read"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 115,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:49:39.193462Z",
- "start_time": "2019-07-05T14:49:39.188510Z"
- }
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['bar.txt', 'foo.txt']"
- ]
- },
- "execution_count": 115,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# list .txt file\n",
- "\n",
- "from glob import glob\n",
- "glob('*.txt')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 118,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:51:02.107240Z",
- "start_time": "2019-07-05T14:51:02.103252Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hello, world!\n"
- ]
- }
- ],
- "source": [
- "file = open('foo.txt')\n",
- "\n",
- "text = file.read()\n",
- "print(text)\n",
- "\n",
- "file.close()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "with open('foo.txt', 'r') as file:\n",
- " text = file.read()\n",
- " print(text)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 123,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:54:04.828063Z",
- "start_time": "2019-07-05T14:54:04.824074Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hello, world!\n"
- ]
- }
- ],
- "source": [
- "with open('foo.txt') as file:\n",
- " text = file.read()\n",
- " print(text)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# file - write"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 125,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:54:24.207691Z",
- "start_time": "2019-07-05T14:54:24.200710Z"
- }
- },
- "outputs": [
- {
- "ename": "TypeError",
- "evalue": "write() argument must be str, not int",
- "output_type": "error",
- "traceback": [
- "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
- "\u001b[1;32m<ipython-input-125-284e7bf509ca>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'foo.txt'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'w'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mfile\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mtext\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m555555555555\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mfile\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[1;31mTypeError\u001b[0m: write() argument must be str, not int"
- ]
- }
- ],
- "source": [
- "with open('foo.txt', 'w') as file:\n",
- " text = 555555555555\n",
- " file.write(text)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 126,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:54:30.858253Z",
- "start_time": "2019-07-05T14:54:30.853235Z"
- }
- },
- "outputs": [],
- "source": [
- "with open('foo.txt', 'w') as file:\n",
- " text = 555555555555\n",
- " file.write(str(text))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 127,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2019-07-05T14:54:38.569137Z",
- "start_time": "2019-07-05T14:54:38.564152Z"
- }
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "555555555555\n"
- ]
- }
- ],
- "source": [
- "# read\n",
- "\n",
- "with open('foo.txt') as file:\n",
- " text = file.read()\n",
- " print(text)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "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.6.5"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement