Here's the question you clicked on:

55 members online
  • 0 replying
  • 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

  • This Question is Closed
  1. Richard_C
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    "divisors = ()" defines that it is a tuple. A tuple consists of a number of values separated by commas.

  2. Kilgore
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  3. bwCA
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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) >>> ```

  4. Kilgore
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  5. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy