Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Kilgore

  • one year 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
    • one year 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
    • one year 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
    • one year 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
    • one year 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

Ask a Question
Find 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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.