Graphics Reference
In-Depth Information
Lists Lists ( list ) are ordered collections of entities. The entities in lists can be of any other data type, and
individualscanbeaccessedbyanintegerindex.Theycanbeaddedtoandalteredeasily,makingthemappro-
priate when a dynamic ordered collection is needed.
Dictionaries Dictionaries ( dict ) are unordered collections of entities, in which pairs of entities have a key-
value relationship. Values can be accessed via their corresponding keys.
Tuples Tuples ( tup ) are similar to lists but are limited in how they can be altered. They can be processed
faster than lists, which makes them appropriate when an unchanging ordered collection is needed.
Lists,dictionaries,andtuplesareparticularlyimportantdatatypesbecausetheycontainotherobjectsanden-
able access to them. Procedural control in Python almost always depends on manipulating these data structures.
The following examples are best viewed in Blender's Python Console.
Integers and Floating-Point Numbers
Integers and floating-point numbers (floats)arerepresentedinPythonbysequencesofdigits.Floatsalsocon-
tain a decimal point. Python can usually handle the interactions between integers and floating-point numbers in
a natural and invisible way. For example, if you try to multiply an integer with a float, the answer will be the
float that you intuitively expect.
Strings
Sequences of characters are appropriately handled as strings in Python. Of course, in a computer program
everything is a sequence of characters, so it is necessary to identify strings by using single quotes or double
quotes. Single or double quotes may be used interchangeably for strings in Python. If you have a string that
contains single quotes, you can avoid the need to escape those quotes by using double quotes as your string
delimiter. Sequences of characters that are not delimited by quotes are not treated as strings in Python. If the
sequence is not a special function word in Python, the Python interpreter will try to treat the sequence as a vari-
able. If this is not what you intended, chances are this will lead to an error.
There are numerous methods that can be called on strings. You can return the length of a string or individual
characters by index similarly to the way you would a list, which you will see in the next section.
Lists and Tuples
Lists are represented in Python as ordered collections of entities within square brackets, where each entity is
separated by a comma. An example of a list is shown here:
['a', 'b', 'c']
This list hasthree elements, the strings 'a' , 'b' ,and 'c' .The len (list) function returns the length of
the list, as shown here:
>>> len(['a','b','c'])
3
Elements of lists can be accessed by their individual indexes. List indexes start with 0 in Python, so the first
element in the list is at index 0, and the last element of a list of length n is at index n -1. The notation for this
is the index integer placed in square brackets immediately after the list. This notation is most commonly used
when the list is stored in a variable, as you will see later in this chapter, but it can also be used with the list in
plain notation, as shown here:
Search WWH ::




Custom Search