Last active
August 4, 2023 13:33
-
-
Save JenniferNorthrup/ecff5553c218b8080a98320738135918 to your computer and use it in GitHub Desktop.
Practice+Exercise+-+Functions-1.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/JenniferNorthrup/ecff5553c218b8080a98320738135918/practice-exercise-functions-1.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "AJ2-9Jn-73Jh" | |
}, | |
"source": [ | |
"# **USER DEFINED FUNCTION AND LAMBDA FUNCTIONS EXERCISE**" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "wyLXAnvJ73Ji" | |
}, | |
"source": [ | |
"### 1. Write a function to add, subtract, multiply and divide two variables passed to it and print the result" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "qOgtr5xj73Jj", | |
"outputId": "34a571a0-aa5d-49ab-89e1-0d7755607d74" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Add: 3\n", | |
"Sub: -1\n", | |
"Mult: 2\n", | |
"Div: 0.5\n" | |
] | |
} | |
], | |
"source": [ | |
"def math_it_up(a, b):\n", | |
" add = a + b\n", | |
" sub = a - b\n", | |
" mult = a * b\n", | |
" div = a / b\n", | |
" print (f\"Add: {add}\")\n", | |
" print (f\"Sub: {sub}\")\n", | |
" print (f\"Mult: {mult}\")\n", | |
" print (f\"Div: {div}\")\n", | |
"\n", | |
"math_it_up(1, 2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "7lee14ZL73Jj" | |
}, | |
"source": [ | |
"### 2. Write a Python function given range (1,10) both included, that gives the square of every number" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "tgLm8_Zp73Jj", | |
"outputId": "00869ce7-ebc8-46f9-819c-e374984dd49f" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"The square of 1 is 1\n", | |
"The square of 2 is 4\n", | |
"The square of 3 is 9\n", | |
"The square of 4 is 16\n", | |
"The square of 5 is 25\n", | |
"The square of 6 is 36\n", | |
"The square of 7 is 49\n", | |
"The square of 8 is 64\n", | |
"The square of 9 is 81\n" | |
] | |
} | |
], | |
"source": [ | |
"my_range = range(1, 10)\n", | |
"\n", | |
"def squaresies(this_range):\n", | |
" for number in this_range:\n", | |
" print(f\"The square of {number} is {number ** 2}\")\n", | |
"\n", | |
"squaresies(my_range)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "eorY0_0473Jj" | |
}, | |
"source": [ | |
"### 3. Write a function to calculate simple interest\n", | |
"\n", | |
"simple interest = (Principal amount * Annual Rate of interest * Time(in years))/100\n", | |
"\n", | |
"Take values as follows\n", | |
"\n", | |
"Principal Amount = 1000\n", | |
"\n", | |
"Rate of interest = 3%\n", | |
"\n", | |
"Time = 5 years" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "9czytLRj73Jk", | |
"outputId": "0a793524-1d22-4308-bac0-9b64242baf05" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"1.5\n" | |
] | |
} | |
], | |
"source": [ | |
"principal = 1000\n", | |
"annual_rate = .03\n", | |
"time = 5\n", | |
"\n", | |
"def simple_interest(amount, rate, years):\n", | |
" return ((principal * annual_rate * years) / 100)\n", | |
"\n", | |
"print(simple_interest(principal, annual_rate, time))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "-TXTGWO073Jk" | |
}, | |
"source": [ | |
"### 4. Write a function to check the number is divisible by 25. The function should return True if divisible and \"Not divisible\" if not" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "nCTe_VzV73Jk", | |
"outputId": "9abdefb9-6efb-467a-af64-db42a003606c" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Not divisible\n" | |
] | |
} | |
], | |
"source": [ | |
"my_number = 423\n", | |
"\n", | |
"def div_checker(number):\n", | |
" if number % 25 == 0:\n", | |
" return True\n", | |
" else:\n", | |
" return \"Not divisible\"\n", | |
"\n", | |
"print(div_checker(my_number))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "hfMLcYTw73Jk" | |
}, | |
"source": [ | |
"### 5. Define a function that takes an input, squares it, adds 5, then returns the answer" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "9dfJrrVB73Jk", | |
"outputId": "092dd1a8-5d85-4c4f-e645-114b8311fa9c" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"9\n", | |
"2606\n" | |
] | |
} | |
], | |
"source": [ | |
"def mathy(number):\n", | |
" return ((number ** 2) + 5)\n", | |
"\n", | |
"print(mathy(2))\n", | |
"print(mathy(51))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "wxG7V5aX73Jl" | |
}, | |
"source": [ | |
"### 6. Using lambda function perform the following task : take an input, squares it, add 5, then return the answer" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "5Vwh3haX73Jl", | |
"outputId": "fa82784d-419a-4708-d474-c080f894806e" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"9\n", | |
"30\n" | |
] | |
} | |
], | |
"source": [ | |
"answer = lambda input: (input ** 2) + 5\n", | |
"\n", | |
"print(answer(2))\n", | |
"print(answer(5))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "y6F5uMUn73Jl" | |
}, | |
"source": [ | |
"### 7. Write a function to calculate the power of a number raised to other ($a^b$)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "ra5UtvrK73Jl", | |
"outputId": "2c409611-fc38-4e0b-86ab-befd6e30b981" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"8\n", | |
"9\n" | |
] | |
} | |
], | |
"source": [ | |
"def result1(a, b):\n", | |
" return a ** b\n", | |
"\n", | |
"# OR...\n", | |
"result2 = lambda a, b: a ** b\n", | |
"\n", | |
"print(result1(2, 3))\n", | |
"print(result2(3, 2))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "u3oV_1Cx73Jl" | |
}, | |
"source": [ | |
"### 8. Write a function to calculate the area of a triangle\n", | |
"\n", | |
"area of triangle = 1/2 * base * height" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "OMo-IgBB73Jl", | |
"outputId": "5e64c9a3-dee2-4188-95c0-66248d66012a" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"10.0\n" | |
] | |
} | |
], | |
"source": [ | |
"def area(base, height):\n", | |
" return .5 * base * height\n", | |
"\n", | |
"print(area(4,5))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "wocYpp5473Jl" | |
}, | |
"source": [ | |
"### 9. Create a function that takes country as the input and returns \"I am from *Country* \"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "FEVegDKq73Jm", | |
"outputId": "3605e28f-753d-4e6e-b20f-af0ccbbc29ed" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"I am from Sweden\n", | |
"I am from Germany\n" | |
] | |
} | |
], | |
"source": [ | |
"def fromfrom(country):\n", | |
" print(f\"I am from {country.capitalize()}\")\n", | |
"\n", | |
"fromfrom(\"sweden\")\n", | |
"fromfrom(\"Germany\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "ntDriIZX73Jm" | |
}, | |
"source": [ | |
"### 10. Write a function to convert degree Celsius temperature to Fahrenheit" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "5o8UFCuG73Jm", | |
"outputId": "d7571d9c-15d4-4339-f8f4-2beb5fee46b0" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"95.0\n" | |
] | |
} | |
], | |
"source": [ | |
"convert = lambda celsius: (celsius * 9/5) + 32\n", | |
"\n", | |
"print(convert(35))" | |
] | |
} | |
], | |
"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.9.7" | |
}, | |
"colab": { | |
"provenance": [], | |
"include_colab_link": true | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment