{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Python \n", "\n", "## Scopes\n", "\n", "### Scopes are defined after a colon (:) and are delimited by the indentation. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "< scope creation statement>: \n", ".... command \n", ".... command \n", ".... command \n", ".... command \n", "< end of scope> " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Scopes are created after the following primitives: \n", "+ _if_ \n", "+ _for_ \n", "+ _while_ \n", "+ _def_ \n", "+ _class_ \n", "+ _with_ \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Nested Scopes in Python" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "local2\n", "local1\n", "global\n" ] } ], "source": [ "x = 'global'\n", "\n", "def local1(): #If another variable exists with the same name inside the function, the reference before assignment yields an error\n", " #print(x)\n", " x = 'local1'\n", " def local2():\n", " #print(x)\n", " x = 'local2'\n", " print(x)\n", " local2() \n", " print(x)\n", "\n", "local1()\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Variables and Functions defined in the inner scope:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What's your name: My_name is Renato\n", "What's your name: My name is Pedro\n", "What's your name: My name is Pedro\n", "What's your name: My_name is Renato\n", "What's your name: My name is Leonardo\n", "What's your name: My name is Leonardo\n", "What's your name: None\n" ] }, { "ename": "NameError", "evalue": "name 'function_inner_scope' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf'What\\'s your name: {x}'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 25\u001b[0;31m \u001b[0mfunction_inner_scope\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# error - the funtion does not exist in this scope\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'function_inner_scope' is not defined" ] } ], "source": [ "x = 'My_name is Renato'\n", "\n", "def function_main_scope():\n", " x = 'My name is Pedro'\n", " print(f'What\\'s your name: {x}')\n", " \n", " def function_inner_scope():\n", " global x\n", " print(f'What\\'s your name: {x}')\n", " x = 'My name is Leonardo'\n", " print(f'What\\'s your name: {x}')\n", " #print(x)\n", " return x\n", " print(f'What\\'s your name: {x}')\n", " x = function_inner_scope()\n", " print(f'What\\'s your name: {x}')\n", " \n", " \n", "print(f'What\\'s your name: {x}')\n", "\n", "x = function_main_scope()\n", "\n", "print(f'What\\'s your name: {x}')\n", "\n", "function_inner_scope() # NameError: name 'function_inner_scope' is not defined" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Functions receive parameters in the tuple \n", " def (parameters): \n", "### and return them with _return_ or _yield_ " ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "3\n" ] } ], "source": [ "x = 1\n", "\n", "def increase(x):\n", " print(x)\n", " x += 1\n", " \n", " def increase_plus(x):\n", " x += 1\n", " return x\n", " \n", " x = increase_plus(x)\n", " return x\n", "\n", "x = increase(x)\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [Global and local variables](https://www.programiz.com/python-programming/global-local-nonlocal-variables) \n", "### Commands _global_ and _nonlocal_ " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "After local assignment: The original local spam\n", "After nonlocal assignment: this is neither local nor global spam\n", "After global assignment: this is neither local nor global spam\n", "Printing in global scope: this is the new global spam\n" ] } ], "source": [ "def scope_test():\n", " def do_local():\n", " spam = \"this is the local spam\"\n", " \n", " def do_nonlocal():\n", " nonlocal spam\n", " spam = \"this is neither local nor global spam\"\n", " \n", " def do_global():\n", " global spam\n", " spam = \"this is the new global spam\"\n", " \n", " spam = \"The original local spam\"\n", " \n", " do_local()\n", " print(\"After local assignment:\", spam)\n", " do_nonlocal()\n", " print(\"After nonlocal assignment:\", spam)\n", " do_global()\n", " print(\"After global assignment:\", spam)\n", "\n", "spam = \"The original global spam\"\n", "\n", "scope_test()\n", "\n", "print(\"Printing in global scope:\", spam)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Another example in a _for_ loop" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "4\n", "5\n", "6\n", "1\n", "4\n", "5\n", "6\n", "2\n", "4\n", "5\n", "6\n" ] } ], "source": [ "for same_name in range(3):\n", " print(same_name)\n", " for same_name in range(4,7):\n", " print(same_name)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.8 64-bit (microsoft store)", "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.8" }, "vscode": { "interpreter": { "hash": "a3a5d4f9651fa550758ade4a473ec6b18daeb3d63067db8abd62b64f4493ea9c" } } }, "nbformat": 4, "nbformat_minor": 4 }