Skip to content

Instantly share code, notes, and snippets.

@tamlt2704
Created June 23, 2018 14:01
Show Gist options
  • Save tamlt2704/632448d3da0c8aac11be72c1f4884eca to your computer and use it in GitHub Desktop.
Save tamlt2704/632448d3da0c8aac11be72c1f4884eca to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. abs(x)\n",
"**Abs(x)** trả về trị tuyệt đối của số **x**\n",
"\n",
"**x** có thể là số thực (**float**), số nguyên (**int**) hoặc số phức\n",
"\n",
"Trong trường hợp **x** là số phức\n",
"$$x = a + b.i; abs(x) = \\sqrt{a^2 + b^2} $$"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 3.14, 5.0)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = abs(-3)\n",
"y = abs(-3.14)\n",
"z = abs(3 + 4j) # |z| = sqrt(a^2 + b^2) = sqrt(3^2 + 4^2) = sqrt(9 + 16) = sqrt(25) = 5\n",
"x,y,z"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. all(iterable)\n",
"**all** trả về giá trị **True** nếu:\n",
" + tất cả các phần tử trong iterable là **True**.\n",
" + iterable rỗng\n",
"**all** trả về giá trị **False** nếu một phần tử bất kì trong iterable là **False**.\n",
"\n",
"```\n",
"def all(iterable):\n",
" for element in iterable:\n",
" if not element:\n",
" return False\n",
" return True\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(True, True, False)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = all([True, 3, 2 < 5])\n",
"y = all([]) # emty iterable\n",
"z = all([True, 3, 2 < 5, y, False])\n",
"x, y, z"
]
},
{
"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