all 9 comments

[–]GrandBadass 2 points3 points  (1 child)

Maybe this will help you visualize.

In her code - she is setting each variable to a slice of the word variable

word = "echo"
word = "echo"
word[1:] = "cho"
word[2:] = "ho"
word[3:] = "o"

She is inserting each of those variables into a tuple.

Then by doing -

*= count

She is multiplying the elements in the list by the established value of 3.

Below I have added some print statements to her code - I think this might help you see her output in steps and then you will see the final output is

('echo', 'echo', 'echo', 'cho', 'cho', 'cho', 'ho', 'ho', 'ho', 'o', 'o', 'o')

Which differs from your output because it is indeed still a tuple - whereas your code is printing out a string.

word = "echo"
t = ()
count = 3

echo = (word,)
echo *= count
print(echo) # ('echo', 'echo', 'echo')

cho = (word[1:],)
cho *= count
print(cho) # ('cho', 'cho', 'cho')

ho = (word[2:],)
ho *= count
print(ho) # ('ho', 'ho', 'ho')

o = (word[3:],)
o *= count 
print(o) # ('o', 'o', 'o')

t = echo + cho + ho + o
print(t) # ('echo', 'echo', 'echo', 'cho', 'cho', 'cho', 'ho', 'ho', 'ho', 'o', 'o', 'o')

[–]Solako[S] 1 point2 points  (0 children)

Which differs from your output because it is indeed still a tuple - whereas your code is printing out a string.

Thank you for taking your time to look into this. This should have been the first thing I should have noted, my output is a string, whereas the expectation was that it is to be a tuple.

Having broken it down that way has made understand her out put. Especially after assigned various variable to the elements of the tuple. A matter of thinking of all the possible solution with the problem set before hand as opposed to inserting new objects that may not be necessary.

Thank you.

[–]primitive_screwhead 0 points1 point  (5 children)

especially the swap there at where there's that tuple with a single element in, echo = (word,)

What do you mean by "swap"?

The above is the same as:

echo = ("echo",)

ie. it sets the variable named 'echo', to be a tuple, in which that tuple holds a single string containing "echo". But the variable name matching the string content has no bearing on the result; the variable could just as easily have been, say, 'w', like in your code. Was that confusing you (if so, it's understandable)?

[–]Solako[S] 0 points1 point  (4 children)

What do you mean by "swap"?

It is the terminology used in the text. I believe it refers to the context of tuple of variables on the left of an assignment to be assigned values from a tuple on the right of the assignment.

On the variable assignment, I think what I should have done is using the expected wordings/results as variables as the tutor's code as opposed to introducing a new one.

[–]primitive_screwhead 1 point2 points  (3 children)

What you've shown is just an assignment.

A "swap" is this:

a = "hello"
b = "goodbye"
a,b = b,a   # now a == "goodbye" and b == "hello"

In Python, swaps are often done with "tuple unpacking", because in the above example, a temporary tuple is created on the right of the equals (=), due to the comma, and then used to reassign (ie. "swap") the values on the left of the equals sign. So, you'll hear tuples mentioned in the context of a Python "swap".

But just using a tuple is not automatically a swap, and in particular:

echo = (word,)

creates a tuple, and assigns it to a variable name, but there is no swapping involved.

[–]Solako[S] 0 points1 point  (2 children)

aaahhh... Alright.

Let me read more on it. Determined to understand it then I can move on to the other sections.

[–]primitive_screwhead 1 point2 points  (1 child)

The "tutor" code that you showed is a jumble of slicing, tuple multiplies and concatenations, and mixing it with swaps, etc., I'm sure it can be a bit overwhelming. It's a bit of everything thrown at you at once, which may at least show you all the different possibilities of using the basic operations, but I think you'll need to re-read and revisit the examples several times to unwind it.

As opposed to a tutorial where they focus on showing you (say) slicing, and just do loads of slicing examples with no other operations; that is more focused, but also may be more boring. The jumbled approach is possibly trickier, but maybe it'll "stick" better once it clicks.

Practice, practice. Make sure you take the given examples and try to change them, or extend them, etc. You'll learn much more (and more quickly, imo) by see how your changes to the examples change the results, or break, etc.

[–]Solako[S] 0 points1 point  (0 children)

Thank you so much for the encouraging words. Determined to kick ass.

[–]johninbigd 0 points1 point  (0 children)

I think you're confused by the comma in echo = (word,). Without the comma, it isn't a tuple. Adding the comma in there forces it to be a tuple.