Collections in Python - list tuple dict and string.
All the languages that we teach have "collection" variables - single names under which a series of values is stored, keyed or indexed in some way. There are four such types in Python
Lists ... starting off with index position 0, and alterable within the stucture as the program runs. You may erroneously associate such a structure with the word "array" - technically incorrect because an array is usually at successive memory loactions, whereas a list isn't necessarily so. At the cost of a little speed in simple operations, the structure of a list allows it to be extended once it's been created, and allows elements to be removed from and inserted into the middle without having to move up all the rest of the data.
Tuples ... also indexed from position 0, but NOT alterable within the stucture as the program runs. If you want to change a tuple, you have to get rebuild and replace the whole collection - quite straightforward, but inefficient. On the other hand, if you set up data in a single statement and then don't want to change it, a tuple gives you a speed advantage.
Dicts ... keyed by name / object rather than by position number. Dicts are sometimes compared to database tables with two columns (a key and value), and at other times to PHP's associative array, or to hashes in Perl. That last's probably the closest comparison and dicts are defined for very fast access to data via keys, at the expense of not being sortable.
Strings ... are a collection of characters. Like Tuples, once set up you're required to rebuild the whole of a string if you change it.
You can create any of these variables (objects) using its constructor, or use shorthands if you prefer:
[] for list
{} for dict
() for tuple
"" for string (or '' or triple quotes)
and you can then reference individual elements within any of the collections using [].
Examples of all the syntax -
[here] - my demonstration from last week's
Python training class.
(written 2013-03-04)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y108 - Python - String Handling [324] The backtick operator in Python and Perl - (2005-05-25)
[463] Splitting the difference - (2005-10-13)
[496] Python printf - (2005-11-15)
[560] The fencepost problem - (2006-01-10)
[773] Breaking bread - (2006-06-22)
[903] Pieces of Python - (2006-10-23)
[943] Matching within multiline strings, and ignoring case in regular expressions - (2006-11-25)
[954] Splitting Pythons in Bradford - (2006-11-29)
[970] String duplication - x in Perl, * in Python and Ruby - (2006-12-07)
[1110] Python - two different splits - (2007-03-15)
[1195] Regular Express Primer - (2007-05-20)
[1517] Python - formatting objects - (2008-01-24)
[1608] Underlining in Perl and Python - the x and * operator in use - (2008-04-12)
[1876] Python Regular Expressions - (2008-11-08)
[2284] Strings as collections in Python - (2009-07-12)
[2406] Pound Sign in Python Program - (2009-09-15)
[2692] Flexible search and replace in Python - (2010-03-25)
[2721] Regular Expressions in Python - (2010-04-14)
[2765] Running operating system commands from your Python program - (2010-05-14)
[2780] Formatted Printing in Python - (2010-05-25)
[2814] Python - splitting and joining strings - (2010-06-16)
[3090] Matching to a string - what if it matches in many possible ways? - (2010-12-17)
[3218] Matching a license plate or product code - Regular Expressions - (2011-03-28)
[3349] Formatting output in Python through str.format - (2011-07-07)
[3468] Python string formatting - the move from % to str.format - (2011-10-08)
[3469] Teaching dilemma - old tricks and techniques, or recent enhancements? - (2011-10-08)
[3796] Backquote, backtic, str and repr in Python - conversion object to string - (2012-07-05)
[3886] Formatting output - why we need to, and first Python example - (2012-10-09)
[4152] Why are bus fares so high? - (2013-08-18)
[4213] Formatting options in Python - (2013-11-16)
[4307] Identifying and clearing denial of service attacks on your Apache server - (2014-09-27)
[4360] Python - comparison of old and new string formatters - (2014-12-22)
[4593] Command line parameter handling in Python via the argparse module - (2015-12-08)
[4595] Python formatting update - including named completions - (2015-12-10)
[4659] Prining a pound sign from Python AND running from the command line at the same time - (2016-03-03)
Y107 - Python - Dictionaries [103] Can't resist writing about Python - (2004-10-29)
[955] Python collections - mutable and imutable - (2006-11-29)
[1144] Python dictionary for quick look ups - (2007-04-12)
[1145] Using a list of keys and a list of values to make a dictionary in Python - zip - (2007-04-13)
[2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
[2986] Python dictionaries - reaching to new uses - (2010-10-05)
[2994] Python - some common questions answered in code examples - (2010-10-10)
[3464] Passing optional and named parameters to python methods - (2011-10-04)
[3488] Python sets and frozensets - what are they? - (2011-10-20)
[3554] Learning more about our web site - and learning how to learn about yours - (2011-12-17)
[3555] Football league tables - under old and new point system. Python program. - (2011-12-18)
[3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
[3934] Multiple identical keys in a Python dict - yes, you can! - (2012-11-24)
[4029] Exception, Lambda, Generator, Slice, Dict - examples in one Python program - (2013-03-04)
[4409] Setting up and using a dict in Python - simple first example - (2015-01-30)
[4469] Sorting in Python 3 - and how it differs from Python 2 sorting - (2015-04-20)
[4661] Unique word locator - Python dict example - (2016-03-06)
[4668] Sorting a dict in Python - (2016-04-01)
Y104 - Python - Lists and Tuples [383] Overloading of operators on standard objects in Python - (2005-07-19)
[657] The ternary operator in Python - (2006-03-25)
[899] Python - extend v append on a list - (2006-10-20)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1641] Tektronix 4010 series / Python Tuples - (2008-05-13)
[1789] Looking for a value in a list - Python - (2008-09-08)
[2280] Creating and iterating through Python lists - (2009-07-12)
[2719] Traffic lights in Python - (2010-04-13)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[3181] Beware - a=a+b and a+=b are different - Python - (2011-02-23)
[3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
[3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
[3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
[3763] Spike solutions and refactoring - a Python example - (2012-06-13)
[4368] Shuffling a list - Ruby and Python - (2014-12-28)
[4722] Embedding more complex code into a named block - (2016-11-04)
Some other Articles
Showing what programming errors look like - web site pitfallWhat is on OUR pond?Really Simple Class and Inheritance example in PythonCollections in Python - list tuple dict and string.The coffee of the Stars comes to MelkshamBackups, Codebase, Strategy and more - dealing with forum incidentsSQL databases from Python - an SQLite exampleMelksham to Chippenham (Station) bus services - times and routesA request for all of Melksham transport groups to work integrated