| | |
List vs Tuples
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Lists are what they seem - a list of values. Each one of them is numbered, starting from zero - the first one is numbered zero, the second 1, the third 2, etc. You can remove values from the list, and add new values to the end. Example: Your many cats' names.
Tuples are just like lists, but you can't change their values. The values that you give it first up, are the values that you are stuck with for the rest of the program. Again, each value is numbered starting from zero, for easy reference. Example: the names of the months of the year.
There are also dictionaries (I know you did not ask about them but I am going to tell you about them anyway).
Dictionaries are similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - tare similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - they aren't in any specific order, either - the key does the same thing. You can add, remove, and modify the values in dictionaries. Example: telephone book.
So there you go hopefully that cleared things a bit for you and if you want to know more about the subject check out this website where the subject is explain more in depth.
http://docs.python.org/tut/node7.html
Python Syntax (Toggle Plain Text)
"A list" cats = ['Tom', 'Snappy', 'Kitty', 'Jessie', 'Chester'] "If you want to print for example the first name on the list you would do the following" print cats[0] "Also if you want to add a name to the list you would do this" cats.append('Catherine') "To remove a name you would do this" del cats[0]
Tuples are just like lists, but you can't change their values. The values that you give it first up, are the values that you are stuck with for the rest of the program. Again, each value is numbered starting from zero, for easy reference. Example: the names of the months of the year.
Python Syntax (Toggle Plain Text)
"Here is an example of a tuple" months = ('January','February','March','April','May','June',\ 'July','August','September','October','November',' December') "So that’s the difference between lists and tuples. A list can be modified but a tuple cannot be modified in anyway(unless changed in the source code of the program)"
There are also dictionaries (I know you did not ask about them but I am going to tell you about them anyway).
Dictionaries are similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - tare similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - they aren't in any specific order, either - the key does the same thing. You can add, remove, and modify the values in dictionaries. Example: telephone book.
Python Syntax (Toggle Plain Text)
"Here is a simple phonebook" phonebook = {'Andrew Parson':8806336, \ 'Emily Everett':6784346, 'Peter Power':7658344, \ 'Lewis Lame':1122345} "To add a name to the phonebook you would do the following" phonebook['Gingerbread Man'] = 1234567 "If you want to remove a name you would do the same as you would do in a list" del phonebook['Andrew Parson']
So there you go hopefully that cleared things a bit for you and if you want to know more about the subject check out this website where the subject is explain more in depth.
http://docs.python.org/tut/node7.html
Why is it that no human being knows the true meaning of life? For the reason that those who have found it are too caught up in their rage to understand that it ends.
In addition to Zetlin's nice explanation, tuples are used in passing arguments to and from functions ...
Then there is the tuple swap that allows you to avoid a temporary variable ...
Python Syntax (Toggle Plain Text)
def myfunction(a, b, c): d = 2 * a e = b + 5 f = c - 1 return d, e, f a = 1 b = 2 c = 3 mytuple = myfunction(a, b, c) d, e, f = myfunction(a, b, c) print a, b, c # 1 2 3 print mytuple # (2, 7, 2) print d, e, f # 2 7 2
python Syntax (Toggle Plain Text)
x = 77 y = 99 print x, y # 77 99 # do a tuple swap y, x = x, y print x, y # 99 77
May 'the Google' be with you!
nothing against zetlins first reply but he did just copy that from the tutorial from here
don't judge me because I'm a year 8!
'it is better to fight for something than to live for nothing'General George S Patton
'it is better to fight for something than to live for nothing'General George S Patton
He should have given some credit to the original source.
Reminds me of a quote from Albert Einstein:
Reminds me of a quote from Albert Einstein:
•
•
•
•
Plagiarism is stealing from one source.
Research is stealing from many sources.
Last edited by sneekula; Aug 25th, 2008 at 12:54 pm. Reason: albert
No one died when Clinton lied.
![]() |
Similar Threads
- Starting Python (Python)
- parsing xml (Python)
- Tuple List (Python)
- Looping and data merging (Python)
- iterators (Python)
- IP adresses (Python)
- Reading from a file question (Python)
- tuple or list? (Python)
Other Threads in the Python Forum
- Previous Thread: convert to xml
- Next Thread: py2exe for Linux (still to create exe files for Windows)
Views: 1777 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy corners curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o iframe images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive screensaverloopinactive scrolledtext server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython






