第六章 字典
alien={"color":"red","points":5}
alien["color"]
'red'
new_points=alien["points"]
print("You just earned "+str(new_points)+" points!")
You just earned 5 points!
增加
alien["x"]=1
alien["y"]=2
alien
{'color': 'red', 'points': 5, 'x': 1, 'y': 2}
删除
del alien["y"]
alien
{'color': 'red', 'points': 5}
people={"名":"lucy","姓":"Amy","年龄":"20","城市":"Los"}
people
{'名': 'lucy', '姓': 'Amy', '年龄': '20', '城市': 'Los'}
遍历
likes={"xiao":"python","wang":"r","liu":"c++"}
likes
for key,value in likes.items():
print("\nKey: "+key)
print("Value: "+value)
Key: xiao
Value: python
Key: wang
Value: r
Key: liu
Value: c++
for key in likes.keys():
print(key)
xiao
wang
liu
for value in likes.values():
print(value)
python
r
c++
river={"a":"1","b":"2","c":"3"}
for key,value in river.items():
print("The "+key+" runs through "+value)
The a runs through 1
The b runs through 2
The c runs through 3
第七章 用户输入和while循环
input函数
name=input("Please enter your name:")
print("\nHello, "+ name + "!")
print("I love "+name)
Please enter your name:mary
Hello, mary!
I love mary
int函数
age=input("What's your age:")
age=int(age)
What's your age:21
age_1=input("What's your age:")
What's your age:21
age_1=input("What's your age:")
What's your age:21
求模运算符
4%3
1
number=input("How many people:")
number=int(number)
if number>=8:
print("There' not seat")
else:
print("There's "+ number +"seats!")
How many people:10
There' not seat
number=input("How many people:")
if int(number)>=8:
print("There' not seat")
else:
print("There are "+ number +" seats!")
How many people:6
There are 6 seats!
while语句
第八章 函数
def greet():
print("hello")
greet()
hello
def greet(user):
print("hello "+user+"!")
greet("mary")
hello mary!
def greet(names):
for name in names:
print("hello "+name+"!")
names_=["mary","wang"]
greet(names_)
hello mary!
hello wang!
magic_=["xiao","wang","li"]
def show_magician(names):
for name in names:
print(name)
show_magician(magic_)
xiao
wang
li
magic_=["xiao","wang","li"]
magic_x=[]
def make_great(names):
for name in names:
print("The Great "+name)
make_great(magic_)
The Great xiao
The Great wang
The Great li
magic_x=["the great "+x for x in magic_]
magic_x
['the great xiao', 'the great wang', 'the great li']
show_magician(magic_)
xiao
wang
li
show_magician(magic_x)
the great xiao
the great wang
the great li
def make_great(names):
for name in names:
print("The Great "+name)
make_great(magic_[:]))
The Great xiao
The Great wang
The Great li
magic_x=
magic_x
show_magician(magic_x)
`
```python