Skip to content

Instantly share code, notes, and snippets.

@phwt
Last active June 26, 2019 13:03
Show Gist options
  • Save phwt/4749e6cc67f26122db020742e3324d5c to your computer and use it in GitHub Desktop.
Save phwt/4749e6cc67f26122db020742e3324d5c to your computer and use it in GitHub Desktop.
Python-T1
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "markdown",
"source": "# Built-in Functions ที่ควรรู้\n\n## `abs(args)`\nส่งออกเป็นค่าสัมบูรณ์ของค่าที่รับเข้าไป\n\n**Input** - `args` เป็น `float` หรือ `int`\n\n**Output** - `float` หรือ `int` เป็นค่าสัมบูรณ์ของค่านั้นๆ"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "abs(-12)",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "12"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "abs(-112.44)",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "112.44"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "----\n## `divmod(args1, args2)`\nหาร `args1` ด้วย `args2` และส่งออกเป็นผลหารและเศษ\n\n**Input**\n* `args1` - เศษ (`float` หรือ `int`)\n* `args2` - ส่วน (`float` หรือ `int`)\n\n**Output** - `tuple` ในรูปแบบ `(ผลหาร, เศษ)`"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "divmod(4,2)",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "(2, 0)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "divmod(7,2)",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "(3, 1)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "-----\n## `float(args)`"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "แปลง `args` เป็น `float`\n\n**Input** - `args` (ตัวเลขหรือ string ที่อยู่ในรูปแบบตัวเลข)\n\n**Output** - ค่าที่ใส่เข้าไปในรูปแบบ `float`"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "float(\"417.762\")",
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "417.762"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "float(15)",
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "15.0"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "float(5.56)",
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "5.56"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "float(\"history of the entire world, i guess\") #ไม่สามารถแปลงได้",
"execution_count": 13,
"outputs": [
{
"output_type": "error",
"ename": "ValueError",
"evalue": "could not convert string to float: 'history of the entire world, i guess'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-13-e76ee52c402a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"history of the entire world, i guess\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#ไม่สามารถแปลงได้\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: could not convert string to float: 'history of the entire world, i guess'"
]
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "-----\n## `input(args)`\nรับค่าในรูปแบบ `string`\n\n**Input** - `args` ค่าที่ผู้ใช้กำหนด\n\n**Output** - ค่าที่ใส่เข้าไปในรูปแบบ `string`\n\n----\n## `int(args)`\nแปลง `args` เป็น `int`\n\n**Input** - `args` (ตัวเลขหรือ string ที่อยู่ในรูปแบบตัวเลข)\n\n**Output** - ค่าที่ใส่เข้าไปในรูปแบบ `int`"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "int(\"6\")",
"execution_count": 22,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 22,
"data": {
"text/plain": "6"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "int(12.7)",
"execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 21,
"data": {
"text/plain": "12"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "int(\"5.45\") #แปลงไม่ได้",
"execution_count": 23,
"outputs": [
{
"output_type": "error",
"ename": "ValueError",
"evalue": "invalid literal for int() with base 10: '5.45'",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-23-e9207733f9f3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"5.45\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#แปลงไม่ได้\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '5.45'"
]
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "-----\n\n## `len(args)`\nส่งออกค่าของความยาวค่าที่ใส่เข้าไป\n\n**Input** - `args` (`string`, `tuple`, `list` หรือ `range`)\n\n**Output** - `int` เป็นความยาวของ input"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "len(\"tornado\")",
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 26,
"data": {
"text/plain": "7"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "jingles = [\"died in a tornado\", \"the sun is a deadly lazer\", \"society\"]\nlen(jingles)",
"execution_count": 27,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 27,
"data": {
"text/plain": "3"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "len(range(0,5))",
"execution_count": 29,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 29,
"data": {
"text/plain": "5"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "----\n## `list(args)`\nแปลง `iterable` เป็น `list`\n\n**Input** - `args` `iterable` (`tuple`, `list` หรือ `dict`)\n\n**Output** - Input ในรูปแบบของ `list`"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "mytuple = (1, 2, 3, 4, 5)\nlist(mytuple)",
"execution_count": 30,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 30,
"data": {
"text/plain": "[1, 2, 3, 4, 5]"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "----\n## `max(args)`\nหาค่าที่มากที่สุดใน `iterable` ที่ใส่เข้ามา\n\n**Input** - `args` `iterable` (`tuple`, `list` หรือ `dict`)\n\n**Output** - ค่าที่มากที่สุดใน Input"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "year = [1917, 1991, 1939, 1945]\nmax(year)",
"execution_count": 31,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 31,
"data": {
"text/plain": "1991"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "text = [\"a\", \"b\", \"e\", \"d\"] # e เป็นตัวอักษรตัวที่ 5 ซึ่งมากกว่าทุกตัว\nmax(text)",
"execution_count": 36,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 36,
"data": {
"text/plain": "'e'"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "----\n## `min(args)`\nหาค่าที่น้อยที่สุดใน `iterable` ที่ใส่เข้ามา\n\n**Input** - `args` `iterable` (`tuple`, `list` หรือ `dict`)\n\n**Output** - ค่าที่น้อยที่สุดใน Input"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "weight = [49, 61, 29, 78, 37]\nmin(weight)",
"execution_count": 37,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 37,
"data": {
"text/plain": "29"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "text = [\"q\", \"w\", \"e\", \"r\", \"t\", \"y\"]\nmin(text)",
"execution_count": 38,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 38,
"data": {
"text/plain": "'e'"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "----\n## `range(args)` / `range(args1, args2, args3)`\n\n**Input**\n* `args` - `int` Stop\n\n* `args1` - `int` Start\n* `args2` - `int` Stop\n* `args3` - `int` Step (Optional)\n\n**Output** - `range()`"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "list(range(5))",
"execution_count": 40,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 40,
"data": {
"text/plain": "[0, 1, 2, 3, 4]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "list(range(4, 9))",
"execution_count": 41,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 41,
"data": {
"text/plain": "[4, 5, 6, 7, 8]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "list(range(1, 10, 2))",
"execution_count": 42,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 42,
"data": {
"text/plain": "[1, 3, 5, 7, 9]"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "---\n## `sorted(args, key=None, reverse=False)`\nเรียงค่าใน `list` จากน้อยไปมาก\n\n**Input**\n* `args` - `iterable` (`tuple`, `list` หรือ `dict`)\n* `key=`- `None` or (Ex: `str.lower`, `lambda i: len(i)`) (Optional)\n* `reverse=` - Boolean `False` or `True` (Optional)\n\n**Output** - `list`"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "mylist = [4, 7, 1, 9, 14, 5, 7]\nsorted(mylist)",
"execution_count": 43,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 43,
"data": {
"text/plain": "[1, 4, 5, 7, 7, 9, 14]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "ourlist = [\"slavsya\", \"otechestvo\", \"nashe\", \"svobodnoye\"]\nsorted(ourlist, key=len)",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "['nashe', 'slavsya', 'otechestvo', 'svobodnoye']"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "mytuple = (1, 9, 10, 19, 17)\nsorted(mytuple, reverse=True)",
"execution_count": 47,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 47,
"data": {
"text/plain": "[19, 17, 10, 9, 1]"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "---\n## `str(args)`\nแปลงค่าเป็น `string`\n\n**Input** - (`args` `int` `float` `list` `tuple`)\n\n**Output** - Input ในรูปแบบ `string`"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "str(1991)",
"execution_count": 48,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 48,
"data": {
"text/plain": "'1991'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "str([1, 3, 5, 7, 9])",
"execution_count": 49,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 49,
"data": {
"text/plain": "'[1, 3, 5, 7, 9]'"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "str(\"จะแปลงทำไม\")",
"execution_count": 51,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 51,
"data": {
"text/plain": "'จะแปลงทำไม'"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "---\n## `sum(args)`\nรวมค่าใน `iterable` ที่ใส่เข้ามา\n\n**Input** - `args` - `iterable` (`tuple`, `list` หรือ `dict`)\n\n**Output** - `int` or `float`"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "wearenumberone = [1, 1, 1, 1, 1, 1, 1, 1]\nsum(wearenumberone)",
"execution_count": 52,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 52,
"data": {
"text/plain": "8"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sum([1205, 768])",
"execution_count": 53,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 53,
"data": {
"text/plain": "1973"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "caliber = [5.56, 6.5, 7.62, 12.7]\nsum(caliber)",
"execution_count": 54,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 54,
"data": {
"text/plain": "32.379999999999995"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "---\n## type(args)\n\nแสดงประเภทของ `args`\n\n**Input** - `args` `int` `float` `list` `tuple` ฯลฯ\n\n**Output** - `class`"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(type(\"This is string\"))",
"execution_count": 56,
"outputs": [
{
"output_type": "stream",
"text": "<class 'str'>\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(type(43982.44))",
"execution_count": 57,
"outputs": [
{
"output_type": "stream",
"text": "<class 'float'>\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(type(2345))",
"execution_count": 58,
"outputs": [
{
"output_type": "stream",
"text": "<class 'int'>\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "central_pwr = [\"Deutschland\", \"Austria-Hungary\", \"Ottoman Empire\", \"Bulgaria\"]\nprint(type(central_pwr))",
"execution_count": 60,
"outputs": [
{
"output_type": "stream",
"text": "<class 'list'>\n",
"name": "stdout"
}
]
}
],
"metadata": {
"kernelspec": {
"name": "python36",
"display_name": "Python 3.6",
"language": "python"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"name": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6",
"file_extension": ".py",
"codemirror_mode": {
"version": 3,
"name": "ipython"
}
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment