[Tutor] Tutor Digest, Vol 96, Issue 26

Michael Lewis mjolewis at gmail.com
Tue Feb 7 20:45:00 CET 2012


On Tue, Feb 7, 2012 at 10:57 AM, <tutor-request at python.org> wrote:

> Send Tutor mailing list submissions to
>        tutor at 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
>        tutor-request at python.org
>
> You can reach the person managing the list at
>        tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>   1. Re: Backspace Escape Sequence; \b (Peter Otten)
>   2. Re: Backspace Escape Sequence; \b (Steven D'Aprano)
>   3. Re: Question on how to do exponents (Alan Gauld)
>   4. Re: Question on how to do exponents (Steven D'Aprano)
>   5. Re: Backspace Escape Sequence; \b (Alan Gauld)
>   6. (no subject) (Debashish Saha)
>   7. confusion with else command (Debashish Saha)
>   8. Re: Question on how to do exponents (Sarma Tangirala)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Tue, 07 Feb 2012 18:41:12 +0100
> From: Peter Otten <__peter__ at web.de>
> To: tutor at python.org
> Subject: Re: [Tutor] Backspace Escape Sequence; \b
> Message-ID: <jgrnmj$g41$1 at dough.gmane.org>
> Content-Type: text/plain; charset="ISO-8859-1"
>
> Garland W. Binns wrote:
>
> > Could someone please tell me a common or semi-frequent scenario in which
> a
> > person would use a backspace escape sequence?
>
> Does
>
> $ python -c 'print "this is b\bbo\bol\bld\bd"' | less
>
> qualify? If you are using (e. g.) Linux this technique is used to show some
> text in boldface when you type something like
>
> >>> help(str)
>
> in Python's interactive interpreter.
>
>
>
>
> ------------------------------
>
> Message: 2
> Date: Wed, 08 Feb 2012 05:18:35 +1100
> From: Steven D'Aprano <steve at pearwood.info>
> To: tutor at python.org
> Subject: Re: [Tutor] Backspace Escape Sequence; \b
> Message-ID: <4F316AFB.7040803 at pearwood.info>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Peter Otten wrote:
> > Garland W. Binns wrote:
> >
> >> Could someone please tell me a common or semi-frequent scenario in
> which a
> >> person would use a backspace escape sequence?
> >
> > Does
> >
> > $ python -c 'print "this is b\bbo\bol\bld\bd"' | less
> >
> > qualify? If you are using (e. g.) Linux this technique is used to show
> some
> > text in boldface
>
> Gah, that has got to be the ugliest hack in the universe.
>
> I don't think that is guaranteed to work everywhere. less supports the \b
> trick, but the Python interactive interpreter doesn't support it directly.
>
>
>
> --
> Steven
>
>
> ------------------------------
>
> Message: 3
> Date: Tue, 07 Feb 2012 18:29:49 +0000
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Question on how to do exponents
> Message-ID: <jgrqit$80s$1 at dough.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 07/02/12 16:54, Sarma Tangirala wrote:
>
> > Is is better to use pow() against **?
>
> I suspect ** will be faster since it doesn't have the function
> call overhead.
>
> But I haven't tried timing it. Feel free to do some tests and find out.
> Let us know how you get on!
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
> ------------------------------
>
> Message: 4
> Date: Wed, 08 Feb 2012 05:31:07 +1100
> From: Steven D'Aprano <steve at pearwood.info>
> To: tutor at python.org
> Subject: Re: [Tutor] Question on how to do exponents
> Message-ID: <4F316DEB.3070001 at pearwood.info>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Sarma Tangirala wrote:
>
> > Is is better to use pow() against **?
>
>
> Advantages of **
>
> - it is shorter to type x**y vs pow(x, y)
> - being an operator, it is slightly faster than calling a function
> - you can't monkey-patch it
>
> Disadvantages of **
>
> - being an operator, you can't directly use it as a function-object
> - it can't take three arguments
> - hard to google for "**"
> - you can't monkey-patch it
>
> Advantages of pow()
>
> - it is a function, so you can pass it around as an object
> - three argument form
> - easy to call help(pow) to see documentation
> - easy to google for "pow"
> - can be monkey-patched
>
> Disadvantages of pow()
>
> - a tiny bit slower due to the function call
> - slightly longer to type
> - can be monkey-patched
>
>
> Weigh up the advantages and disadvantages of each, and make the call which
> is
> better for you.
>
> (My preference is to use the ** operator.)
>
>
>
> --
> Steven
>
>
> ------------------------------
>
> Message: 5
> Date: Tue, 07 Feb 2012 18:42:09 +0000
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Backspace Escape Sequence; \b
> Message-ID: <jgrra2$ec8$1 at dough.gmane.org>
> Content-Type: text/plain; charset=UTF-8; format=flowed
>
> On 07/02/12 17:19, Garland W. Binns wrote:
> > Could someone please tell me a common or semi-frequent scenario in which
> > a person would use a backspace escape sequence?
>
> Do you mean backspace specifically or do you really mean backslash?
>
> In other words are you looking for a use of literal backspaces in a
> string or are you looking at the use of escaped characters in general?
>
> backspace specifically is not commonly used but is sometimes handy for
> positioning the cursor in the middle of a line.
> eg
>
> raw_input("Area:    acres\b\b\b\b\b\b\b\b")
>
> Should output the prompt line with the cursor positioned in the gap
> between Area: and acres.
>
>
>  >>> raw_input("Area:    acres\b\b\b\b\b\b\b\b")
> Area: 45 acres
> '45'
>  >>>
>
> But in the general sense there are lots of scenarios where we use
> escaped characters.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
> ------------------------------
>
> Message: 6
> Date: Wed, 8 Feb 2012 00:20:29 +0530
> From: Debashish Saha <silideba at gmail.com>
> To: tutor at python.org
> Subject: [Tutor] (no subject)
> Message-ID:
>        <CA+b=61Bv2EB1+fVZgucKAdwZGk4cozxaWS19W0j8p1JdEcGiDw at mail.gmail.com
> >
> Content-Type: text/plain; charset=ISO-8859-1
>
> for i in range(1, 8):
>    print(i)
>    if i==3:
>        break
> else:
>    print('The for loop is over')
>
>
>  Output:
> 1
> 2
> 3
>
> Question:but after breaking the for loop why the else command could not
> work?
>
>
> ------------------------------
>
> Message: 7
> Date: Wed, 8 Feb 2012 00:22:32 +0530
> From: Debashish Saha <silideba at gmail.com>
> To: tutor at python.org
> Subject: [Tutor] confusion with else command
> Message-ID:
>        <CA+b=61CK+RHOWMPi98hnSNrhaVdyG=-+gaEjc5qP+=PjATAvVQ at mail.gmail.com
> >
> Content-Type: text/plain; charset=ISO-8859-1
>
> for i in range(1, 8):
>    print(i)
>    if i==3:
>        break
> else:
>    print('The for loop is over')
>
>
> Output:
> 1
> 2
> 3
>
> Question:
>
> but after breaking the for loop why the else loop could not work?
>
>
> ------------------------------
>
> Message: 8
> Date: Wed, 8 Feb 2012 00:27:02 +0530
> From: Sarma Tangirala <tvssarma.omega9 at gmail.com>
> To: "Steven D'Aprano" <steve at pearwood.info>
> Cc: tutor at python.org
> Subject: Re: [Tutor] Question on how to do exponents
> Message-ID:
>        <CABFCkKTJL6hk7f3j2cLGmHEN5-i8Ox5PrSa1c+TZQJw1pwZ6Sg at mail.gmail.com
> >
> Content-Type: text/plain; charset="iso-8859-1"
>
> On 8 February 2012 00:01, Steven D'Aprano <steve at pearwood.info> wrote:
>
> > Sarma Tangirala wrote:
> >
> >  Is is better to use pow() against **?
> >>
> >
> >
> > Advantages of **
> >
> > - it is shorter to type x**y vs pow(x, y)
> > - being an operator, it is slightly faster than calling a function
> > - you can't monkey-patch it
> >
> > Disadvantages of **
> >
> > - being an operator, you can't directly use it as a function-object
> > - it can't take three arguments
> > - hard to google for "**"
> > - you can't monkey-patch it
> >
> > Advantages of pow()
> >
> > - it is a function, so you can pass it around as an object
> > - three argument form
> > - easy to call help(pow) to see documentation
> > - easy to google for "pow"
> > - can be monkey-patched
> >
> > Disadvantages of pow()
> >
> > - a tiny bit slower due to the function call
> > - slightly longer to type
> > - can be monkey-patched
> >
> >
> > Weigh up the advantages and disadvantages of each, and make the call
> which
> > is better for you.
> >
> > (My preference is to use the ** operator.)
> >
> >
> A simple "function call" argument would have done! :D Thanks for the
> survey!
>
> Anyway, I was wondering about this, if internally pow() actually uses **.
> :P
>
>
> >
> >
> > --
> > Steven
> >
> > ______________________________**_________________
> > Tutor maillist  -  Tutor at python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/**mailman/listinfo/tutor<
> http://mail.python.org/mailman/listinfo/tutor>
> >
>
>
>
> --
> Sarma Tangirala,
> Class of 2012,
> Department of Information Science and Technology,
> College of Engineering Guindy - Anna University
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/tutor/attachments/20120208/1c12a5c9/attachment.html
> >
>
>

Message: 7
Date: Wed, 8 Feb 2012 00:22:32 +0530
From: Debashish Saha <silideba at gmail.com>
To: tutor at python.org
Subject: [Tutor] confusion with else command
Message-ID:
       <CA+b=61CK+RHOWMPi98hnSNrhaVdyG=-+gaEjc5qP+=PjATAvVQ at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

for i in range(1, 8):
   print(i)
   if i==3:
       break
else:
   print('The for loop is over')


Output:
1
2
3

Question:

but after breaking the for loop why the else loop could not work?

The else statement is only executed if the if statement is never true. Each
time i iterates in your for loop, the if statement is checked, if the if
statement is not true, your for loop continues until it either reaches the
end of the range specified in the for loop or until the loop breaks. In
your case, the loop breaks once i is set to 3. If you DO want to print the
string 'the for loop is over', you should do the following.

for i in range(1, 8):
   print(i)
print('The for loop is over')
You'll notice that you don't need the if/else statement.
Your output here will print i for each iteration until the end of the range
and then break out of the for loop and print 'The for loop is over'.




> ------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 96, Issue 26
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120207/79be8e04/attachment-0001.html>


More information about the Tutor mailing list