﻿ Program Arcade Games With Python And Pygame

# Program Arcade GamesWith Python And Pygame

 < Previous Home Next >

# Chapter 7: 列表入门

## 7.1 数据类型

• 字符串(String)
• 整数(Integer)
• 浮点(Floating point)
• 布尔(Boolean)

Python可以同股使用type函数来显示一个值的类型。

 type(3) type(3.145) type("Hi there") type(True)  Output: >>> type(3) >>> type(3.145) >>> type("Hi there") >>> type(True) 

x = 3
type(x)


type(  (2, 3, 4, 5) )
type(  [2, 3, 4, 5] )


## 7.2 使用列表

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


>>> print(x[0])
1


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


>>> x = (1, 2)
>>> print(x)
(1, 2)
>>> print(x[0])
1
>>> x[0] = 22
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
x[0] = 22
TypeError: 'tuple' object does not support item assignment
>>>


## 7.3 列表的迭代

for 元素变量 in 列表名:

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

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

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

 my_list = [101, 20, 10, 50, 60] for i in range(len(my_list)): print(my_list[i])  Output: 101 20 10 50 60 

## 7.4 向列表里添加

 my_list = [2, 4, 5, 6] print(my_list) my_list.append(9) print(my_list)  Output: [2, 4, 5, 6] [2, 4, 5, 6, 9] 

 Output: Enter an integer: 4 [4] Enter an integer: 5 [4, 5] Enter an integer: 3 [4, 5, 3] Enter an integer: 1 [4, 5, 3, 1] Enter an integer: 8 [4, 5, 3, 1, 8] 

# 创建一个100个0构成的数组
my_list = [0] * 100


## 7.5 读一个列表求和或者修改

# 复制数组到列表
my_list = [5,76,8,5,3,3,56,5,23]

# 起始的和设为0
list_total = 0

# 从0循环至列表的长度
for i in range(len(my_list)):
加上元素0，元素1，元素2...
list_total += my_list[i]

# 打印结果
print(list_total)


# 复制数组到列表
my_list = [5, 76, 8, 5, 3, 3, 56, 5, 23]

# 起始的和设为0
list_total = 0

# 循环数组, 复制每个元素到一个变量元素
for item in my_list:
# 加上每个变量
list_total += item

# 打印结果
print(list_total)


# 复制数组到列表
my_list = [5, 76, 8, 5, 3, 3, 56, 5, 23]

# 从0循环至列表的长度
for i in range(len(my_list)):
# 对每个元素乘以2
my_list[i] = my_list[i] * 2

# 打印结果
print(my_list)


# 复制数组到列表
my_list = [5, 76, 8, 5, 3, 3, 56, 5, 23]

# 循环myArray里的每一个元素
for item in my_list:
# 这会对item翻倍, 但并不改变原数组
# 因为item只是一个复制品
item = item * 2

# 打印结果
print(my_list)


## 7.6 切割字符串

x = "这是一个示例字符串"
#x = "0123456789"

print("x=", x)

# 访问单个字符
print("x[0]=", x[0])
print("x[1]=", x[1])

# 从右端开始访问
print("x[-1]=", x[-1])

# 访问 0-5
print("x[:6]=", x[:6])
# 访问 6
print("x[6:]=", x[6:])
# 访问 6-8
print("x[6:9]=", x[6:9])


Python中的字符串可以进行一些数学的运算。 试着运行下面这段代码，看看Python砸做什么:

a = "Hi"
b = "There"
c = "!"
print(a + b)
print(a + b + c)
print(3 * a)
print(a * 3)
print((a * 2) + (b * 2))


a = "Hi There"
print(len(a))

b = [3, 4, 5, 6, 76, 4, 3, 3]
print(len(b))


for character in "This is a test.":
print(character)


months = "JanFebMarAprMayJunJulAugSepOctNovDec"

n = int(input("Enter a month number: "))


## 7.7 秘密代码

plain_text = "This is a test. ABC abc"

for c in plain_text:
print(c, end=" ")


Unicode的表格用数字0-127涵盖了西方字母表。每个西方字符用内存中的一个字节表示。 其他字母表，比如西里尔字母，需要多个字节来表示美国字母。 下面是Unicode表格的一个部分:
 Value Character Value Character Value Character Value Character 40 ( 61 = 82 R 103 g 41 ) 62 > 83 S 104 h 42 * 63 ? 84 T 105 i 43 + 64 @ 85 U 106 j 44 , 65 A 86 V 107 k 45 - 66 B 87 W 108 l 46 . 67 C 88 X 109 m 47 / 68 D 89 Y 110 n 48 0 69 E 90 Z 111 o 49 1 70 F 91 [ 112 p 50 2 71 G 92 \ 113 q 51 3 72 H 93 ] 114 r 52 4 73 I 94 ^ 115 s 53 5 74 J 95 _ 116 t 54 6 75 K 96  117 u 55 7 76 L 97 a 118 v 56 8 77 M 98 b 119 w 57 9 78 N 99 c 120 x 58 : 79 O 100 d 121 y 59 ; 80 P 101 e 122 z 60 < 81 Q 102 f

http://en.wikipedia.org/wiki/ASCII

plain_text = "This is a test. ABC abc"

for c in plain_text:
print(ord(c), end=" ")


plain_text = "This is a test. ABC abc"

for c in plain_text:
x = ord(c)
x = x + 1
c2 = chr(x)
print(c2, end="")


# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://simpson.edu/computer-science/

# Explanation video: http://youtu.be/sxFIxD8Gd3A

plain_text = "This is a test. ABC abc"

encrypted_text = ""
for c in plain_text:
x = ord(c)
x = x + 1
c2 = chr(x)
encrypted_text = encrypted_text + c2
print(encrypted_text)



# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://simpson.edu/computer-science/

# Explanation video: http://youtu.be/sxFIxD8Gd3A

encrypted_text = "Uijt!jt!b!uftu/!BCD!bcd"

plain_text = ""
for c in encrypted_text:
x = ord(c)
x = x - 1
c2 = chr(x)
plain_text = plain_text + c2
print(plain_text)



## 7.8 关联数组

Python并不限制于只使用数字来做数组的索引。我们也可以使用关联数组这种数据结构。 它是这样工作的：

# 创建一个空白的关联数组
# (注意使用了曲括号)
x = {}

# 添加一些内容进去
x["fred"] = 2
x["scooby"] = 8
x["wilma"] = 1

# 获取并打印其中一个元素
print(x["fred"])
`