Example
Local and Global Variables
Local variables exist only inside functions. Global ones are accessible everywhere.
Showing 1–10 of 12
Example
Local variables exist only inside functions. Global ones are accessible everywhere.
Quiz
Free beginner quiz on Python variable scope: 15 questions on local and global variables, the global and nonlocal keywords, and nested functions.
Quiz
Free beginner quiz on Python variables: 15 questions on naming, assignment, data types, type conversion and scope, with instant answers.
Challenge
refuel() assigns to fuel_kg inside the function, creating a local variable and leaving the global fuel unchanged. Predict the last line, compare both values, fix it by returning the total, then write…
Article Mini projects
…secret. Set MEDICATION_DISPENSER_SECRET environment variable for production.") return secret def _compute_hmac(self, message): """Compute HMAC-SHA256 of message using device secret""" return hmac.new(…
Example
The nonlocal keyword gives access to variables in the enclosing function.
Example
Namespaces isolate variables. Modules and functions create their own namespaces.
Example
Parameters allow passing data to functions. Functions become more flexible.
Example
The global keyword allows modifying a global variable inside a function.
Article Python Fundamentals
…greet_crew is the name. The rules are the same as for variable names: lowercase letters, words joined with underscores, no spaces, and it can't start with a number. The parentheses () are where inputs…