473,748 Members | 7,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: "...Learnin g 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 7704


je***********@y ahoo.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.2999999999999 998
3.3*'1'


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

Thanks,

Jeff


Jul 19 '05 #2
je***********@y ahoo.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 "concatenat ive 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.goo glegroups.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***********@y ahoo.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.tombstoneze ro.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.goo glegroups.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***********@y ahoo.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
1256
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 posted and received by all subscribers. Other than global variables used to share data across threads, does Python have anything similar? jonathon
4
2429
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 python have a sorted map type object is a good question. clearly many people like have a sorted map, and sorting the keys every time seems rather wasteful, as does keeping a separate sorted list of the keys.
4
1516
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 self.__init__ should be
8
3350
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 application in a few hours, I'm very happy with its productivity. But the performance is not satisfactory. I decide to optimized it in Python before trying C/C++ extensions. But I don't know Python much and have no clu to tune my program. Also, I...
5
1686
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 material in the wee hours of the morning and checking out some of the modules. I was keenly interested in the os module, as it is necessary for me to learn this stuff in order to begin my first real Python project. I was looking at os.walk(top) when...
14
2181
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 (and this is the crucial bit), were persuaded to have a more positive view of the other language; (deep breath, this is a long, as well as grammatically incorrect sentence), THEN WHY NOT POST ON WHAT ARGUMENTS PERSUADED YOU.
92
3959
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 resource. If we search PHP, all results are programming resource. Second, python also means snake, snake is not a good thing in western culture. Many people dislike any things relevant to snake. We must have high regard for the custom.
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.