Summary of Scope (lesson learnt from 100 Days of Code, Day 12) There is the local scope and global scope for variables. Global scope refers to variables that we assigned in our codes. Local scope refers to variables that we assign or use within a function. In a function, if no variable is defined, the global scope version will be used. eg 1. coffee_drink = "long black" def cuppa(): print("Drink inside cuppa function:", coffee_drink) cuppa() print("Drink outside cuppa function:", coffee_drink) This will return you: Drink inside cuppa function: long black Drink outside cuppa function: long black However, if you had defined a coffee_drink in the function, that coffee_drink is only callable within the function. eg 2. def cuppa(): coffee_drink = "latte" print("Drink inside cuppa function:", coffee_drink) cuppa() print("Drink outside cuppa function:", coffee_drink) This will return you: Drink inside cuppa function:
I hope to share with you my consolidated thoughts on a wide variety of topics here.