reading-notes

Reading Questions

  1. 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.
    • scope of a name defnes the area of a program in which you can unambiguously access that name, such as variables, functions, objects, and so on. A name will only be visible to and accessible by the code in its scope.
    • Global Scope: The names that you define in this scope are available to all your code
    • Local Scope: the names that you define in this scope are only available or visible to the code within the scope
  2. How do the global and nonlocal keywords work in Python, and in what situations might you use them?
    • Global (module) scope: s the top-most scope in a Python program, script, or module. This Python scope contains all of the names that you define at the top level of a program or a module. Names in this Python scope are visible from everywhere in your code.
    • Enclosing (or nonlocal) scope is a special scope that only exists for nested functions. If the local scope is an inner or nested function, then the enclosing scope is the scope of the outer or enclosing function. This scope contains the names that you define in the enclosing function. The names in the enclosing scope are visible from the code of the inner and enclosing functions.
  3. In your own words, describe the purpose and importance of Big O notation in the context of algorithm analysis.
    • When used properly Big O notation can give us an idea of the computing power required of an algorithm. Being familiar with this methodology can help us find the most efficient means of running an operation. Depending on the size and complexity and type of data involved Big O notation gives us a good idea of what to expect.
  4. 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.
    • def probability_of_rolling_number(number): “"”Calculates the probability of rolling a specific number.””” trials = 10000 number_of_rolls = 0 for _ in range(trials): result = roll_dice() if result == number: number_of_rolls += 1 return number_of_rolls / trials