Skip to content

Instantly share code, notes, and snippets.

@masasa27
Created August 1, 2020 20:22
Show Gist options
  • Select an option

  • Save masasa27/d6e3fdb43dd4cd5637ac78c6711128e2 to your computer and use it in GitHub Desktop.

Select an option

Save masasa27/d6e3fdb43dd4cd5637ac78c6711128e2 to your computer and use it in GitHub Desktop.
Functions
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Functions"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 0)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# % checks reminders\n",
"7 % 2, 8 % 2"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"def test_even(x):\n",
" if x % 2 == 0:\n",
" return True\n",
" return False\n",
"\n",
"# this is the same, just cooler\n",
"def test_even(x):\n",
" return not x % 2 "
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(False, True)"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# making sure my function works\n",
"test_even(3), test_even(2)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 2, 4, 6, 8]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"even_numbers = []\n",
"for i in range(10):\n",
" if test_even(i):\n",
" even_numbers.append(i)\n",
"even_numbers"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 2, 4, 6, 8]"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# same thing, ill cover this in an other day\n",
"[i for i in range(10) if test_even(i)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fucntion can return numbers as well"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"def power(x, r=2): # default value\n",
" return x ** r"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.0"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"power(4, 1/2) # this is sqrt, 4 in the power of 1/2"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(0, 0),\n",
" (1, 1),\n",
" (2, 4),\n",
" (3, 9),\n",
" (4, 16),\n",
" (5, 25),\n",
" (6, 36),\n",
" (7, 49),\n",
" (8, 64),\n",
" (9, 81)]"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"powers = []\n",
"for i in range(10):\n",
" value = (i, power(i))\n",
" powers.append(value)\n",
"powers"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(0, 0),\n",
" (1, 1),\n",
" (2, 4),\n",
" (3, 9),\n",
" (4, 16),\n",
" (5, 25),\n",
" (6, 36),\n",
" (7, 49),\n",
" (8, 64),\n",
" (9, 81)]"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# same thing - again\n",
"[(i,power(i)) for i in range(10)]"
]
}
],
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment