Daily Reading 7
Ten Thousand 2
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.
Variable scope is a term for the level of visibility a variable has, especially in relation to fundamental defaults based on where, and under what contexts a variable was declared. If a variable were to be declared within a method, calling it directly from outside the method would not work, and instead, you need to go “into” the method through the usage of dot notation. For global variables however, you are able to reference, read, or change them from almost any part of your code.
A variable that is local in scope can only be read while inside the method/function/code section that the variable resides in, and any attempts to read it without first gaining entry to the scope will fail (as mentioned, dot notation is a potential method for doing this, for cases where the local variable exists at the time of checking and has not been deleted as a result of the method/function completing)
A variable that is global on the other hand, can be referenced at almost any time, and is kept in memory for the duration of the program’s runtime from the point of declaration.
How do the global and nonlocal keywords work in Python, and in what situations might you use them?
keywords in python, when referenced are looked up in a particular order, with the first occurrence being the one assumed to be what you are looking for. This is known as the LEGB rule, and goes as follows:
Local: Check if the keyword exists in the context of the local scope of a method or function.
Enclosing: Exists only in the context of nested functions. Inner functions that are running are able to see keywords that exist one level above.
Global: keywords that are visible to any part of the program, and can be referenced at any time, from within a function/method or not.
Built-in: Keywords that belong to python itself, that are provided regardless of the code you have written. Keywords that are “part of” python.
This ordering of lookup means that if you define something in a local scope context that matches the name of a global scope entity exactly, the local one will take priority, as it will be discovered first. This flow of searching for keywords is also one of several reasons that naming variables the exact same name as one that already exists built-in is strongly discouraged, as doing so renders referencing the built-in one impossible.
In your own words, describe the purpose and importance of Big O notation in the context of algorithm analysis.
Big O notation serves the purpose of illustrating, in broad strokes, how an algorithm will “settle” by providing a measure of it’s final rate of growth, under worst circumstance. Does an algorithm increase in value at a constant rate? is the value doubling every time? A fractional multiplier? BigO helps to illustrate which of 5 fundamental forms an algorithm follows.
Constant O(1): unchanging, no addition or modification of the number in any way.
Logarithmic O(log n): Value increase follows a logarithmic scale.
Polynomial O(n^4): Value increase is polynomial, where the value of increase can vary due to numerous factors. the exponent listed should match with the number of factors involved in the algorithm’s increase.
Exponential O(5^n): Number’s rate of increase is exponentially linked to the value listed.
Factorial O(n!): value is being multiplied by every previous incarnation.
When programming, it is always best to target the big O value highest on this list, as these serve as measures of complexity as time goes on.
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.
Python has an importable math library that can be used for probability testing. A dice roll has 6 different possibilities, each of the whole numbers 1, 2, 3, 4, 5, and 6, each with equal probability. This makes the random library’s randint function ideal for this purpose.
import random
my_value = random.randint(1,6)
print my_value
An estimate of the total count of a specific equal-chance outcome can be determined by adding up the total possibilities across all trials, and dividing that value by the probability of achieving that result in any individual trial. So to see the number of times we might expect to roll a 3 over 1000 trials, our calculation would be somewhere along the lines of:
total_possibilities = (1000 * 6) #6000 in our case.
single_trial_odds = 1/6
expected_hit_count = total_possibilities * single_trial_odds
print(expected_hit_count)