Quoting [EMAIL PROTECTED]: > Send Tutor mailing list submissions to > tutor@python.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > [EMAIL PROTECTED] > > You can reach the person managing the list at > [EMAIL PROTECTED] > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Changing lists in place (Paul D. Kraus) > 2. Re: Changing lists in place (Paul D. Eden) > 3. Re: Changing lists in place (Bob Gailer) > 4. Re: Meaning of %g ? (Terry Carroll) > 5. Re: Changing lists in place (Kent Johnson) > 6. Re: Tutor Digest, Vol 26, Issue 55 (Carroll, Barry) > 7. Re: Changing lists in place (Paul D. Eden) > 8. Re: functions in Python (Alan Gauld) > 9. Raw Bits! (Control characters ahoy!) (doug shawhan) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Mon, 17 Apr 2006 14:02:37 -0400 > From: "Paul D. Kraus" <[EMAIL PROTECTED]> > Subject: [Tutor] Changing lists in place > To: tutor@python.org > Message-ID: > <[EMAIL PROTECTED]> > Content-Type: text/plain; charset="iso-8859-1" > > I have a list that I want to change in a for loop. It changes in the loop > but the changes are not persistant outside of the loop. > I thought lists were mutuable objects so I am a bit confused. > > Sample Code.... > #!/usr/bin/env python > """ Testing lists """ > > mylist = [ 'One ', ' two', ' three ' ] > print mylist > for element in mylist: > element = element.strip() > print "<>" + element + "<>" > print mylist > > > Sample Output.... > ['One ', ' two', ' three '] > <>One<> > <>two<> > <>three<> > ['One ', ' two', ' three '] > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: > http://mail.python.org/pipermail/tutor/attachments/20060417/b1d0ae57/attachment-0001.html > > ------------------------------ > > Message: 2 > Date: Mon, 17 Apr 2006 11:11:44 -0700 > From: "Paul D. Eden" <[EMAIL PROTECTED]> > Subject: Re: [Tutor] Changing lists in place > To: [EMAIL PROTECTED] > Cc: tutor@python.org > Message-ID: <[EMAIL PROTECTED]> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > Lists are mutable, you are right. > > But the code you gave does not change the list. It changes the variable > element which is separate from the list myList. > > If you want to change the list try something like this: > > mylist = [ 'One ', ' two', ' three ' ] > print mylist > newlist = [] > for element in mylist: > element = element.strip() > newlist.append(element) > print "<>" + element + "<>" > print newlist > > OR > > mylist = [ 'One ', ' two', ' three ' ] > print mylist > mylist = [element.strip() for element in mylist] > for element in mylist: > print "<>" + element + "<>" > print mylist > > Paul > > Paul D. Kraus wrote: > > I have a list that I want to change in a for loop. It changes in the > > loop but the changes are not persistant outside of the loop. > > I thought lists were mutuable objects so I am a bit confused. > > > > Sample Code.... > > #!/usr/bin/env python > > """ Testing lists """ > > > > mylist = [ 'One ', ' two', ' three ' ] > > print mylist > > for element in mylist: > > element = element.strip() > > print "<>" + element + "<>" > > print mylist > > > > > > Sample Output.... > > ['One ', ' two', ' three '] > > <>One<> > > <>two<> > > <>three<> > > ['One ', ' two', ' three '] > > > > > > ------------------------------------------------------------------------ > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > ------------------------------ > > Message: 3 > Date: Mon, 17 Apr 2006 11:17:18 -0700 > From: Bob Gailer <[EMAIL PROTECTED]> > Subject: Re: [Tutor] Changing lists in place > To: [EMAIL PROTECTED] > Cc: tutor@python.org > Message-ID: <[EMAIL PROTECTED]> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > Paul D. Kraus wrote: > > I have a list that I want to change in a for loop. It changes in the > > loop but the changes are not persistant outside of the loop. > > I thought lists were mutuable objects > They are. > > so I am a bit confused. > > > > Sample Code.... > > #!/usr/bin/env python > > """ Testing lists """ > > > > mylist = [ 'One ', ' two', ' three ' ] > > print mylist > > for element in mylist: > element is a variable to which the successive items in mylist are > assigned. element has no "magic" connection to the list. > > element = element.strip() > In order to change a list item you must do something like: > mylist[itemIndex] = element.strip() > > So you need both the item's value and its position in the list. That's > what enumerate is for: > > for itemIndex, element in enumerate(mylist): > mylist[itemIndex] = element.strip() > > [snip] > > > ------------------------------ > > Message: 4 > Date: Mon, 17 Apr 2006 11:35:13 -0700 (PDT) > From: Terry Carroll <[EMAIL PROTECTED]> > Subject: Re: [Tutor] Meaning of %g ? > To: tutor@python.org > Message-ID: <[EMAIL PROTECTED]> > Content-Type: TEXT/PLAIN; charset=US-ASCII > > On Mon, 17 Apr 2006, Alan Gauld wrote: > > > >>>> a = 123456.78 > > >>>> print "%g\n%e\n%f" % (a,a,a) > > > 123457 > > > 1.234568e+005 > > > 123456.780000 > > > > >Float number loses digits and becomes integer in the output ? > > > > Yep, I am rapidly coming to the comnclusion that %g is broken > > in Python. I must do some tests with gcc to see what it does > > with %g, it may be the wierd behaviour is coming from there. > > I've never used %g with gcc... > > FWIW, (Cygwin) Perl gives a similar result: > > > perl -e 'my $a = 123456.78;printf ("%g\n%e\n%f", $a, $a, $a);' > 123457 > 1.234568e+05 > 123456.780000 > > The only difference being the two-digit vs. three-digit exponent on %e > (Python's 1.234568e+005 vs. Perl's 1.234568e+05). > > Hmm.. Upon further review, Cygwin Perl gives 1.234568e+05; while > Activestate Perl gives 1.234568e+005. (For reasons that elude me, > Activestate Perl won't do the command-line version, but I don't care > enough to track it down.) > > > > ------------------------------ > > Message: 5 > Date: Mon, 17 Apr 2006 15:01:59 -0400 > From: Kent Johnson <[EMAIL PROTECTED]> > Subject: Re: [Tutor] Changing lists in place > Cc: tutor@python.org > Message-ID: <[EMAIL PROTECTED]> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > Paul D. Eden wrote: > > Lists are mutable, you are right. > > > > But the code you gave does not change the list. It changes the variable > > element which is separate from the list myList. > > > > If you want to change the list try something like this: > > > > mylist = [ 'One ', ' two', ' three ' ] > > print mylist > > newlist = [] > > for element in mylist: > > element = element.strip() > > newlist.append(element) > > print "<>" + element + "<>" > > print newlist > > > > OR > > > > mylist = [ 'One ', ' two', ' three ' ] > > print mylist > > mylist = [element.strip() for element in mylist] > > for element in mylist: > > print "<>" + element + "<>" > > print mylist > > Neither of these changes the original list either. They both create new > lists with the desired contents. The second example binds the new list > to the old name, but it is still a new list. In many cases this is fine, > but the distinction is important. For example if you are writing a > function that modifies a list passed to it, these solutions won't work. > Bob's solution using enumerate() is the simplest way to modify a list in > place. > > Kent > > > > ------------------------------ > > Message: 6 > Date: Mon, 17 Apr 2006 11:48:08 -0700 > From: "Carroll, Barry" <[EMAIL PROTECTED]> > Subject: Re: [Tutor] Tutor Digest, Vol 26, Issue 55 > To: <tutor@python.org> > Message-ID: > <[EMAIL PROTECTED]> > Content-Type: text/plain; charset="US-ASCII" > > Payal: > > > -----Original Message----- > > Date: Mon, 17 Apr 2006 13:24:31 -0400 > > From: Payal Rathod <[EMAIL PROTECTED]> > > Subject: Re: [Tutor] functions in Python > > To: Steve Nelson <[EMAIL PROTECTED]> > > Cc: "Python\[Tutor\]" <tutor@python.org> > > Message-ID: <[EMAIL PROTECTED]> > > Content-Type: text/plain; charset=us-ascii > > > > On Mon, Apr 17, 2006 at 05:42:05PM +0100, Steve Nelson wrote: > > > When you define a function, you are writing a block of code which > you > > > can ask to perform a task. The task may be simple, and not require > > > any additional information, or it may be more complex and need > > > information. > > > > What is the difference between, > > > > >>> def f(x): > > ... return x > > ... > > >>> f(4) > > 4 > > > > >>> def f(x): > > ... print x > > ... > > >>> f(4) > > 4 > > > > Both give same results. So, why return statement is needed? > > With warm regards, > > -Payal > > Let's use your two functions named and see what we can do with them. In > order to use both function definitions in the same interactive session, > I will name the first 'f1' and the second 'f2'. > > >>>>>>>>>> > >>> def f1(x): > ... return x > ... > >>> def f2(x): > ... print x > ... > >>>>>>>>>> > > Okay so far? > > Now, let's use each of these functions in a simple computation. To > start, we'll just add two integers (called literals in programming > jargon), > > >>>>>>>>>> > >>> 4 + 4 > 8 > >>>>>>>>>> > > The '+' operator takes two integers, adds them together and gives back > (returns) the result. (Actually, the '+' operator can do more than > this, but this is enough for this example.) > > Now, we'll do the same calculation using f1 instead of the literals. > > >>>>>>>>>> > >>> f1(4) + f1(4) > 8 > >>>>>>>>>> > > This worked as expected: f1 sent back (returned) the value we gave it > (the argument 4) and the '+' operator did the addition. > Questions? > > Now, let's do the same calcuation once more, using f2 this time. > > >>>>>>>>>> > >>> f2(4) + f2(4) > 4 > 4 > Traceback (most recent call last): > File "<input>", line 1, in ? > TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType' > >>>>>>>>>> > > This time the operation failed. The f2 function did what we wrote it to > do: it printed the argument to the Standard Output device (also called > STDOUT), the interactive shell window in this case. But, as the error > message indicates, it also RETURNED a value: in this case None, which > has the type NoneType. The '+' operator then tried to add the two None > values together and failed. > > The examples above show that, although the output of f1 and f2 look the > same, it is actually different. The 'f2' function displays its argument > for humans to read. The 'f1' function makes the argument available for > other code to use. That is the purpose of the 'return' statement: to > make the results of the function available to other code. > > It is important to remember that, in Python, EVERY function returns a > value. If a 'return' statement is included in the function, the value > the result of the expression associated with the statement (in your > example, 4). If no 'return' statement is included, the function returns > None. > > I hope this makes sense to you. If not, keep asking questions. > > Best Regards, > > Barry > [EMAIL PROTECTED] > 541-302-1107 > ________________________ > We who cut mere stones must always be envisioning cathedrals. > > -Quarry worker's creed > > > > > ------------------------------ > > Message: 7 > Date: Mon, 17 Apr 2006 12:04:57 -0700 > From: "Paul D. Eden" <[EMAIL PROTECTED]> > Subject: Re: [Tutor] Changing lists in place > To: Kent Johnson <[EMAIL PROTECTED]> > Cc: tutor@python.org > Message-ID: <[EMAIL PROTECTED]> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > Very true. > > Paul > > Kent Johnson wrote: > > Paul D. Eden wrote: > > > >>Lists are mutable, you are right. > >> > >>But the code you gave does not change the list. It changes the variable > >>element which is separate from the list myList. > >> > >>If you want to change the list try something like this: > >> > >>mylist = [ 'One ', ' two', ' three ' ] > >>print mylist > >>newlist = [] > >>for element in mylist: > >> element = element.strip() > >> newlist.append(element) > >> print "<>" + element + "<>" > >>print newlist > >> > >>OR > >> > >>mylist = [ 'One ', ' two', ' three ' ] > >>print mylist > >>mylist = [element.strip() for element in mylist] > >>for element in mylist: > >> print "<>" + element + "<>" > >>print mylist > > > > > > Neither of these changes the original list either. They both create new > > lists with the desired contents. The second example binds the new list > > to the old name, but it is still a new list. In many cases this is fine, > > but the distinction is important. For example if you are writing a > > function that modifies a list passed to it, these solutions won't work. > > Bob's solution using enumerate() is the simplest way to modify a list in > > place. > > > > Kent > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > ------------------------------ > > Message: 8 > Date: Mon, 17 Apr 2006 21:05:02 +0100 > From: "Alan Gauld" <[EMAIL PROTECTED]> > Subject: Re: [Tutor] functions in Python > To: "Payal Rathod" <[EMAIL PROTECTED]>, "Steve Nelson" > <[EMAIL PROTECTED]> > Cc: "Python\[Tutor\]" <tutor@python.org> > Message-ID: <[EMAIL PROTECTED]> > Content-Type: text/plain; format=flowed; charset="iso-8859-1"; > reply-type=original > > Wow! I checked the list at lunchtime and there were only a few things > here, then I check again now and lots of stuff from my tutor! Fortunately > most of it has been answered already - thanks folks - but I feel honour > bound to contribute something... > > > What is the difference between, > > > >>>> def f(x): > > ... return x > > >>>> def f(x): > > ... print x > > ... > >>>> f(4) > > 4 > > > Both give same results. So, why return statement is needed? > > As has been stated the print displays on the output > The return sends the value of the function back to the caller. > If the caller is the interactive prompt(as here) the prompt > prints the value so the effect is the same. > > If you need to save the result for later use then you must > store it in a variable. You cannot do that if the function > just prints the result - the value gets lost when the function > ends. > > Try this: > > >>> def f(x): return x > ... > >>> def g(x): print x > ... > >>> fVal = f(4) > >>> gVal = g(4) > 4 > > Now notice that the two functoons behave differently > f() doesn't display a value but g does because of the > print statement. So far g seems to win. But... > > >>> print fVal > 4 > >>> print gVal > None > >>> > > Now f() wins because we have stored its value in fVal > and can use it over and over again as we wish. > > The reason gVal has stored None is because any function that > does not return an explicit value is treated as retiurning the > special value None. Since g() only printed the parameter 4 > but did not return it gVal stored a None value. > > If we return the value of f() we can display it any time we > want by using print: > > >>> print f(4) > 4 > >>> > > So we do not lose anything by using return instead of print > but we gain a lot by being able to store the returned value. > > HTH, > > Alan G > Author of the learn to program web tutor > http://www.freenetpages.co.uk/hp/alan.gauld > > > > > ------------------------------ > > Message: 9 > Date: Mon, 17 Apr 2006 16:40:45 -0500 > From: "doug shawhan" <[EMAIL PROTECTED]> > Subject: [Tutor] Raw Bits! (Control characters ahoy!) > To: tutor@python.org > Message-ID: > <[EMAIL PROTECTED]> > Content-Type: text/plain; charset="iso-8859-1" > > I am in the middle of a project that requires me to send and retrieve > information from a machine connected to a serial port. My problems are > these: > > 1. I cannot send control characters > 2. I cannot read data streaming from the serial port > > > I have been doing fine with: > > os.system("echo '5' >/dev/tty00") > os.system("echo '8' >/dev/tty00") > > > and the like to the remote machine, but I need to be able to send control > characters the same way to scroll through menus. I have tried pyserial and > pexpect with mixed (mostly horrible) results (not the fault of the module > builders!), thus far the simple act of echoing the strings to the serial > port is the only reliable method I have found. > > Reading from the serial port has also been problematic. I find that simply > opening the port /dev/tty00 file and using readlines(), read() or whatever > gives me empty strings or lists. Pbth. > > So first: how should I go about adding the hex escape sequences (\x0a and > \x50) to the above strings in their raw form? > second: what method should I use to read a raw stream from the serial port > for a given period? > > I have looked carefully at the neat and tidy miniterm.py example in the > pyserial examples directory, and see how one can continuously read via a > thread from the port, but have not been able to replicate it thus far. > > Any help whatsoever would be great! I'm beginning to feel a little > lightheaded. Thanks! > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: > http://mail.python.org/pipermail/tutor/attachments/20060417/27ca42b3/attachment.htm > > ------------------------------ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > End of Tutor Digest, Vol 26, Issue 56 > ************************************* > I want to build a function which will reverse my input sentance.if i try to use the rervese it does not work here is my function:
def reversor(): for i in string(a): l=[] if a==string: l.reversed(a) print l ---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor