Program Arcade GamesWith Python And Pygame

String (a string is short for “string of characters,” which normal people think of as text.)

Integer

Floating point

Boolean

Python can display what type of data a value is with the type function.

This type function isn't useful for other programming
in this book,
but it is good to demonstrate the types
of data introduced so far. Type the following into
the interactive IDLE shell.
(Don't create a new window and type this in as a program; it won't work.)

It is also possible to use the type function on a variable to
see what kind of data is in it.

x = 3
type(x)

More than one coin to collect? Use a list!

The two new types of data introduced in this chapter are Lists and Tuples.
Lists are similar to another data structure called an array. A list can
be resized, but an array can not. A course in data structures will
teach you the details, but it that is beyond the scope of this book.
Try running the following commands in the interactive Python shell and
see what is displayed:

You've created grocery lists, to-do lists, bucket lists, but how do you create
a list on the computer?
Figure 7.1: Even computers use lists

Try these examples using IDLE's command line. To create a
list and print it out, try the following:

>>> x = [1,2]
>>> print(x)
[1, 2]

To print an individual element in a list:

>>> print(x[0])
1

This number with the item's location is called the index.
Note that list locations start at zero. So a list or array with 10 elements
does not have an element in spot [10]. Just spots [0] through [9].
It can be very confusing to create an list of 10 items and then not
have an item 10, but most computer languages start counting at 0 rather than
1.

Think
of a list as an ice cube tray that holds numbers, as shown in
Figure 7.2. The values are
stored inside each tray spot, and written on the side of the tray are numbers
starting at zero that identify the location of each spot.

Don't mix the index and the value!

Remember, there are two sets of numbers to consider when working with a list
of numbers: the position and the value.
The position, also known as index, refers
to where a value is. The value is the actual number stored at that
location. When working with a list or array, make sure to think if you need the
location or the value.

It is easy to get the value given
the location, but it is harder to get the location given the value.
Chapter 15 is dedicated to answering how to find the location
of a particular value.
Figure 7.2: Lists are like ice cube trays

A program can assign new values to an individual element in a list.
In the case below, the first spot at location zero (not one) is assigned
the number 22.

>>> x[0] = 22
>>> print(x)
[22, 2]

Also, a program can create a “tuple.” This data type works
just like a list, but with two differences. First, it
is created with parentheses rather than square brackets.
Second, it is not possible to change the tuple once created.
See below:

As can be seen from the output of the code above,
we can't assign an item in the tuple a new value. Why would we want this
limitation? First, the computer can run faster if it knows the value won't change.
Second, some lists we don't want to change, such as a list of RGB colors for red.
The color red doesn't change, therefore an immutable tuple is a better choice.

If a program needs to iterate through each item in a list,
such as to print it out, there are two types of for loops that can
do this.

The first method to iterate through each item in
a loop is by using a “for-each” loop. This type of loop
takes a collection of items, and loops the code once per item.
It will take a copy of the item and store it in a
variable for processing.

The format of the command: for item_variable in list_name:

Here are some examples:

my_list = [101, 20, 10, 50, 60]
for item in my_list:
print(item)

Output:

101
20
10
50
60

Programs can store strings in lists too:

my_list = ["Spoon", "Fork", "Knife"]
for item in my_list:
print(item)

Output:

Spoon
Knife
Fork

Lists can even contain other lists.
This iterates through each item in the main list, but not
in sublists.

my_list = [ [2,3], [4,3], [6,7] ]
for item in my_list:
print(item)

Output:

[2,3]
[4,3]
[6,7]

The other way to iterate through a list is to use
an index variable and directly access the list rather than through a copy of each item.
To use an index variable, the program counts from 0 up to the length
of the list. If there are ten elements, the loop must go from 0 to 9 for a
total of ten elements.

The length of a list may be found by using the len function.
Combining that with the range function allows the program to loop through
the entire list.

This method is more complex, but is also more powerful. Because we are
working directly with the list elements, rather than a copy, the list can
be modified. The for-each loop does not allow modification of the original
list.

Side note: If performance while appending is a concern,
it is very important to understand how a list is being implemented.
For example, if a list is implemented as an array data type,
then appending an item to the list is a lot like adding a new egg
to a full egg carton. A new egg carton must be built with thirteen
spots. Then twelve eggs are moved over. Then the thirteenth egg is added.
Finally the old egg carton is recycled.
Because this can happen behind the scenes in a function,
programmers may forget
this and let the computer do all the work. It would be more efficient
to simply tell
the computer to make an egg carton with enough spots to begin with.
Thankfully, Python does not implement a list as an array data type.
But it is important to pay attention to your next semester data structures
class and learn how all of this works.

To create a list from scratch, it is necessary to create a blank list and then
use the append function. This example creates a list based upon user input:

Creating a running total of an array is a common
operation. Here's how it is done:

# Copy of the array to sum
my_list = [5,76,8,5,3,3,56,5,23]
# Initial sum should be zero
list_total = 0
# Loop from 0 up to the number of elements
# in the array:
for i in range(len(my_list)):
# Add element 0, next 1, then 2, etc.
list_total += my_list[i]
# Print the result
print(list_total)

The same thing can be done by using a for loop to iterate the
array, rather than count through a range:

# Copy of the array to modify
my_list = [5, 76, 8, 5, 3, 3, 56, 5, 23]
# Loop from 0 up to the number of elements
# in the array:
for i in range(len(my_list)):
# Modify the element by doubling it
my_list[i] = my_list[i] * 2
# Print the result
print(my_list)

However version 2 does not work at doubling the values in
an array. Why? Because item is a copy of an element
in the array. The code below doubles the copy, not the original array element.

# Copy of the array to modify
my_list = [5, 76, 8, 5, 3, 3, 56, 5, 23]
# Loop through each element in myArray
for item in my_list:
# This doubles item, but does not change the array
# because item is a copy of a single element.
item = item * 2
# Print the result
print(my_list)

Computers do not actually store letters of a string in memory; computers store
a series of numbers. Each number represents a letter. The system that computers
use to translate numbers to letters is called Unicode.
The full name for the encoding is Universal Character Set Transformation Format
8-bit, usually abbreviated UTF-8.

The Unicode chart covers the Western alphabet using the numbers 0-127. Each
Western letter is represented by one byte of memory. Other alphabets, like
Cyrillic, can take multiple bytes to represent each letter.
A partial copy of the Unicode chart is below:

Finally, the last code takes each UTF-8 value and subtracts one from it,
then converts the value back to
a letter. By feeding this program the output of the previous program, it
serves as a decoder for text encoded by the prior example.