This is in the " Snake Wrangling For Kids" Learning to Program with Python by Jason R Briggs
Thought this would help.
Michael

What’s the difference between 10 and ‘10’?
Not much apart from the quotes, you might be thinking. Well, from reading the earlier chapters, you know that the first is a number and the second is a string. This makes them differ more than you might expect. Earlier we compared the value
of a variable (age) to a number in an if-statement:
>>> if age == 10:
... print ’you are 10’
If you set variable age to 10, the print statement will be called:
>>> age = 10
>>> if age == 10:
... print ’you are 10’
...
you are 10
However, if age is set to ’10’ (note the quotes), then it won’t:
>>> age = ’10’
>>> if age == 10:
... print ’you are 10’
...
Why is the code in the block not run? Because a string is different from a
number, even if they look the same:
>>> age1 = 10
>>> age2 = ’10’
>>> print age1
10
>>> print age2
10
See! They look exactly the same. Yet, because one is a string, and the other is a number, they are different values. Therefore age == 10 (age equals 10) will never
be true, if the value of the variable is a string.
Probably the best way to think about it, is to consider 10 books and 10 bricks. The number of items might be the same, but you couldn’t say that 10 books are exactly the same as 10 bricks, could you? Luckily in Python we have magic functions which can turn strings into numbers and numbers into strings (even if they won’t quite turn bricks into books). For example, to convert the string ’10’ into a number
you would use the function int:
4.5. WHAT’S THE DIFFERENCE. . .? 43
>>> age = ’10’
>>> converted_age = int(age)
The variable converted age now holds the number 10, and not a string. To convert
a number into a string, you would use the function str:
>>> age = 10
>>> converted_age = str(age)
converted age now holds the string 10, and not a number. Back to that if-statement
which prints nothing:
>>> age = ’10’
>>> if age == 10:
... print ’you are 10’
...
If we convert the variable before we check, then we’ll get a different result:
>>> age = ’10’
>>> converted_age = int(age)
>>> if converted_age == 10:
... print ’you are 10’
...
you are 10






On Jul 3, 2008, at 12:29 PM, Dong Li wrote:


Date: Thu, 3 Jul 2008 10:18:23 +0100
From: "Alan Gauld" <[EMAIL PROTECTED]>
Subject: Re: [Tutor] Question about string
To: tutor@python.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
        reply-type=original


"Dong Li" <[EMAIL PROTECTED]> wrote

I am new to python, so what I ask may be so basic. I don't know the
difference between

s = 'a' 'b'
and
s = 'a'+'b'

They have the same results. Thanks for relying!

I think the differencec is that the first is purely a syntax thing so
the interpreter does the work of joining the strings together before
processing the result as a single string whereas the second the
two strings are treated separately and actual string addition
(concatenation) is done which is a much more expensive
operation in terms of computer power.

The first is only possible if you have literal strings but the second
can be used for variables:

s1 = 'a'
s2 = 'b'
s = s1 s2     # doesn't work
s = s1 + s2   # works

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


------------------------------

Date: Thu, 3 Jul 2008 09:53:07 +0000
From: "Monika Jisswel" <[EMAIL PROTECTED]>
Subject: Re: [Tutor] Question about string
To: tutor@python.org
Message-ID:
        <[EMAIL PROTECTED]>
Content-Type: text/plain; charset="iso-8859-1"

Python is one of the smartest languages, it does many things for the
programmer  (I don't know but this might be what they mean with
Batteries-Included) , & you have just scratched the surface of it, here python concatenated your strings together for you, later you will meet list comprehention & other stuff that actually does most of the programing logic
for you for free.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080703/9c54d12a/attachment-0001.htm >

------------------------------

Thank you for excellent explanations! I have been attracted by python
more and more!

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to