Skip to content

Instantly share code, notes, and snippets.

@hvnsweeting
Created April 18, 2019 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hvnsweeting/56b4e686cb062f4a20059e238140e6b0 to your computer and use it in GitHub Desktop.
Save hvnsweeting/56b4e686cb062f4a20059e238140e6b0 to your computer and use it in GitHub Desktop.
6. function,set - Học lập trình Python tại https://pymi.vn
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lesson 6 (1/2) :yeah:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dict (cont)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"tel = {'jack': 4098, 'sape': 4139}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'jack': 4098, 'sape': 4139}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tel"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"jack 4098\n",
"sape 4139\n"
]
}
],
"source": [
"for key, value in tel.items():\n",
" print(key, value)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4098"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tel['jack']"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "'PYMI'",
"output_type": "error",
"traceback": [
"\u001b[0;31m-------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-5-154d3cb17894>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtel\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'PYMI'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m: 'PYMI'"
]
}
],
"source": [
"tel['PYMI']"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"user = 'PYMI'"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Khong co so cua PYMI\n"
]
}
],
"source": [
"try:\n",
" print(\"SDT cua {} la {}\".format(user, tel[user]))\n",
"except KeyError:\n",
" print(\"Khong co so cua {}\".format(user))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"khong co so\n"
]
}
],
"source": [
"if user in tel:\n",
" print(\"Co so\", tel[user])\n",
"else:\n",
" print('khong co so')"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'PYMI' in tel"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'jack': 4098, 'sape': 4139}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tel"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'jack' in tel"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'jack': 4098, 'sape': 4139}"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tel"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"del tel['jack'] # del - delete"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'sape': 4139}"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tel"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "'pikachu'",
"output_type": "error",
"traceback": [
"\u001b[0;31m-------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-19-2caecab80e98>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0mtel\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'pikachu'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m: 'pikachu'"
]
}
],
"source": [
"del tel['pikachu']"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"d = {str(i) * 4 : i for i in range(7)}"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'0000': 0, '1111': 1, '2222': 2, '3333': 3, '4444': 4, '5555': 5, '6666': 6}"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"my_items = list(d.items())"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('0000', 0), ('1111', 1), ('2222', 2), ('3333', 3), ('4444', 4), ('5555', 5), ('6666', 6)]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_items"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'0000': 0, '1111': 1, '2222': 2, '3333': 3, '4444': 4, '5555': 5, '6666': 6}"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dict(my_items)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Set {} phan cach bởi dấu , "
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"S = {1,2,3} "
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<class 'set'>"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(S)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"S1 = {1,1,2,3,2,2,4,1}"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4 {1, 2, 3, 4}\n"
]
}
],
"source": [
"print(len(S1), S1)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"S2 = {1, 11, 11, 2, 1, 4}"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 11, 4}"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"S2"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"11\n",
"4\n"
]
}
],
"source": [
"for i in S2:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"11\n",
"4\n"
]
}
],
"source": [
"for i in S2:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 11, 4]"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(S2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Set không có thứ tự (unordered)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"houses = {'Anh1', 'Anh2', 'Anh3', 'Anh4'}"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"cars = {'Anh4', 'Anh2', 'Anh5', 'Anh7'}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Có nhà và xe "
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Anh4', 'Anh2'}"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"houses.intersection(cars)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Anh4', 'Anh2'}"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"houses & cars"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### CHỈ có nhà "
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'Anh3', 'Anh1'}"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"houses.difference(cars)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Anh3', 'Anh1'}"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"houses - cars"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Chỉ có xe "
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Anh7', 'Anh5'}"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cars.difference(houses)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Anh7', 'Anh5'}"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cars - houses"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tất cả các anh"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Anh7', 'Anh4', 'Anh5', 'Anh3', 'Anh2', 'Anh1'}"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"houses.union(cars)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'Anh7', 'Anh4', 'Anh5', 'Anh3', 'Anh2', 'Anh1'}"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"houses | cars"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Chỉ có 1 thứ"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Anh7', 'Anh5', 'Anh3', 'Anh1'}"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"houses.symmetric_difference(cars)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### XOR"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Anh7', 'Anh5', 'Anh3', 'Anh1'}"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"houses ^ cars"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'Anh4', 'Anh2', 'Anh3', 'Anh1'}"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set(list(houses))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Naming"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"x = 7"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"y = 5"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"c = (x + y) * 2"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"24\n"
]
}
],
"source": [
"print(c)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5 quả chuối: mỗi quả 3000\n",
"## 7 cốc trà sữa, mỗi cốc 15000\n",
"## tính tổng tiền và in ra "
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"soluong_chuoi = 5"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"sotien_chuoi = 3000"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"soluong_trasua = 7"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
"sotien_trasua = 15000"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TONG TIEN LA: 120000\n"
]
}
],
"source": [
"tongtien = soluong_chuoi*sotien_chuoi + soluong_trasua*sotien_trasua\n",
"print('TONG TIEN LA:',tongtien\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Có 5 tờ báo: mỗi tờ 5000\n",
"## Có 7 cái bao: mỗi cái 2000\n",
"\n",
"### Viết code in ra tổng giá tiền "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tong cong: 39000\n"
]
}
],
"source": [
"to_bao = 5\n",
"cai_bao = 7\n",
"gia_to_bao = 5000\n",
"gia_cai_bao = 2000\n",
"tong_cong = cai_bao * gia_cai_bao + to_bao * gia_to_bao\n",
"\n",
"print('tong cong: ', tong_cong)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"total: 29000\n"
]
}
],
"source": [
"new_price = 3000\n",
"bag_price = 2000\n",
"new_no = 5\n",
"bag_no = 7\n",
"total = new_price * new_no + bag_price * bag_no\n",
"print('total:', total)"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"29000\n"
]
}
],
"source": [
"newspaper_count = 5\n",
"newspaper_price = 3000\n",
"bag_count = 7\n",
"bag_price = 2000\n",
"total = bag_count * bag_price + newspaper_count * newspaper_price\n",
"print(total)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://www.slideshare.net/pirhilton/how-to-name-things-the-hardest-problem-in-programming"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Iterable"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- list\n",
"- dict\n",
"- tuple\n",
"- string\n",
"- set"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"sum?\n",
"\n",
"Function\n",
"\n",
"​\n",
"\n",
"Signature: sum(iterable, start=0, /)\n",
"Docstring:\n",
"Return the sum of a 'start' value (default: 0) plus an iterable of numbers\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum([1,2,3])"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum((1,2,3))"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum({3,2,1,1,3,3,})"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {1: \"mot\", 2: \"hai\", 3: \"ba\"}\n",
"sum(d)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(d)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len({1,2,3,1})"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {
"scrolled": true
},
"outputs": [
{
"ename": "TypeError",
"evalue": "object of type 'int' has no len()",
"output_type": "error",
"traceback": [
"\u001b[0;31m-------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-80-1cf91bb60cc0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: object of type 'int' has no len()"
]
}
],
"source": [
"len(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://github.com/saltstack/salt/blob/develop/salt/state.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Function"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [],
"source": [
"funcs = [len, sum, print,\n",
" bin, str, list, int, \n",
" float, bool, tuple, set,\n",
" dict, type, dir, \n",
" id, range,\n",
" ord, chr, \n",
" sorted, \n",
" enumerate, \n",
" min, max, \n",
" zip,]"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"23"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(funcs)"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"len <class 'builtin_function_or_method'>\n",
"sum <class 'builtin_function_or_method'>\n",
"print <class 'builtin_function_or_method'>\n",
"bin <class 'builtin_function_or_method'>\n",
"str <class 'type'>\n",
"list <class 'type'>\n",
"int <class 'type'>\n",
"float <class 'type'>\n",
"bool <class 'type'>\n",
"tuple <class 'type'>\n",
"set <class 'type'>\n",
"dict <class 'type'>\n",
"type <class 'type'>\n",
"dir <class 'builtin_function_or_method'>\n",
"id <class 'builtin_function_or_method'>\n",
"range <class 'type'>\n",
"ord <class 'builtin_function_or_method'>\n",
"chr <class 'builtin_function_or_method'>\n",
"sorted <class 'builtin_function_or_method'>\n",
"enumerate <class 'type'>\n",
"min <class 'builtin_function_or_method'>\n",
"max <class 'builtin_function_or_method'>\n",
"zip <class 'type'>\n"
]
}
],
"source": [
"for f in funcs:\n",
" print(f.__name__, type(f))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Built-in"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"http://docs.python.org/library/functions.html"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## User-defined function"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
"squared = lambda x: x**2"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"81"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"squared(9)"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [],
"source": [
"increase = lambda x: x+1"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"increase(6)"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
"decrease = lambda n: n-1"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"decrease(10)"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [],
"source": [
"average = lambda x,y: (x+y)/2"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8.0"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"average(10, 6)"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [],
"source": [
"perimeter = lambda a, b: (a+b) * 2"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"perimeter(2,4)"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [],
"source": [
"sum_two = lambda x, y : x + y"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum_two(20, 22)"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"\n",
"sum_three = lambda x,y,z : x+y+z"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum_three(1,2,3)"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [],
"source": [
"five = lambda : 5"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"five()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# function nhận vào 1 tuple, trả về phần tử thứ 2"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [],
"source": [
"second = lambda t: t[1]"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 106,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"second((1,2,3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nhận vào list, trả về phần tử cuối cùng "
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [],
"source": [
"last = lambda l:l[-1]"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"19"
]
},
"execution_count": 108,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"last(range(20))"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [],
"source": [
"cuoi_cung = last"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cuoi_cung([1,2,3,4])"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(139657452992168, 139657452992168)"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id(last), id(cuoi_cung)"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"last is cuoi_cung"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### function alias ^^^^ "
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(9297504, 9297504)"
]
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 5\n",
"y = x\n",
"id(x), id(y)"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"x is y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Function trong Python là object "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Function nhận vào 1 list, in ra lần lượt các phần tử ,nếu phần tử lớn hơn 5 thì dừng lại "
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"1\n",
"4\n"
]
}
],
"source": [
"numbers = [1,2,3,4,1,4,6,7]\n",
"for i in numbers:\n",
" if i > 5:\n",
" break\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lambda chỉ dùng để định nghĩa function đơn giản, 1 dòng "
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {},
"outputs": [],
"source": [
"squared = lambda x: x**2"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [],
"source": [
"def squared(x): # def == define\n",
" return x**2"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"squared(10)"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {},
"outputs": [],
"source": [
"def sum_three(x, y, z):\n",
" return x + y + z"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum_three(1,2,3)"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [],
"source": [
"def print_number_till_six(numbers):\n",
" for i in numbers:\n",
" if i > 5:\n",
" break\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"2\n",
"1\n"
]
}
],
"source": [
"print_number_till_six([4,2,1,6,2])"
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {},
"outputs": [],
"source": [
"def hello(name, age): \n",
" \"\"\"\n",
" Chao hoi le phep\n",
" nhiều đoạn?\n",
" thì ghi ra?\n",
" \"\"\"\n",
" print(\"Xin chao {}\".format(name))\n",
" if age > 30:\n",
" print(\"Co gia dinh chua\")\n",
" else:\n",
" print(\"Cứ chơi đi \")"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Xin chao Tảo\n",
"Cứ chơi đi \n"
]
}
],
"source": [
"hello('Tảo', 24)"
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Xin chao Thắng\n",
"Co gia dinh chua\n"
]
}
],
"source": [
"hello('Thắng', 35)"
]
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {},
"outputs": [],
"source": [
"hello?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Docstring cua function\n",
"\n",
" Triple quote ngay dưới dòng def ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Docstring: Chao hoi le phep"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"def function_name(argument1, argument2, ...):\n",
" \"\"\"\n",
" docstring\n",
" docstring\n",
" \"\"\"\n",
" function body\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"outputs": [],
"source": [
"def hello(name, age): \n",
" \"\"\"\n",
" Chao hoi le phep\n",
" \"\"\"\n",
" print(\"Xin chao {}\".format(name))\n",
" if age > 30:\n",
" print(\"Co gia dinh chua\")\n",
" else:\n",
" print(\"Cứ chơi đi \")"
]
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Xin chao Khương\n",
"Cứ chơi đi \n"
]
}
],
"source": [
"x = hello('Khương', 20)"
]
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
},
{
"data": {
"text/plain": [
"(<class 'NoneType'>, None)"
]
},
"execution_count": 144,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(x), print(x)"
]
},
{
"cell_type": "code",
"execution_count": 145,
"metadata": {},
"outputs": [],
"source": [
"def hello(name, age): \n",
" \"\"\"\n",
" Chao hoi le phep\n",
" \"\"\"\n",
" print(\"Xin chao {}\".format(name))\n",
" if age > 30:\n",
" print(\"Co gia dinh chua\")\n",
" else:\n",
" print(\"Cứ chơi đi \")\n",
" return None"
]
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"outputs": [],
"source": [
"def hello(name, age): \n",
" \"\"\"\n",
" Chao hoi le phep\n",
" \"\"\"\n",
" print(\"Xin chao {}\".format(name))\n",
" if age > 30:\n",
" print(\"Co gia dinh chua\")\n",
" else:\n",
" print(\"Cứ chơi đi \")\n",
" return "
]
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {},
"outputs": [],
"source": [
"def foo(n):\n",
" k = n * 2\n",
" if k > 7:\n",
" return k\n",
" else: \n",
" return n\n",
" return 100\n",
" return 200\n",
" return 300 # dead code = không bao giờ chạy tới "
]
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20\n"
]
}
],
"source": [
"t = foo(10)\n",
"print(t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## code trong func gặp return dừng lại ngay lập tức "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 153,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20\n",
"9\n"
]
}
],
"source": [
"k = 9\n",
"def foo(n):\n",
" k = n * 2\n",
" if k > 7:\n",
" return k\n",
" else: \n",
" return n\n",
" \n",
"print(foo(10))\n",
"print(k)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 154,
"metadata": {},
"outputs": [],
"source": [
"n = 1\n",
"def bar(i):\n",
" return i + n\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11\n"
]
}
],
"source": [
"print(bar(10))"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [
{
"ename": "UnboundLocalError",
"evalue": "local variable 'n' referenced before assignment",
"output_type": "error",
"traceback": [
"\u001b[0;31m-------------------------------------------------\u001b[0m",
"\u001b[0;31mUnboundLocalError\u001b[0mTraceback (most recent call last)",
"\u001b[0;32m<ipython-input-156-cd992713c79d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbaz\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-156-cd992713c79d>\u001b[0m in \u001b[0;36mbaz\u001b[0;34m(i)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mbaz\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mUnboundLocalError\u001b[0m: local variable 'n' referenced before assignment"
]
}
],
"source": [
"n = 1\n",
"def baz(i):\n",
" n = n + 1\n",
" return n\n",
"\n",
"print(baz(7))"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [
{
"ename": "UnboundLocalError",
"evalue": "local variable 'n' referenced before assignment",
"output_type": "error",
"traceback": [
"\u001b[0;31m-------------------------------------------------\u001b[0m",
"\u001b[0;31mUnboundLocalError\u001b[0mTraceback (most recent call last)",
"\u001b[0;32m<ipython-input-157-ed557839e651>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbaz\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-157-ed557839e651>\u001b[0m in \u001b[0;36mbaz\u001b[0;34m(i)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mbaz\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mUnboundLocalError\u001b[0m: local variable 'n' referenced before assignment"
]
}
],
"source": [
"n = 1\n",
"def baz(i):\n",
" print(n)\n",
" n = n + i\n",
" return n\n",
"\n",
"print(baz(7))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Hạn chế dùng "
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"n = 1\n",
"def baz():\n",
" # tang n len 2???\n",
" global n\n",
" n = n + 1\n",
" \n",
"baz()\n",
"print(n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://duckduckgo.com/?q=global+is+evil&t=ffab&ia=web"
]
},
{
"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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment