Game Development Reference
In-Depth Information
>>>
The most common list method you will use is append() . This method will add the
value you pass as an argument to the end of the list. Try typing the following into the shell:
>>> eggs = []
>>> eggs.append('hovercraft')
>>> eggs
['hovercraft']
>>> eggs.append('eels')
>>> eggs
['hovercraft', 'eels']
>>> eggs.append(42)
>>> eggs
['hovercraft', 'eels', 42]
>>>
Though string and list data types have methods, integers do not happen to have any
methods.
The Difference Between Methods and Functions
You may be wondering why Python has methods, since they seem to act just like
functions. Some data types have methods. Methods are functions associated with values of
that data type. For example, string methods are functions that can be called on any string. If
you have the string value 'Hello' , you could call the string method upper() like this:
'Hello'.upper() . Or if the string 'Hello' were stored in a variable named spam , it
would look like this: spam.upper()
You cannot call string methods on values of other data types. For example, [1, 2,
'apple'].upper() would cause an error because [1, 2, 'apple'] is a list and
upper() is a string method.
The values of data types that have methods are also called objects. Object-oriented
programming is a bit advanced for this topic, and you don't need to completely understand
it to make games. Just understand that objects are another name for a values of data types
that have methods.
The split() List Method
Line 59 is a very long line of code, but it is really just a simple assignment statement.
This line also uses the split() method, which is a method for the string data type (just
like the lower() and upper() methods).
 
Search WWH ::




Custom Search