A community for students.
Here's the question you clicked on:
 0 viewing
Kilgore
 2 years ago
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
Kilgore
 2 years ago
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

This Question is Closed

anonymous
 2 years ago
Best ResponseYou've already chosen the best response.0"divisors = ()" defines that it is a tuple. A tuple consists of a number of values separated by commas.

Kilgore
 2 years ago
Best ResponseYou've already chosen the best response.0Cool, 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

anonymous
 2 years ago
Best ResponseYou've already chosen the best response.0nope, 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) >>> ```

Kilgore
 2 years ago
Best ResponseYou've already chosen the best response.0Cool, thanks for the demo. Between that, and some more reading, I should be well on my way (at least with this question). Thanks again
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.