一些基础
更多内容在仓库看起来更方便~
dictionaries.py
# 类似 js 对象
# 访问值
items = {
'color': 'yellow',
'price': 22,
}
print(items['color']) # yellow
print(items['price']) # 22
print(items.get('color')) # yellow
print(items.get('color1')) # None
# 添加值
items['x'] = 10
items['y'] = 20
print(items) # {'color': 'yellow', 'price': 22, 'x': 10, 'y': 20}
# 修改值
items['x'] = 180
print(items['x']) # 180
# 删除
del items['x']
print(items) # {'color': 'yellow', 'price': 22, 'y': 20}
while.py
current_number = 1
# while current_number < 5:
# current_number += 1
# print(current_number) # 1-5
# active = True
# while active:
# message = input('Enter: ')
#
# if message == 'quit':
# active = False
# break
# else:
# print(message)
# counting
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number) # 3 5 7 9
def.py
def hello():
print('hello')
hello()
# 参数
def getUser(name='cc', age=20):
print(f'name is {name} , age is {age}')
getUser('ppx', 18) # name is ppx , age is 18
# Returning a Dictionary
def has_person(first_name, last_name):
person = {'first': first_name,
'last': last_name}
return person
p = has_person('ppx', 'cc')
print(p) # {'first': 'ppx', 'last': 'cc'}
# 传递 list
def greet(names):
for name in names:
msg = f'hello {name}'
print(msg)
name_list = ['ppx', 'll', 'mm', 'cc']
greet(name_list)
# hello ppx
# hello ll
# hello mm
# hello cc
# 剩余参数分配 这个表达有点新
def build_person(first, last, **user_info):
user_info['first_name'] = first
user_info['last_name'] = last
return user_info
val = build_person('ppx', 'cc', location='CG', field='PP')
print(val) # {'location': 'CG', 'field': 'PP', 'first_name': 'ppx', 'last_name': 'cc'}