FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

Python: Adding sequences




Sequences in Python can be added. Since strings are some sort of sequence this statement also holds true for strings then.

We know:

Code:
>>> 1 + 1
2
>>>


As long as 1 is not a sequence. Since:

Code:
>>> [1] + [1]
[1, 1]
>>>


Or a string:

Code:
>>> "1" + "1"
'11'
>>>




Confused ? Sorry about this. Here are two more obvious and less weird examples demonstrating how sequences can be added:

Code:
>>> [1,2,3] + [4,5,6]
[1, 2, 3, 4, 5, 6]
>>> "Hello" + " World!"
'Hello World!
>>>


Of course you can not concatenate sequences of different types:

Code:
>>> "Hello" + [1,2,3]

Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    "Hello" + [1,2,3]
TypeError: cannot concatenate 'str' and 'list' objects
>>>




0 blog comments below




FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.