[Tutor] doubt in a program
Hi, I am a new beginner in programming. I saw a code (given below) to reverse string. newstring = '' oldstring = 'Newton' for char in oldstring: newstring = char + newstring print(newstring) Could someone explain how it is traversing to get the string reversed? As the new character is adding the char in the new string but how is it getting reversed without any line giving the char number to be traversed in reverse order. Thanks, Vinod ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] doubt in a program
Thanks a lot Nitin. I misunderstood the "char + newstring". As a newbie to programming as well as newbie to Python trying to grasp basics. Sure will need the built in functions present for different things in Python. Any suggestion good book for python? Thanks, Vinod Bhaskaran On Mon, Jan 29, 2018, 2:47 PM Nitin Madhok wrote: > Vinod, > > First time it loops, > newstring = ‘’ > oldstring = ‘Newton’ > char = ‘N’ > char + newstring = ‘N’ + ‘’ = ‘N’ > > Second time it loops, > newstring = ‘N’ > oldstring = ‘Newton’ > char = ‘e’ > char + newstring = ‘e’ +’N’ = ‘eN’ > > Third time it loops, > newstring = ‘eN’ > oldstring = ‘Newton’ > char = ‘w’ > char + newstring = ‘w’ +’eN’ = ‘weN’ > > Fourth time it loops, > newstring = ‘weN’ > oldstring = ‘Newton’ > char = ‘t’ > char + newstring = ‘t’ +’weN’ = ‘tweN’ > > Fifth time it loops, > newstring = ‘tweN’ > oldstring = ‘Newton’ > char = ‘o’ > char + newstring = ‘o’ +’tweN’ = ‘otweN’ > > Sixth/Final time it loops: > newstring = ‘otweN’ > oldstring = ‘Newton’ > char = ‘n’ > char + newstring = ‘n’ +’otweN’ = ‘notweN’ > > newstring after loop finishes will be ‘notweN’ which is the reverse of > ‘Newton’ > > Let me know if that explanation helps you understand it better. There are > built in functions to reverse a string that are more efficient than this > approach that you should be using to reverse a string. > > -- > > Thanks, > Nitin Madhok > Clemson University > > CONFIDENTIALITY NOTICE > This e-mail, and any attachments thereto, is intended only for use by the > addressee(s) named herein and may contain privileged and/or confidential > information. If you are not the intended recipient of this e-mail, any > dissemination, distribution or copying of this e-mail, and any attachments > thereto, is strictly prohibited. If you have received this e-mail in error, > please immediately notify the sender by e-mail or telephone and permanently > delete all copies of this e-mail and any attachments. > > > On Jan 29, 2018, at 1:42 AM, vinod bhaskaran > wrote: > > > > Hi, > > > > I am a new beginner in programming. > > I saw a code (given below) to reverse string. > > > > newstring = '' > > oldstring = 'Newton' > > for char in oldstring: > > newstring = char + newstring > > print(newstring) > > > > > > > > Could someone explain how it is traversing to get the string reversed? > > > > As the new character is adding the char in the new string but how is it > > getting reversed without any line giving the char number to be traversed > in > > reverse order. > > > > Thanks, > > Vinod > > ___ > > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] can someone explain the reason for error
Hi, I am a beginner level programmer and in one assignment the question given is:to remove ',' from a list after getting a comma separated input from console. I gave the below (most print statements are for reference except the last print statement). but i get the attached error. can someone explain why this error nd how to rectify? inputdata = input ('Enter comma separated data \n') type(inputdata) inputlist = list(inputdata) print(inputlist) a = len(inputdata) print(a) print ('') for b in range(0,a): print(a) a = a - 1 print(inputlist) inputlist.remove(',') print(inputlist) Thanks, Vinod ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Doubt in list comprehension
Hi All, I am a beginner programmer and i wrote a small program (as per a assignment i saw) as below: newlist = [] for a in range(2,5): for b in range (0,3): newlist.append([a]) a = a + 1 print(newlist) it gives the expected output as below: [[2], [3], [4], [3], [4], [5], [4], [5], [6]] but when i try list comprehension i am not able to get it correctcan someone please suggest where the (a=a+1) should be placed in a list comprehension Thanks, Vinod Bhaskaran ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Doubt in list comprehension
Thanks Peter. Shall figure it out with the below hint. I had a hunch am wrong but was not sure where to put in . Thanks, Vinod Bhaskaran On Fri, Feb 23, 2018, 7:11 PM Peter Otten <__pete...@web.de> wrote: > vinod bhaskaran wrote: > > > Hi All, > > > > I am a beginner programmer and i wrote a small program (as per a > > assignment i saw) as below: > > > > newlist = [] > > for a in range(2,5): > > for b in range (0,3): > > newlist.append([a]) > > a = a + 1 > > print(newlist) > > > > it gives the expected output as below: > > [[2], [3], [4], [3], [4], [5], [4], [5], [6]] > > > > but when i try list comprehension i am not able to get it correctcan > > someone please suggest where the (a=a+1) should be placed in a list > > comprehension > > You canot sneak a statement like > > > a = a + 1 > > into a list comprehension, you have to modify the expressions. Given > > [[...] for a in range(2, 5) for b in range(3)] > > what expression replacing the ... would give the expected result? > > Hint: it depends on both a and b. > > Once you have figured it out you can try and reshuffle it a bit into > > [[b] for a in range(2, 5) for b in range(...)] > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor