{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Python\n", "### สำหรับหลักการเชิงตัวเลข ###\n", "\n", "* Open-Source Object-Oriented interpret since 1980s\n", "* รองรับหลายระบบปฏิบัติการ Linux, Windows, OSX, FreeBSD, etc. \n", "* เขียนง่าย อ่านง่าย\n", "* มี package เสริมมากมาย สำหรับหลากหลายด้าน\n", "* Apps, games, graphics, data analysis, Big data, machine learning, etc. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Downloads\n", "\n", "* [Anaconda](https://www.continuum.io/downloads) python3 + numpy + matplotlib + jupyter + ...\n", "\n", " * https://repo.continuum.io/archive/Anaconda3-4.2.0-MacOSX-x86_64.pkg\n", " * https://repo.continuum.io/archive/Anaconda3-4.2.0-Windows-x86_64.exe\n", " * https://repo.continuum.io/archive/Anaconda3-4.2.0-Linux-x86_64.sh\n", "\n", "* Linux\n", " * installation\n", " ```sh\n", " bash Anaconda3-4.2.0-Linux-x86_64.sh\n", " ``` \n", " * start\n", " ```sh\n", " mkdir NumericalMethod/\n", " cd NumericalMethod/\n", " source ~/anaconda3/bin/activate ~/anaconda3\n", " ```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Getting Start\n", "```sh\n", "$ ipython3\n", "```\n", "\n", "```python\n", "# comment\n", "name = input(\"What's your name? \")\n", "print(\"Hello, \"+name)\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name? วิชิต สมบัติ\n" ] } ], "source": [ "name = input(\"name? \")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5922444" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "name = \"5922444\"\n", "int(name)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# ประเภทของข้อมูล\n", "\n", "* ข้อความ - `str`\n", "```python\n", "'Conor McGregor'\n", "\"Cody Garbrandt\"\n", "\"\"\" หลายๆ บรรทัด \n", "บรรทัดต่อมา\n", "บรรทัดสุดท้าย \"\"\"\n", "```\n", "* ตัวเลข - `int`, `float`, `complex`\n", "```python\n", "20\n", "int('12')\n", "30.0\n", "float('3.54')\n", "complex(10, 1)\n", "```\n", "* List - `list`, `tuple`\n", "```python\n", "[1, 3, 4, 5, 7]\n", "(2, 1, 4)\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "# ประเภทของข้อมูล\n", "\n", "* ประพจน์ - bool\n", "```python\n", "True\n", "False\n", "```\n", "* Dictionary - `dict`\n", "```python\n", "{\n", " 'key1': value,\n", " 'name': 'Jack',\n", " 'age': 25\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# ตัวแปร\n", "** identifier / name **\n", "> สายอักขระที่ประกอบด้วย a-z, A-Z, 0-9, _, แต่ตัวแรกต้องไม่เป็น 0-9 \n", "```python\n", "i = 20\n", "x = 3.0\n", "name = 'Conor A. McGregor'\n", "u = [ 2.0, 3.5, 4.0, 5.0 ]\n", "t = ( 1.0, 2.0, 3.0 )\n", "kv = { 1: 1.0, 2: 4.0, 3: 27.0, 4: 256.0 }\n", "```" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "i = 20\n", "x = 3.0\n", "name = 'Conor A. McGregor'\n", "u = [ 2.0, 3.5, 4.0, 5.0 ]\n", "t = ( 1.0, 2.0, 3.0 )\n", "kv = { 1: 1.0, 2: 4.0, 3: 27.0, 4: 256.0 }" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i.real" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# ตัวดำเนินการทางคณิตศาสตร์\n", "\n", "```python\n", "x = 5 + 9 # บวก\n", "y = 9 - 4 # ลบ\n", "z = x * y # คูณ\n", "z = 9 / 2 # หาร\n", "z = x ** 2.5 # ยกกำลัง\n", "z = 100 % y # mod (เศษที่ได้จากการหาร)\n", "```" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10//4" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Augmented assignment operators\n", "\n", "```python\n", "x += 5 # x = x + 5\n", "y -= 9 # y = y - 9\n", "z *= x # z = z * x\n", "z /= 9 # z = z / 9\n", "z **= x # z = z ** x \n", "z %= y # z = z % y\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "# เครื่องหมายเปรียบเทียบ\n", "| เครื่องหมาย | ความหมาย |\n", "|:---------:|-------------------|\n", "| < | น้อยกว่า |\n", "| > | มากกว่า |\n", "| <= | น้อยกว่าหรือเท่ากับ |\n", "| >= | มากกว่าหรือเท่ากับ |\n", "| == | เท่ากับ |\n", "| != | ไม่เท่ากับ |\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "# เครื่องหมายเปรียบเทียบ\n", "* Logical\n", "```python\n", "2 < x and x < 10 # &&\n", "2 < x or x > 10 # ||\n", "2 < x < 10\n", "```\n", "\n", "* เพิ่มเติม\n", "```python\n", "a = [2,4,6,8]\n", "```\n", "\n", "```python\n", "2 in a\n", "1 not in a\n", "len(a) is 4\n", "```" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [2,4,6,8]\n", "len(a) is 6" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Control Structure\n", "## `if`\n", "```python\n", "if len(a) > 0:\n", " a[0] = 9\n", " a[1] *= a[0]\n", "```\n", "## `if-elif`\n", "```python\n", "if 1 in a:\n", " a[0] = 9\n", "elif 2 in a:\n", " a[1] = 2\n", "else:\n", " a[2] = 3 \n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "# `If-else`\n", "* regular `if-else`\n", "```python\n", "if 1 in a:\n", " print('a contains 1.')\n", "else:\n", " print(\"a doesn't contains 1\")\n", "```\n", "* one-liner\n", "```python\n", "print('a contains 1.' if 1 in a else \"a doesn't contains 1.\")\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "# Note on block\n", "python บังคับให้จัด block ให้เป็นระเบียบ โดยใช้ tab\n", "\n", "* java\n", "```java\n", "if (objList.contains(obj)) {\n", " objList.remove(obj);\n", " obj.consume();\n", "}\n", "```\n", "* python\n", "```python\n", "if obj in objList: \n", "```\n", "> objList.remove(obj)\n", "> \n", "> obj.consume()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Control Structure\n", "## `while`\n", "```python\n", "while เงื่อนไข:\n", " statements\n", "```\n", "* ตัวอย่าง\n", "```python\n", "x = 1\n", "y = 10\n", "while x != y:\n", " x += 1\n", " y -= 1\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Control Structure\n", "## `for`\n", "```python\n", "a = [1, 2, 3, 4, 5, 6]\n", "for x in a:\n", " print(x**2)\n", "\n", "for i in range(10):\n", " print(i)\n", " \n", "for i in range(1,10,2):\n", " print(i)\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Utility Functions\n", "```python\n", "abs(-10.0) # ค่าสัมบูรณ์\n", "max( [1,3,2,4,5] ) # ค่าสูงสุดใน list\n", "min( [2,3,9,7,10,12] ) # ค่าต่ำสุดใน list\n", "round(12345.12345, 3) # ปรับให้เหลือ 3 ตำแหน่ง\n", "cmp(a, b) # ab == 1\n", "eval('20.932') # แปลงข้อความเป็นตัวเลข\n", "type(a) # ดูชนิดข้อมูลของตัวแปร a\n", "\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# IO Functions\n", "* เปิดไฟล์เพื่ออ่านข้อความในไฟล์\n", "```python\n", "with open('file.txt', 'r') as f:\n", " lines = f.readlines()\n", " print(lines)\n", "```\n", "* เปิดไฟล์เพื่อเขียนข้อความ\n", "```python\n", "with open('file.txt', 'w') as f:\n", " f.write('text in line one\\n')\n", " f.writelines(['text in line two\\n', 'line 3\\n', 'line 4\\n'])\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Function\n", "\n", "* defining function\n", "```python\n", "def timestable(x):\n", " for i in range(1,13):\n", " print('{}x{}={}'.format(i,x,i*x))\n", "```\n", "\n", "* calling function\n", "```python\n", "timestable(9)\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Modules\n", "* regular import\n", "```python\n", "import math\n", "x = math.pi\n", "math.cos(x)\n", "```\n", "* function import\n", "```python\n", "from math import pi, cos\n", "cos(pi)\n", "```\n", "* rename import\n", "```python\n", "import math as m\n", "m.cos(m.pi)\n", "```" ] } ], "metadata": { "anaconda-cloud": {}, "celltoolbar": "Slideshow", "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.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }