reading-notes

Class 7

Back to home page

Python scope

Q. Explain the concept of variable scope in Python and describe the difference between local and global scope. Provide an example illustrating the usage of both.

Q. How do the global and nonlocal keywords work in Python, and in what situations might you use them?

Global keyword example using count

In this example we are using the count variable that has been declared outside as a global, and using this particular variable in our function increment(). A nonlocal example looks like this…

nonlocal keyword example using count

In this example, we are using the count variable declared in a different function from an outer function. This is different from a global function because this function is nested within the outer() function.

Big O notation

Q. In your own words, describe the purpose and importance of Big O notation in the context of algorithm analysis.

Q. Based on the Rolling Dice Example, explain how you would simulate a dice roll using Python. Describe how you would use code to calculate the probability of rolling a specific number (e.g., the probability of rolling a 6) over a large number of trials.

Bookmark and review

Things I want to know more about

What are some uses of nested functions? What is their purpose?