Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 14 Online
OpenStudy (anonymous):

Python Definitions can some one tell me the difference between a: list, tuples and strings.

OpenStudy (anonymous):

a string is basically a list of characters in a specific order ("abc def g"). Characters being anything really that you can type on a keyboard (and contained between quotes ' or ") touples and lists can contain a list of things like ints or strings A list is basically an array of elements, you can do some cool things to lists like the ".append(newElement)" function which will add that element to the end of the list. i.e.: myList = ["hello", "world"] myList.append("new string") now your myList variable will be ["hello", "world", "new string"]

OpenStudy (anonymous):

There is a more in depth way to explain it but it would be over explaining really for someone who just starts out. So that is basically the core of it.

OpenStudy (anonymous):

Here is a good page to explain the touple http://docs.python.org/release/1.5.1p1/tut/tuples.html

OpenStudy (anonymous):

I think the easiest way to think about tuples is that they are like points in the cartesian plane (x-coordinate, y-coordinate), in the sense that each coordinate is related to each other in the way that it defines uniquely a point. In this sense, a tuple is immutable, meaning you can assign different values to a tuple already created - you have to create a new one. This is quite an oversimplification, but it gives you a visual about what you could use tuples for and what not. In a sense, almost everywhere that you don't want to change values(albeit it might or might not be optimally), you can use tuples instead of lists.

OpenStudy (anonymous):

meaning you can't* assign : typo there.

OpenStudy (anonymous):

So lists and tuples are made up of strings. Tuples cant be changed and lists can be modified. Right?

OpenStudy (anonymous):

yes, but you can pass more than strings into them

OpenStudy (anonymous):

Ok i can my head around that, thanks.

OpenStudy (anonymous):

You can modify touples though in the sense that you can append to them. if i were to say toupleVar[0] = 'x' then it would return an error however if i were to say listVar[0] = 'x' then the first element of the list will be changed to 'x'

OpenStudy (anonymous):

toupleName = ("First", "Second") toupleName += ("Third",) now your toupleName is = to ("First", "Second", "Third")

OpenStudy (anonymous):

So toupleName[0] = "New First" would error out and say "'tuple' object does not support item assignment"

OpenStudy (anonymous):

on the flip: listName = ["First", "Second"] listName.append("Third") now you listName would = ["First", "Second", "Third"] and if you were to say: listName[0] = "New First" then your listName would = ["New First", "Second", "Third"]

OpenStudy (anonymous):

so you can add to tuples but can you subtract from tuples

OpenStudy (anonymous):

It's an undefined operand; in short, as it is built-in, no, you can't.

OpenStudy (anonymous):

you can subtract items from a touple by reassigning the touple and slicing it i.e.: toupleName = (0, 1, 2, 3, 4, 5) toupleName = toupleName[:4] now toupleName equals (0, 1, 2, 3, 4)

OpenStudy (anonymous):

the [:4] basically is saying get every element of the touple from 0 to 4 but do not include 4

OpenStudy (anonymous):

So over all you can change both toples and lists but its a lot easier to change list and you can do a lot more with lists.

OpenStudy (anonymous):

In my opinion yes. I use lists way more often

OpenStudy (anonymous):

This link has a list of different methods you can perform on lists to do things like removing elements and so forth http://docs.python.org/tutorial/datastructures.html

OpenStudy (anonymous):

But don't short-shoot touples. If you want to make a list of elements that you don't want to accidentally change (or someone else to accidently change) you may want to use a touple

OpenStudy (anonymous):

As a side note, I believe that when you add an element to a tuple, using syntax described above by brent, you are returning the reference to a new tuple. Ultimately, tuples are immutable, even if they look changeable, albeit it doesn't really matter. But I am quite sleepy / lazy to go look for the PyObject code for tuples :-)

OpenStudy (anonymous):

Lol, you may be right, I have been programming in python for about 2 weeks now. I'm a C++/C# guy >.>. Nevertheless I personally have never used the re-assigning touple code that I said. I have only tried it to see if I could get away with it.

OpenStudy (anonymous):

Actually, I think that the PyObject code is all written in C. Quite interesting stuff, if you have the time / interest. You can fiddle in quite a few stuff there :-), but I am not 100% sure if I am correct.

OpenStudy (anonymous):

Oh? I did hear that it was written in C++ but never looked into it, I just might :]

OpenStudy (anonymous):

I believe it's in C : http://programmers.stackexchange.com/questions/20988/why-is-python-written-in-c-and-not-in-c Python is good because it has compatibility to C code (you can write costly operations more efficiently in C, and port it to Python), albeit I still have to use this feature. Anyway, I will stop posting here, as to not flood too much. If you need anything mate, hit me up in the chat. :-)

OpenStudy (anonymous):

When you do myTuple = (3, 4, 5) myTuple += (6, ) you are creating a new tuple, not appending to an existing one. It's the same as this myTuple = (3, 4, 5) myTuple = myTuple + (6, ) The += operator works just like with immutable integers. x = 2 x += 1 Same as x = x + 1 You haven't changed "2" by doing "x += 1". You've changed where x is pointing. Just like "myTuple" changes where it's pointing.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!