In ordinary math you're given the relationship of the equation and you just have to plug in the numbers. Do people going to the beach make the temperature go up? Or is it the other way around? In this example it is obvious, but lots of times it isn't. Sometimes there is a relationship, sometimes there is not, and even when there is a relationship it isn't aways easy to figure out what it is. In statistics you're basically given two or more variables (x, y, etc) and you have to figure out if there is a relationship among them. In ordinary mathematics you're given a relationship in the form of an equation (x+y = z) that you can then plug numbers into and get an answer. In this case there obviously is, but in other examples the relationship isn't so obvious. For example, if the temperature goes up on the thermometer, and you count more people going to the beach, then you might want to determine whether there is a relationship between the two things. trying to do it manually with a negative increment.Statistics attempt to establish the relationship between one or more measured things. ![]() If you want to loop over the index numbers of a string or list backwards, it's easier to use reversed() above, vs. > list(range(4, 5, -2)) # beyond the stop is omitted > list(range(5, 5, -2)) # equal to stop is omitted As always, numbers reaching or beyond the stop are omitted, but now step is decreasing. If the step is negative, the range decreases from start down to stop. As before, the stop number itself is always omitted. Once the number is equal or goes beyond the stop, the range ends. The difference is the "step" amount between numbers is now custom. The 3 parameter form begins with start number, up to but no including the stop number as usual. Range(start, stop, step) - 3 Parameter Form Mnemonic: the "stop" number is strong, so as soon as the numbers hit or exceed the stop the range is done (even if the start number initiates things in that position). > list(range(5, 5)) # start >= stop, no numbers Range with 2 parameters specifies a start number other than 0, but is otherwise like the 1 parameter form above, going up to but not including the stop number. ![]() This works nicely with range() to go over the regular numbers in reverse order: The reversed() function takes in a linear collection and returns a reversed form of it. Probably the second most common problem is to go through the standard index numbers, but in reverse order. ![]() What is range(0)? Well range(n) returns n numbers, so this case returns no numbers at all - like the empty list. This use of list() is only for printing, not needed to use range() in a loop. ![]() For cosmetic reasons in the examples below, the call of the range() function is inside a list() so the numbers will print out. This is perfect for generating the index numbers into, for example, a string.īelow are some more examples calling range(). Or put another way, range(n) returns a series of n numbers, starting with 0`. The most common form is range(n), for integer n, which returns a numeric series starting with 0 and extending up to but not including n, e.g. The range() function can be called in a few different ways. This is very useful, since the numbers can be used to index into collections such as string. The python range() function creates a collection of numbers on the fly, like
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |