py3实例

@dict-1

init = {"x":0,"y":25,"speed":"medium"}
print("初始位置:\n" + "x: " + str(init["x"]) + "\ny: " + str(init["y"]) + "\nspeed:" + init["speed"] + "\n")   
if init["speed"] == "slow":
    x_increment = 1
elif init["speed"] == "medium":
    x_increment = 2
else:
    x_increment = 3
init["x"] = init["x"] + x_increment
print("现在的位置:\nx:{0}\ny:{1}\nspeed:{2}\n".format(init["x"],init["y"],init["speed"]))

运行结果:
初始位置:
x: 0
y: 25
speed:medium

现在的位置:
x:2
y:25
speed:medium
@dict-2

init_list = []
for x in range(1,10):
    mydict = {
        "xiaojie":{"LAN":"python","pet":"dog"},
        "shuangzhang":{"LAN":"java","age":"29","addr":"soho","tel":"110"},
        "zz":{"LAN":"PHP","age":"none"},
        }
    init_list.append(mydict)
for y in init_list[0:3]:
    if y["zz"] == {"LAN":"PHP","age":"none"}:
        y["zz"] = {"LAN":"PHP","age":"none","sex":"male"}

for z in init_list[0:4]:
    print(z) 

运行效果:
{‘xiaojie’: {‘LAN’: ‘python’, ‘pet’: ‘dog’}, ‘shuangzhang’: {‘LAN’: ‘java’, ‘age’: ‘29’, ‘addr’: ‘soho’, ‘tel’: ‘110’}, ‘zz’: {‘LAN’: ‘PHP’, ‘age’: ‘none’, ‘sex’: ‘male’}}
{‘xiaojie’: {‘LAN’: ‘python’, ‘pet’: ‘dog’}, ‘shuangzhang’: {‘LAN’: ‘java’, ‘age’: ‘29’, ‘addr’: ‘soho’, ‘tel’: ‘110’}, ‘zz’: {‘LAN’: ‘PHP’, ‘age’: ‘none’, ‘sex’: ‘male’}}
{‘xiaojie’: {‘LAN’: ‘python’, ‘pet’: ‘dog’}, ‘shuangzhang’: {‘LAN’: ‘java’, ‘age’: ‘29’, ‘addr’: ‘soho’, ‘tel’: ‘110’}, ‘zz’: {‘LAN’: ‘PHP’, ‘age’: ‘none’, ‘sex’: ‘male’}}
{‘xiaojie’: {‘LAN’: ‘python’, ‘pet’: ‘dog’}, ‘shuangzhang’: {‘LAN’: ‘java’, ‘age’: ‘29’, ‘addr’: ‘soho’, ‘tel’: ‘110’}, ‘zz’: {‘LAN’: ‘PHP’, ‘age’: ‘none’}}

@dict-3

mydict = {
    "tom":[{"语言":"PHP"},{"年龄":20},{"宠物":"狗"}],
    "mary":[{"语言":"PYTHON"},{"年龄":25},{"宠物":"猫"}],
}
for name,infos in mydict.items():
    print("\n" + name.title() + "的信息:")
    for info in  infos:
        print(info) 

运行结果:
Tom的信息:
{‘语言’: ‘PHP’}
{‘年龄’: 20}
{‘宠物’: ‘狗’}

Mary的信息:
{‘语言’: ‘PYTHON’}
{‘年龄’: 25}
{‘宠物’: ‘猫’}

@flag

msg = input("请输入一些内容,输入'quit'终止程序运行: ")
flag = True
while flag:
    if msg == "quit":
        flag = False
    else:
       print(msg)

@while-1

unconfirm_user = ["alice","brian","candace"]
confirm_user = []
while unconfirm_user:
    current_user = unconfirm_user.pop()
    print("正在认证的用户:"+current_user.title())
    confirm_user.append(current_user)
print("\n下列用户已认证:")
for i in confirm_user:
    print(i)

运行结果:
正在认证的用户:Candace
正在认证的用户:Brian
正在认证的用户:Alice

下列用户已认证:
candace
brian
alice

@while-2

mydict = {}
flag = True
while flag:
    name = input("请输入的名字: ")
    info = input("请输入你的回复意见: ")
    mydict[name] = info
    msg = input("还有其他人要参见调查活动么?(yes/no) ")
    if msg == "no":
        flag = False
for name,info in mydict.items():
    print(name + ":" + info)

运行结果
请输入的名字: tom
请输入你的回复意见: dog
还有其他人要参见调查活动么?(yes/no) yes
请输入的名字: bob
请输入你的回复意见: cat
还有其他人要参见调查活动么?(yes/no) no

@list-1

mylists = ["aa","bb","cc"]
for mylist in mylists:
    mylists.pop()
    mylists = mylists[0:-1]
print(mylists)

运行结果:
[]

@def

def person(first,last,age=''):
    p = {"first":first,"last":last}
    if age:
        p = {"first":first,"last":last,"age":age}
    else:
        p = {"first":first,"last":last}
    return p
p1 = person("ma","yun")
p2 = person("lei","jun","50")
print(p1)
print(p2)

运行结果:
{‘first’: ‘ma’, ‘last’: ‘yun’}
{‘first’: ‘lei’, ‘last’: ‘jun’, ‘age’: ‘50’}

@def + while

def names(first,last):
    full_name = first + " " + last
    return full_name

while True:
    f_name = input("firstname: ")
    l_name = input("lastname: ")
    formate_name = names(f_name,l_name)
    print("\nHello,"+formate_name.title() + "!")

运行结果:
firstname: sm
lastname: lz

Hello,Sm Lz!
firstname:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值