Re: [Tutor] Help
On 07/03/2015 19:04, Alan Gauld wrote: On 07/03/15 15:39, elie khairallah wrote: hello , I would like to know if theres a way to change a variable's name after every iteration for example I want to make a function that stores a number in x_1 if i=1 and in x_2 if i=2. To be more precise: i=1 def f(n): while i Others have already suggested a list, which IMHO is the best option. However if for some reason the zero based index of a list is an issue for you then you could use your naming scheme in a dictionary: def f(n): values = {} root = 'x_' for n in range(1,n+1): values[root+str(n)] = n**2 return values You can then access the values with, for example: print (values['x_2']) But unless you are reading the variable names from a file/network or a user then the numerical index of a list will usually be easier to work with. If zero based indexing is an issue I'd just write:- values = [None] to start with. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help
On 07/03/15 15:39, elie khairallah wrote: hello , I would like to know if theres a way to change a variable's name after every iteration for example I want to make a function that stores a number in x_1 if i=1 and in x_2 if i=2. To be more precise: i=1 def f(n): while i Others have already suggested a list, which IMHO is the best option. However if for some reason the zero based index of a list is an issue for you then you could use your naming scheme in a dictionary: def f(n): values = {} root = 'x_' for n in range(1,n+1): values[root+str(n)] = n**2 return values You can then access the values with, for example: print (values['x_2']) But unless you are reading the variable names from a file/network or a user then the numerical index of a list will usually be easier to work with. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help
> You could certainly use I as an index into a list called x. This > doesn't look like a good idea though Can you explain more what's problematic with a list? My best understanding so far of the problem is that the original questioner is trying to compute a tabulation of results. For example, I think they're trying to compute the squares of the numbers 1 up to some large number like 20. In that scenario, lists seem like a reasonable representation for such a table. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help
On Sat, Mar 7, 2015 at 7:39 AM, elie khairallah wrote: > hello , I would like to know if theres a way to change a variable's name > after every iteration for example I want to make a function that stores a > number in x_1 if i=1 and in x_2 if i=2. Conceptually, I think you're looking for a list. The "subscript" you're using can be implemented as a list indexing operation. e.g.: x = [0, 0] x[0] = 5 x[1] = 4 print x[0] print x[1] ## The index we're using can be an arbitrary expression, not just a fixed number: messages = [0] * 5 for i in range(5): messages[i] = ("This is %d" % i) print messages ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help
On Sat, Mar 7, 2015 at 1:28 PM, Danny Yoo wrote: >> You could certainly use I as an index into a list called x. This >> doesn't look like a good idea though > > > Can you explain more what's problematic with a list? > > My best understanding so far of the problem is that the original > questioner is trying to compute a tabulation of results. For example, > I think they're trying to compute the squares of the numbers 1 up to > some large number like 20. In that scenario, lists seem like a > reasonable representation for such a table. Sorry for clumsy answer. I was trying to say that using a list with i as an index was a solution rather than trying to build names on the fly with some text concatenated to the value of i -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Daft subjects (Was Help)
On 07/03/2015 15:39, elie khairallah wrote: hello , I would like to know if theres a way to change a variable's name after every iteration for example I want to make a function that stores a number in x_1 if i=1 and in x_2 if i=2. To be more precise: i=1 def f(n): while i Why bother? What's wrong with this? def f(i, n): values = [] for x in range(i, n+1, 1): values.append(x**2) return values -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help
On Sat, Mar 7, 2015 at 10:39 AM, elie khairallah wrote: > hello , I would like to know if theres a way to change a variable's name > after every iteration for example I want to make a function that stores a > number in x_1 if i=1 and in x_2 if i=2. > To be more precise: > i=1 > def f(n): >while i x_i = i**2 ##this writing is wrong I would like here to have > x_1 or x_2 ... > (depending on i) > return (x_1,x_2,x_3 .) > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor You could certainly use I as an index into a list called x. This doesn't look like a good idea though -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Help
hello , I would like to know if theres a way to change a variable's name after every iteration for example I want to make a function that stores a number in x_1 if i=1 and in x_2 if i=2. To be more precise: i=1 def f(n): while ihttps://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String method "strip()" not working
On 07/03/15 15:17, Mark Lawrence wrote: S.strip([chars]) -> string or unicode Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping Presumably Python 2 (or earlier Python 3) help output. In Python 3 strings are always unicode, so that last line has gone. Good catch, especially since the OP specifically said v3. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String method "strip()" not working
On 07/03/2015 14:54, Alan Gauld wrote: On 07/03/15 13:15, Akash Shekhar wrote: I am trying to learn how to use strip() method. It is supposed to cut out all the whitespace as I read in the tutorial. Read it again more closely. --- Help on built-in function strip: strip(...) S.strip([chars]) -> string or unicode Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping Presumably Python 2 (or earlier Python 3) help output. In Python 3 strings are always unicode, so that last line has gone. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String method "strip()" not working
On 03/07/2015 08:15 AM, Akash Shekhar wrote: I am trying to learn how to use strip() method. It is supposed to cut out all the whitespace as I read in the tutorial. But the code is not working. Here's my code: sentence = "Hello, how are you?" print(sentence) print(sentence.strip()) input("\n\nPress enter key to exit.") Here's it's output: Hello, how are you? Hello, how are you? Press enter key to exit. Both results are same. P.S.: I am using Python 3.1 IDLE on Windows 7. Thanks for mentioning the python version and OS. You don't have any whitespace at the beginning nor end of the string bound to sentence. So there's nothing to strip. By the way, if you're checking such a function, it's sometimes more informative to write print(repr(sentence)) which will add quotes at begin and end, and show newlines and tabs as escape sequences. If you only want to strip from one end of the string, you'd use lstrip() or rstrip(). If you're also trying to remove characters from the middle of the string, you might use translate() or the string method replace(). For example, to remove all spaces from a string, use sentence.replace(" ", "") -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String method "strip()" not working
On 07/03/15 13:15, Akash Shekhar wrote: I am trying to learn how to use strip() method. It is supposed to cut out all the whitespace as I read in the tutorial. Read it again more closely. --- Help on built-in function strip: strip(...) S.strip([chars]) -> string or unicode Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping -- So it only strips leading and training whitespace, not the whitespace inside the string. (Use str.translate() for that) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] String method "strip()" not working
I am trying to learn how to use strip() method. It is supposed to cut out all the whitespace as I read in the tutorial. But the code is not working. Here's my code: sentence = "Hello, how are you?" > > >> print(sentence) > > >> print(sentence.strip()) > > >> input("\n\nPress enter key to exit.") > > > Here's it's output: Hello, how are you? > Hello, how are you? > > Press enter key to exit. > Both results are same. P.S.: I am using Python 3.1 IDLE on Windows 7. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor