Skip to content

Instantly share code, notes, and snippets.

@mahenzon
Created June 11, 2023 12:10
Show Gist options
  • Save mahenzon/c8fe4caf7b3e01b9485fff192a8d3d16 to your computer and use it in GitHub Desktop.
Save mahenzon/c8fe4caf7b3e01b9485fff192a8d3d16 to your computer and use it in GitHub Desktop.
Python funcs returns and float/Decimal
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "ecc849da",
"metadata": {},
"outputs": [],
"source": [
"def square(num):\n",
" result = num * num\n",
" print(result)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a85ae3db",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9\n"
]
}
],
"source": [
"square(3)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "55f5e660",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"16\n"
]
}
],
"source": [
"square(4)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "491a58e7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
}
],
"source": [
"square(2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d9b3441c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"16\n"
]
}
],
"source": [
"square(4)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "09d6f078",
"metadata": {},
"outputs": [],
"source": [
"def square(num):\n",
" result = num * num\n",
" print(result)\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2b5ecf38",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
},
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square(2)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "25df9656",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9\n"
]
},
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square(3)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "f7627677",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"16\n"
]
},
{
"data": {
"text/plain": [
"16"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square(square(2))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "1874a9e6",
"metadata": {},
"outputs": [],
"source": [
"def square(num):\n",
" result = num * num\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "a1fd3b15",
"metadata": {},
"outputs": [],
"source": [
"def square(num):\n",
" return num * num"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "16a28947",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"25"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square(5)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "9df61b56",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"625"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square(square(5))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "c8026c6d",
"metadata": {},
"outputs": [],
"source": [
"five_squared = square(5)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "48164e6c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"25\n"
]
}
],
"source": [
"print(five_squared)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "71bb2683",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"625"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square(five_squared)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "c3c82c35",
"metadata": {},
"outputs": [],
"source": [
"def check_age(age):\n",
" if age < 18:\n",
" return \"Young\"\n",
" else:\n",
" return \"Old enough\""
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "8ecac501",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Young'"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check_age(17)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "68007f5d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Old enough'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check_age(18)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "93e5040e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Old enough'"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check_age(21)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "c0cb8fdb",
"metadata": {},
"outputs": [],
"source": [
"def check_age(age):\n",
" if age < 18:\n",
" return \"Young\"\n",
" return \"Old enough\""
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "874f9a2c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Young'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check_age(10)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "a6afe58a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Old enough'"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check_age(18)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "dc692257",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Old enough'"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check_age(25)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "7ac274f1",
"metadata": {},
"outputs": [],
"source": [
"def check_age(age):\n",
" if age < 18:\n",
" return \"Young\"\n",
" if age < 21:\n",
" return \"Old enough\"\n",
" return \"Adult\""
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "d997911b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Young'"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check_age(10)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "89da34a3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Old enough'"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check_age(18)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "938c5e0e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Old enough'"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check_age(20)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "c79016ba",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Adult'"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check_age(21)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "bd304cf1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Adult'"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check_age(25)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "2c280431",
"metadata": {},
"outputs": [],
"source": [
"def divide(a, b):\n",
" if b == 0:\n",
" return None\n",
" return a / b"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "431f1e2d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.5\n"
]
}
],
"source": [
"print(divide(10, 4))"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "3f315ac7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"print(divide(10, 0))"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "586b4d75",
"metadata": {},
"outputs": [],
"source": [
"def divide(a, b):\n",
" if b == 0:\n",
" return\n",
" return a / b"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "4078016b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.75\n"
]
}
],
"source": [
"print(divide(30, 8))"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "f7104f75",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"print(divide(30, 0))"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "87f727f6",
"metadata": {},
"outputs": [],
"source": [
"def divide(a, b):\n",
" if b != 0:\n",
" return a / b"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "3df83dde",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.5\n"
]
}
],
"source": [
"print(divide(7, 2))"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "b8555bae",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"print(divide(7, 0))"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "d87d68fd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.1415\n"
]
}
],
"source": [
"pi = 3.1415\n",
"print(pi)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "45526a89",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'float'>\n"
]
}
],
"source": [
"print(type(pi))"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "7552ebd6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"print(isinstance(pi, float))"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "45ad5d50",
"metadata": {},
"outputs": [],
"source": [
"def divide_n_times(value, divider, times):\n",
" for i in range(times):\n",
" value /= divider\n",
" return value"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "773a364a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.75"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divide_n_times(28, 4, 2)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "df092fa9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.12345000000000002"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divide_n_times(12345, 10, 5)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "e8e40cee",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.3000000000000003"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1.1 + 2.2"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "f8fb0eb1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"not equal\n"
]
}
],
"source": [
"if 1.1 + 2.2 == 3.3:\n",
" print(\"are equal\")\n",
"else:\n",
" print(\"not equal\")"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "b0284562",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.00123"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"123 / 100000"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "b62758e2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.000123"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"123 / 1000000"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "d4061197",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.23e-07"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"123 / 1000000000"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "b2753501",
"metadata": {},
"outputs": [],
"source": [
"ant_weight = 2e-6"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "22e10daa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The weight of the ant is 2e-06 kilograms.\n"
]
}
],
"source": [
"print(\"The weight of the ant is\", ant_weight, \"kilograms.\")"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "01b6376e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The weight of the ant is 0.000002 kilograms.\n"
]
}
],
"source": [
"print(f\"The weight of the ant is {ant_weight:.6f} kilograms.\")"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "e6aab2bc",
"metadata": {},
"outputs": [],
"source": [
"earth_weight = 5.97e24"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "ed0b2ade",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The weight of the Earth is 5.97e+24 kilograms.\n"
]
}
],
"source": [
"print(\"The weight of the Earth is\", earth_weight, \"kilograms.\")"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "210e616d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"earth_weight == 5.97e+24"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "36a139d5",
"metadata": {},
"outputs": [],
"source": [
"from decimal import Decimal"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "37bc8c62",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Decimal('3.3')"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Decimal('1.1') + Decimal('2.2')"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "673f4e97",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"not equal\n"
]
}
],
"source": [
"if (Decimal(\"1.1\") + Decimal(\"2.2\")) == 3.3:\n",
" print(\"are equal\")\n",
"else: \n",
" print(\"not equal\")"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "ad02e99b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Decimal('3.3') == 3.3"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "fe88fde6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Decimal('3.3') == Decimal('3.3')"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "a5af8e77",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"float(Decimal('3.3')) == 3.3"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "e41c5428",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Decimal('1') == 1"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "fa0641e0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Decimal('1.1') == 1.1"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "56021200",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(Decimal(\"1.1\") + Decimal(\"2.2\")) == Decimal(\"3.3\")"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "c44d1bc9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"float(Decimal(\"1.1\") + Decimal(\"2.2\")) == 3.3"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "a3cecf20",
"metadata": {},
"outputs": [],
"source": [
"def divide_n_times(value, divider, times):\n",
" value = Decimal(value)\n",
" for i in range(times):\n",
" value /= divider\n",
" return value"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "811b4f05",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Decimal('1.75')"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divide_n_times(28, 4, 2)"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "d0aba1a4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Decimal('0.12345')"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divide_n_times(12345, 10, 5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ae5d296",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment