Here's the question you clicked on:
Kilgore
Hi, I'm looking at the code for lecture 4, the last program, and am seeking a bit of clarification on the construct: divisors = divisors + (i,) Why does this create a list instead of a sum? Is it because of the () and ,? or does it change the type to str? (full code below) Thanks! # calculates all possible factors of x x = 100 divisors = () for i in range(1, x): if x%i == 0:#if there's no remainder divisors = divisors + (i,)# create csv list of divisors used print divisors print divisors[0] + divisors[1] print divisors[2:4]#prints divisors in range between positi
"divisors = ()" defines that it is a tuple. A tuple consists of a number of values separated by commas.
Cool, Thanks. That clears it up. So, when you add to a tuple, you just put another piece of data into the list of values. Thanks again, Kilgore
nope, when you add to tuple you create a new tuple that includes the old and new item(s). ``` >>> >>> T = (1,2) >>> id(T) 42281760 >>> T1 = T + (3,) >>> id(T1) 42481184 >>> T (1, 2) >>> T1 (1, 2, 3) >>> T2 = T1 + () >>> T1 (1, 2, 3) >>> T2 (1, 2, 3) >>> T1 == T2 True >>> T1 is T2 False >>> id(T1), id(T2) (42481184, 42482624) >>> ```
Cool, thanks for the demo. Between that, and some more reading, I should be well on my way (at least with this question). Thanks again