#本文章仅用于记录本人学习过程,当作笔记来用,如有侵权请及时告知,谢谢!
P8:
#if条件测试
list1 = ['aa', 'bb', 'cc']
for zimu in list1:
if zimu == 'bb':
print(zimu.title())
else:
print(zimu.upper())
# AA
# Bb
# CC
P9:
""" #简单的if语句
age = 19
if age >= 18:
print("you are old enough!")
#you are old enough!
#if-else从句
age = 17
if age >= 18:
print("you are old enough!")
else:
print("No yet!")
#No yet!
#if-elif-else语句
age = 12
if age <= 4:
print("give me 0 yuan")
elif age <=12:
print("give me 12 yuan")
else:
print("give 13 yuan")
#give me 12 yuan
#多个elif语句
age = 12
if age <= 4:
print("give me 0 yuan")
elif age <=12:
print("give me 12 yuan")
elif age <=24:
print("give me 24 yuan")
else:
print("give 13 yuan")
#give me 12 yuan
#省略else语句
age = 12
if age <= 4:
print("give me 0 yuan")
elif age <=12:
print("give me 12 yuan")
elif age <=24:
print("give me 24 yuan")
elif age > 24:
print("give 13 yuan")
#give me 12 yuan
#测试多个条件
list1 = ['a', 'b', 'c']
if 'a' in list1:
print("list1 have a")
if 'b' in list1:
print("list1 have b")
if 'c' in list1:
print("list1 have c")
print("\nall is done!")
# list1 have a
# list1 have b
# list1 have c
# all is done!
P10:
#确定列表不是空的
list1 = []
if list1:
for zimu in list1:
print(zimu)
else:
print("it's empty.")
#it's empty.
#使用多个列表
list2 = ['a','b','c']
list3 = ['a','b','c','e']
for zimu in list3:
if zimu in list2:
print("list2 also has the same zimu.")
else:
print("list2 doesn't have the same zimu.")
# list2 also has the same zimu.
# list2 also has the same zimu.
# list2 also has the same zimu.
# list2 doesn't have the same zimu.
P11:
#一个简单的字典
zidian = {'mingzi':'1', 'color':'red'}
print(zidian['mingzi'])
print(zidian['color'])
# 1
# red
#访问字典中的值
new_mingzi = zidian['mingzi']
print('You have got a new name:' + str(new_mingzi) + ' haha!')
# You have got a new name:1 haha!
#添加键值对
zidian['x_position']=0
zidian['y_position']=25
print(zidian)
#{'mingzi': '1', 'color': 'red', 'x_position': 0, 'y_position': 25}
#先创建一个空字典
zidian1 = {}
zidian1['mingzi'] = '2'
zidian1['color']= 'red'
print(zidian1)
#{'mingzi': '2', 'color': 'red'}
#修改字典中的键值
zidian1['mingzi'] = '3'
print(zidian1['mingzi'])
#3
zidian3 = {'x_position':0, 'y_position':25, 'speed':'medium'}
print('original x-position:' + str(zidian3['x_position']))
if zidian3['speed'] == 'slow':
x_increment = 1
elif zidian3['speed'] == 'medium':
x_increment = 2
else :
x_increment =3
zidian3['x_position'] = zidian3['x_position'] + x_increment
print('New x-position:' + str(zidian3['x_position']))
# original x-position:0
# New x-position:2
#删除键值对
zidian4 = {'x_position':0, 'y_position':25, 'speed':'medium'}
del zidian4['speed']
print(zidian4)
#{'x_position': 0, 'y_position': 25}
#由类似对象组成的字典
favorite_language = {
'a':'c',
'b':'python',
'c':'vb'
}
print("a's favorite language is " + favorite_language['a'].title() +'.')
#a's favorite language is C.
P12:
#遍历字典中所有键——值对
user_0 = {
'username' : 'steve',
'first' : 'a',
'last' : 'b',
}
for jian,zhi in user_0.items(): #items()返回键值对列表 存储至jian,zhi两个变量中
print('\njian:' + jian)
print('\nzhi:' + zhi)
# jian:username
# zhi:steve
# jian:first
# zhi:a
# jian:last
# zhi:b
favorite_language = {
'a':'c',
'b':'python',
'c':'vb'
}
for name,language in favorite_language.items():
print(name.title() + "'s language is " + language + '.')
# A's language is c.
# B's language is python.
# C's language is vb.
#遍历字典中所有的键
favorite_language = {
'a':'c',
'b':'python',
'c':'vb'
}
for name in favorite_language.keys():
print(name.title())
# A
# B
# C
#按字母顺序遍历字典中所有的键
favorite_language = {
'a':'c',
'b':'python',
'c':'vb'
}
for name in sorted(favorite_language.keys()):
print(name.title())
# A
# B
# C
#遍历字典中的所有值
favorite_language = {
'a':'c',
'b':'python',
'c':'vb'
}
for languages in favorite_language.values():
print(languages.title())
# C
# Python
# Vb
#set 找出列表中独一无二的元素,并使用这些元素创建一个集合
favorite_language = {
'a':'c',
'b':'python',
'c':'vb',
'd':'c'
}
for languages in set(favorite_language.values()):
print(languages.title())
# C
# Python
# Vb
P13:
#嵌套
#字典列表
alien_0 = {'color':'red','points':5}
alien_1 = {'color':'yellow','points':10}
alien_2 = {'color':'blue','points':15}
aliens = [alien_0,alien_1,alien_2]
for alien in aliens:
print(alien)
{'color': 'red', 'points': 5}
{'color': 'yellow', 'points': 10}
{'color': 'blue', 'points': 15}
#创建一个外星人空列表
aliens = []
#创建30个外星人
for alien_number in range(30):
new_alien = {'color':'red', 'points':5, 'speed':'low'}
aliens.append(new_alien)
#显示前五个外星人
for alien in aliens[:5]:
print(alien)
print('...')
#显示外星人的个数
print('the number of aliens are: ' + str(len(aliens)))
# {'color': 'red', 'points': 5, 'speed': 'low'}
# {'color': 'red', 'points': 5, 'speed': 'low'}
# {'color': 'red', 'points': 5, 'speed': 'low'}
# {'color': 'red', 'points': 5, 'speed': 'low'}
# {'color': 'red', 'points': 5, 'speed': 'low'}
# ...
# the number of aliens are: 30
#修改前三个外星人的颜色
for alien in aliens[0:3]:
if alien['color'] == 'red':
alien['color'] = 'green'
alien['speed'] = 'medium'
for alien in aliens[:5]:
print(alien)
print('...')
# {'color': 'green', 'points': 5, 'speed': 'medium'}
# {'color': 'green', 'points': 5, 'speed': 'medium'}
# {'color': 'green', 'points': 5, 'speed': 'medium'}
# {'color': 'red', 'points': 5, 'speed': 'low'}
# {'color': 'red', 'points': 5, 'speed': 'low'}
# ...
#在字典中存储列表
pizza ={
'list' : 'a',
'list1' : ['b', 'c']
}
print('Your pizza has ' + pizza['list'] + 'and the following list: ')
for list1 in pizza['list1']:
print("\t" + list1)
# Your pizza has aand the following list:
# b
# c
#字典中存储字典
users = {
'a' : {
'first' : 'aa',
'last' : 'ab',
'location' : 'abc'
},
'b' : {
'first' : 'ba',
'last' : 'bb',
'location' : 'bbc'
}
}
for user_name,user_info in users.items():
print("\nusername: " + user_name)
full_name = user_info['first'] + user_info['last']
location = user_info['location']
print("\tfull name: " + full_name.title())
print("\tlocation: " + location.title())
# username: a
# full name: Aaab
# location: Abc
# username: b
# full name: Babb
# location: Bbc
P14:
#input函数
message = input("say it: ")
print(message)
#say it:
#编写清晰的程序
prompt = 'hey '
prompt += '\nhow are you?'
print(prompt)
#用int()获取输入,转换成数值型数据进行比较
age = input('how old are you?')
age = int(age)
age >= 18
#求模预算符%
number = 12
if number % 2 == 0:
print("The number " + str(number) + " is even.")
else:
print("The number " + str(number) + " is odd.")
#使用while循环
number = 1
while number <= 5:
print(number)
number += 1
# 1
# 2
# 3
# 4
# 5
#使用标志
message = "tell me sth: "
active = True
while active:
message = input('ok?')
if message == 'no':
active = False
else:
print(message)
#跳出循环
message = "tell me sth: "
active = True
while active:
message = input('ok?')
if message == 'no':
break
else:
print(message)