Feb-21-2021, 04:09 PM
Hi,
This is my second post on any sort of coding website and I'm an absolute beginner at programming. I'm trying to learn Python and am currently about halfway through an introductory book in which the author presents a block of code showing how to test the "evenness" of the randint function in Python using a dictionary (taking the range of numbers from 1 to 10 as an example). I'm having trouble figuring out exactly what is going on in this code. Here it is:
Here is what I think the code is doing:
The first line defines the "frequency" variable as an empty dictionary.
The second and third lines generate a random number between 1 and 10 1,000 times (I think).
The lines after this with the "if ... else" statement have me a bit stumped. It seems to me that the "if frequency.has_key(num)" criterion will always be met, so the resulting frequency of each number will always have 1 added to it. Why? Does this have something to do with the fact that the range function in Python increments by 1 by default? I'm having trouble understanding how the "if ... else" statement helps generate a frequency distribution for numbers 1 to 10.
Also, if I'm correct that the "if frequency.has_key(num)" criterion will always be met, is the "else" statement at the end of the block of code even necessary?
Thanks in advance for any help.
This is my second post on any sort of coding website and I'm an absolute beginner at programming. I'm trying to learn Python and am currently about halfway through an introductory book in which the author presents a block of code showing how to test the "evenness" of the randint function in Python using a dictionary (taking the range of numbers from 1 to 10 as an example). I'm having trouble figuring out exactly what is going on in this code. Here it is:
frequency = {} for i in range(1000): num = randint(1, 10) if frequency.has_key(num): frequency[num] = frequency[num] + 1 else: frequency[num] = 1 print frequencyThe output from this code is given in the book as follows:
Output:{1: 99, 2: 105, 3: 93, 4: 114, 5: 81, 6: 95, 7: 82, 8: 116, 9: 118, 10: 97}
The author writes that "It's not perfectly even, but most statisticians would be happy with our result."Here is what I think the code is doing:
The first line defines the "frequency" variable as an empty dictionary.
The second and third lines generate a random number between 1 and 10 1,000 times (I think).
The lines after this with the "if ... else" statement have me a bit stumped. It seems to me that the "if frequency.has_key(num)" criterion will always be met, so the resulting frequency of each number will always have 1 added to it. Why? Does this have something to do with the fact that the range function in Python increments by 1 by default? I'm having trouble understanding how the "if ... else" statement helps generate a frequency distribution for numbers 1 to 10.
Also, if I'm correct that the "if frequency.has_key(num)" criterion will always be met, is the "else" statement at the end of the block of code even necessary?
Thanks in advance for any help.