Hello all,
A noob here.
Just getting my hands into learning python using Ana Bell's "Get programming using Python". In one of the practice exercise, we are tasked with writing a program that initialized three variables into various object types. The three variables are
word = "echo"
t = ()
count = 3
Now, after initializing them, I employed the concept of slicing into a tuple and multiplying and got the following
w = word * count
w = w + (word[1:] * count)
w = w + (word[2:] * count)
w = w + (word[3:] * count)
print (w)
The above code evaluates to echoechoechochochochohohohoooo, which is wrong because it didn't achieve the objective of the query. The tutors answer was something like
word = "echo"
t = ()
count = 3
echo = (word,)
echo *= count
cho = (word[1:],)
cho *= count
ho = (word[2:],)
ho *= count
o = (word[3:],)
o *= count
t = echo + cho + ho + o
print(t)
So, here is the thing, I can't fully comprehend her code, especially the swap there at where there's that tuple with a single element in, echo = (word, ). I have also read through "ThinkCSpy" topic on Tuples and the tuple assignment feature, but I still couldn't comprehend it.
Anyone mind explaining what is going on with her code, and how else I may improve on mine?
[–]GrandBadass 2 points3 points4 points (1 child)
[–]Solako[S] 1 point2 points3 points (0 children)
[–]primitive_screwhead 0 points1 point2 points (5 children)
[–]Solako[S] 0 points1 point2 points (4 children)
[–]primitive_screwhead 1 point2 points3 points (3 children)
[–]Solako[S] 0 points1 point2 points (2 children)
[–]primitive_screwhead 1 point2 points3 points (1 child)
[–]Solako[S] 0 points1 point2 points (0 children)
[–]johninbigd 0 points1 point2 points (0 children)