473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Q: "...Learning with Python" ...a property that addition and multiplication have...

Greetings.

I'm reading "How to think like a computer scientist: Learning with
Python" and there's a question regarding string operations. The
question is, "Can you think of a property that addition and
multiplication have that string concatenation and repetition do not?"

I thought it was the commutative property but "<string>"*3 is
equivalent to 3*"<string>". Any ideas?

Thanks,

Jeff

Jul 19 '05 #1
7 7615


je***********@yahoo.com wrote:
Greetings.

I'm reading "How to think like a computer scientist: Learning with
Python" and there's a question regarding string operations. The
question is, "Can you think of a property that addition and
multiplication have that string concatenation and repetition do not?"

I thought it was the commutative property but "<string>"*3 is
equivalent to 3*"<string>". Any ideas?
3.3*1 3.2999999999999998
3.3*'1'


Traceback (most recent call last):
File "<pyshell#11>", line 1, in -toplevel-
3.3*'1'
TypeError: can't multiply sequence to non-int

Thanks,

Jeff


Jul 19 '05 #2
je***********@yahoo.com wrote:
"Can you think of a property that addition and
multiplication have that string concatenation and repetition do not?"


Existence of inverses?

E.g. the additive inverse of 3 is -3, but there are
no "concatenative inverses" of strings.

(BTW, I would consider this more about thinking like a
mathematician than a computer scientist!)

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
Jul 19 '05 #3

<je***********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Greetings.

I'm reading "How to think like a computer scientist: Learning with
Python" and there's a question regarding string operations. The
question is, "Can you think of a property that addition and
multiplication have that string concatenation and repetition do not?"

I thought it was the commutative property but "<string>"*3 is
equivalent to 3*"<string>". Any ideas?


Go back to concatenation instead of repetition.

TJR

Jul 19 '05 #4
On 25 May 2005 17:23:45 -0700,
je***********@yahoo.com wrote:
I'm reading "How to think like a computer scientist: Learning with
Python" and there's a question regarding string operations. The
question is, "Can you think of a property that addition and
multiplication have that string concatenation and repetition do not?" I thought it was the commutative property but "<string>"*3 is
equivalent to 3*"<string>". Any ideas?


Thinking like an old embedded systems programmer, for many types of
numbers (ints and floats, but not longs), multiplication and addition
are constant time, constant space operations. String concatenation and
repetition are not.

As already mentioned, there are no inverses. Similarly, there aren't
even inverse operations (e.g., it makes little sense to divide a string
by a number, let alone a number by a string).

For floating point numbers (and integers that can overflow),
multiplication and addition are not necessarily exact. Unless your
strings get too big for the memory manager, concatenation and
replication are exact.

And now that I reread the question, "a property that addition and
multiplication have" is "the distributive property," but the
distributive property of multiplcation over addition does not translate
to a distributive property of repetition over concatenation:

2 * ( 3 + 4 ) == 14
2 * 3 + 2 * 4 == 14

but

2 * ( "ABC" + "DEFG" ) == "ABCDEFGABCDEFG"
2 * "ABC" + 2 * "DEFG" == "ABCABCDEFGDEFG"

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Jul 19 '05 #5
The inablility to work with negative values.

Addition can do the following:
5 + (-4) read as 5 plus the value negative four.

Multiplication can do the following:
5 * (-1) read as 5 times the value negative one.

String concatination can not subtract the sub-string 'lo' from 'hello'.
'hello' - 'lo' is invalid.

string repetition can not repeat a value negative times:
'hello' * -3 is invalid.
'hello' * 2.75 is also invalid, in that you can not repeat a fractional
amount.

-Dave
(Python Newbie)
<je***********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Greetings.

I'm reading "How to think like a computer scientist: Learning with
Python" and there's a question regarding string operations. The
question is, "Can you think of a property that addition and
multiplication have that string concatenation and repetition do not?"

I thought it was the commutative property but "<string>"*3 is
equivalent to 3*"<string>". Any ideas?

Thanks,

Jeff

--
http://mail.python.org/mailman/listinfo/python-list


Jul 19 '05 #6
je***********@yahoo.com writes:
question is, "Can you think of a property that addition and
multiplication have that string concatenation and repetition do not?"

I thought it was the commutative property but "<string>"*3 is
equivalent to 3*"<string>". Any ideas?


Um, string concatenation is not commutative. "a"+"b" is not the same
as "b"+"a".
Jul 19 '05 #7
GMane Python wrote:
string repetition can not repeat a value negative times:
'hello' * -3 is invalid.


Well, it's not invalid:

py> 'a' * -1
''
py> 'a' * -42
''

but you could definitely say that
S * N1 == S * N2
does not imply that
N1 == N2

STeVe
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: j_mckitrick | last post by:
I used to like Objective-C and Gnustep, but I gave up for lack of support and recognition of the language/platform under Unix. But it did offer NSNotifications, a basic message class that could be...
4
by: Tim Henderson | last post by:
Hi The question why are there no sorted dictionaries in python, seems to pop up with unseeming regularity. That question in itself in nonsensical sense dictionaries are hash-maps, however should...
4
by: AbrahamLincolnIllinois | last post by:
Hello all. On page 479, the 2nd edition of the "Learning Python" book, this code appears class Derived(Base): def __init__(self, arg, *args, **kw): self.__init__(self, *args, **kw) Surely...
8
by: Mike Meng | last post by:
Hi all, I just finished reading Learning Python 3rd ed, and am doing my first Python application, which retrieves and process text and XML documents from Web. Python helped me to write the...
5
by: AlbaClause | last post by:
I'm just learning Python, and I have a question about os.path.join(dirpath, name) and its use. Simply put, I haven't figured out how to use it. I was looking through the Python reference...
14
by: Paddy3118 | last post by:
This month there was/is a 1000+ long thread called: "merits of Lisp vs Python" In comp.lang.lisp. If you followed even parts of the thread, AND previously used only one of the languages AND...
92
by: ureuffyrtu955 | last post by:
Python is a good programming language, but "Python" is not a good name. First, python also means snake, Monty Python. If we search "python" in google, emule, many results are not programming...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.