Python Forum
Explanantion needed in part of code...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Explanantion needed in part of code...
#1
def reverse(word):
	x = ''
	for i in range(len(word)):
		x += word[len(word)-1-i]
	return x
- i do not understand the x += word(len(word)-1-i
- i am new to python and trying to understand the thinking behind this line of code. if someone could explain it to me, it would be very helpful. Thank you.
Reply
#2
len(word) is the number of letters in word
len(word)-1 is the index of the last letter in word. Python indexing starts with 0.
word[len(word)-1] is the last letter in word.

x = '' sets x to an empty string
x += 'a' is the same as x = x + 'a'
Adding two strings makes a new string that is the two strings concatenated. if x == 'b', x += 'a' is the same as x = 'b' + 'a' or x = 'ba'

x += word[len(word)-1] gets the last letter in word and adds it to the end of whatever is in 'x'.

This program loops through the letters in word starting at the end. Ig adds the letters to x. In the end, x has the same letters as word, but in reverse order.
Reply
#3
(Apr-21-2020, 03:05 AM)deanhystad Wrote: len(word) is the number of letters in word
len(word)-1 is the index of the last letter in word. Python indexing starts with 0.
word[len(word)-1] is the last letter in word.

x = '' sets x to an empty string
x += 'a' is the same as x = x + 'a'
Adding two strings makes a new string that is the two strings concatenated. if x == 'b', x += 'a' is the same as x = 'b' + 'a' or x = 'ba'

x += word[len(word)-1] gets the last letter in word and adds it to the end of whatever is in 'x'.

This program loops through the letters in word starting at the end. Ig adds the letters to x. In the end, x has the same letters as word, but in reverse order.

thank you for your response. but i see it has -1 and -i. i dont know what that really means. thanks again
Reply
#4
Use pen and paper. Write down a string and label each character with its index. Then, calculate the value of len(word) - 1 - i and see which character in the string that refers to. Repeat it for each iteration of the loop.
Reply
#5
Another 'cheap' way is to let Python do the lifting:

>>> word = 'hello'
>>> x = ''
>>> for i in range(len(word)):
...     indice = len(word) - 1 - i
...     letter = word[indice]
...     x += letter
...     print(f'{i}/{len(word)}: indice is {indice}, letter is {letter} and x is {x}')
... 
0/5: indice is 4, letter is o and x is o
1/5: indice is 3, letter is l and x is ol
2/5: indice is 2, letter is l and x is oll
3/5: indice is 1, letter is e and x is olle
4/5: indice is 0, letter is h and x is olleh
There are several other ways to achieve desired result:

>>> ''.join(word[-i] for i, _ in enumerate(word, start=1))
'olleh'
>>> ''.join(reversed(word))
'olleh'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
From: https://stackoverflow.com/questions/4841...-in-python

+= adds another value with the variable's value and assigns the new value to the variable.
>>> x = 3
>>> x += 2
>>> print x
5
-=, *=, /= does similar for subtraction, multiplication and division.

You could think of as a shortcode to use some operators but depends on what you trying to achieve.
Reply
#7
>>> a='hello'
>>> print(a[::-1])
olleh
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove part of the code hack3rcon 5 580 Jan-08-2024, 10:25 AM
Last Post: hack3rcon
  Code Assistance needed in saving the file MithunT 0 784 Oct-09-2022, 03:50 PM
Last Post: MithunT
  Could you explain each part of the code? Tsushida 2 1,466 Mar-20-2022, 08:19 AM
Last Post: Larz60+
  Help with writing or plan part of code Merlin_1 1 1,780 Aug-24-2020, 03:28 AM
Last Post: Larz60+
  Cant get grade part of code to work correctly Expel 5 2,594 Jul-10-2019, 05:09 AM
Last Post: perfringo
  Help needed for Understanding this code thepinecone461 4 2,911 Apr-03-2018, 06:55 PM
Last Post: buran
  What Does This Part of Close Alert Code Mean? digitalmatic7 2 2,551 Feb-13-2018, 03:48 AM
Last Post: digitalmatic7
  Simple code help needed Harveyyy 3 3,192 Feb-07-2018, 09:32 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020