Search the courses, subjects, tracks, games and features, or jump straight to a page.
3 cards from this section. Quick, good the night before a test.
Define a local variable in a function and try to print it outside the function. What do you observe?
Try printing the variable after the function call.
Create a global variable and declare it outside any functions. Then print it within a function. What happens?
Just define it on the top level of your script.
What is a local variable?
Think about where the variable is defined.
True or False: A global variable can be accessed inside any function.
Remember where global variables exist.
Design and implement a Python script using a combination of local and global variables. Explain how local and global variables interact within your script.
View this as testing the space they occupy in your code.
Create a function that adds to a global counter each time it's called, using the global keyword. Demonstrate by calling the function multiple times and printing the counter.
Think about how often you call the function and what it does during each call.
5 more questions in this section. Enroll free to practise them all.
Enroll free