Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNicholas Gaerlan
9,501 PointsDo sets have a default order to them?
I noticed that the output of this set is in order, but you mentioned sets don't have an order. Is there a default order that happens to correlate with numerical order in say... unicode or some sort of underlying hex value? Also in the example where this is presented as a list, I noticed that it starts with 2... so I would expect the set version to also start with 2 and since the next nineteen elements are unique, the set version of that list would read 2, 3, 4, 5, 6, 7, 8... 19, 20, 1.
{round(x/y) for y in range(1,11) for x in range (2, 21)} {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
lastly, the calculation of x/y rounded to the nearest int seems a little off in the output
[round(x/y) for y in range(1,11) for x in range (2, 21)] [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1, 2, 2, 2, 3, 4, 4, 4, 5,...
I get that the first time around numerator x loops 2 through 20 while the divisor of y = 1. Then the second time looping numerator x loops 2 through 20 again this time the divisor y is = 2, therefore I would expect the sequence to read 1, 2, 2, 3, 3, 4, 4, 5 but somehow 5/2 = 2.5 is rounded down? Is this due to some minor variance while converting float to int maybe? Is there a way to correct for it? I can imagine some disastrous outcomes due to a rounding error.
2 Answers
Ari Misha
19,323 PointsA set contains an "unordered" collection of "unique" and "immutable" objects. The set data type is, as the name implies, a Python implementation of the sets as they are known from mathematics. Just like dictionaries , sets are a collection of data and they dont have an order to 'em. Sets are very useful when you want your data collection to be unique and immutable. You can experiment on your own with sets in Python shell and get your hands dirty.
I hope it helped. (:
Daniel Phillips
12,632 PointsFrom the Python documentation, everything is working as intended.
round(number[, ndigits]) Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.
For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if ndigits is omitted or None. Otherwise the return value has the same type as number.