{ "cells": [ { "cell_type": "markdown", "id": "aa23661b-dcc4-482f-b023-8b56748460f7", "metadata": {}, "source": [ "# Lesson 06 in-class demo: functions\n", "\n", "## 1. Functions intro\n", "\n", "### 1.1. Defining functions" ] }, { "cell_type": "code", "execution_count": 1, "id": "de9ac96d-1a69-40b5-8819-553e89435bde", "metadata": {}, "outputs": [], "source": [ "def greeting():\n", " print('Hello world!')" ] }, { "cell_type": "markdown", "id": "6739d17e-6de6-4970-8261-d92823b6ce5a", "metadata": {}, "source": [ "## 1.2. Calling functions" ] }, { "cell_type": "code", "execution_count": 2, "id": "8bc74ba9-b554-4435-9ff1-c24c9527c64d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world!\n" ] } ], "source": [ "greeting()" ] }, { "cell_type": "markdown", "id": "fcad18a7-7e87-45c9-9f11-c8793a41c807", "metadata": {}, "source": [ "## 2. Function arguments\n", "\n", "### 1.1. Single positional argument" ] }, { "cell_type": "code", "execution_count": 5, "id": "6d7f423d-be16-4102-90d8-4bc0da88fb86", "metadata": {}, "outputs": [], "source": [ "def greet_person(name):\n", " print(f'Hello {name}')" ] }, { "cell_type": "code", "execution_count": 6, "id": "1c827839-0ca1-46eb-95f0-4ba39d06b5e5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Alice\n" ] } ], "source": [ "greet_person('Alice')" ] }, { "cell_type": "code", "execution_count": 7, "id": "afe93239-f93b-4d1e-a5d4-4007a37c8322", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Bob\n" ] } ], "source": [ "greet_person('Bob')" ] }, { "cell_type": "markdown", "id": "432e319a-1f63-4826-ace4-3772d62d0a4f", "metadata": {}, "source": [ "### 1.2. Multiple positional arguments" ] }, { "cell_type": "code", "execution_count": 18, "id": "9eb80a8a-0403-471b-b773-222165298be0", "metadata": {}, "outputs": [], "source": [ "def introduce(name, age):\n", " print(f'My name is {name} and I am {age} years old')" ] }, { "cell_type": "code", "execution_count": 19, "id": "63ee11ca-cd8f-4073-b199-130be5408ef7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is Laura and I am 37 years old\n" ] } ], "source": [ "introduce('Laura', 37)" ] }, { "cell_type": "markdown", "id": "e647ae28-24e5-4874-ab67-5fe90318abbb", "metadata": {}, "source": [ "### 1.3. Keyword arguments\n", "\n", "- **Positional args** defined by order\n", "- **Keyword args** defined by name" ] }, { "cell_type": "code", "execution_count": 20, "id": "f1824c88-1b90-4c62-adec-799cca75bacd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is Carol and I am 30 years old\n" ] } ], "source": [ "introduce(age=30, name='Carol')" ] }, { "cell_type": "code", "execution_count": 21, "id": "e4da32a5-14b9-4148-aebe-c69a5f81f97c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is Carol and I am 30 years old\n" ] } ], "source": [ "introduce(name='Carol', age=30)" ] }, { "cell_type": "markdown", "id": "6f750f6f-2df5-41ac-b214-940b139ef426", "metadata": {}, "source": [ "## 2.3. Arbitrary arguments" ] }, { "cell_type": "code", "execution_count": 37, "id": "f37f28c4-9a34-424f-9cf0-c72f430be72e", "metadata": {}, "outputs": [], "source": [ "def add_numbers(*args):\n", " \n", " total = 0\n", " \n", " for num in args:\n", " total += num\n", " \n", " return total" ] }, { "cell_type": "code", "execution_count": 38, "id": "eecba6b8-2c03-4410-8cd8-5855d53d664a", "metadata": {}, "outputs": [], "source": [ "result = add_numbers(1,2,3,4,5,6)" ] }, { "cell_type": "code", "execution_count": 39, "id": "6c67fdfd-7892-4014-9742-6bf36fb08e66", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "21\n" ] } ], "source": [ "print(result)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 [3.10]", "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.10.2" } }, "nbformat": 4, "nbformat_minor": 5 }