LUNAR
🌙
Dashboard
Browse
SW06_Notes
JUPYTER
View Source
{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "picio\n", "picio\n", "picio\n", "banano\n", "banano\n", "banano\n" ] } ], "source": [ "def caccolo(cacco,lone):\n", " for scrotus in range(lone):\n", " print(cacco)\n", " \n", "caccolo(\"picio\",3)\n", "\n", "scrotus = 3\n", "caccolo(\"banano\",_)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1024 caccolo 5\n", "2048 caccolo 5\n", "3072 caccolo 5\n" ] } ], "source": [ "def somma(n,x,y,z):\n", " sum = 0\n", " for _ in range(n):\n", " sum += (x+y)**z\n", " print(sum,\"caccolo\", z)\n", "\n", "somma(3,1,3,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Return with Boolean" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6 is even\n" ] } ], "source": [ "def even(number:int)->bool:\n", " \"\"\"\n", " Check if number is even\n", "\n", " Parameters:\n", " number (int): Number to check\n", " Returns:\n", " bool: True if number is even\n", " \"\"\"\n", " return number % 2 == 0\n", "\n", "n = 6\n", "if even(n):\n", " print(f\"{n} is even\")\n", "\n", "else:\n", " print(f\"{n} is odd\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Inner functions" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cacca: culo\n" ] } ], "source": [ "def outer_function(message):\n", " def inner_function():\n", " print(f\"cacca: {message}\")\n", " \n", " inner_function()\n", "\n", "outer_function(\"culo\")" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Result: 8\n" ] } ], "source": [ "def outer_function(x):\n", " def inner_function(y):\n", " return x + y \n", " \n", " return inner_function\n", "\n", "inner = outer_function(5)\n", "result = inner(3)\n", "print(f\"Result: {result}\")" ] } ], "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.9" } }, "nbformat": 4, "nbformat_minor": 2 }