• 循环,元组,字典,列表练习题


    !/usr/bin/env python
    -*- coding: utf-8 -*-
    @Time : 2017/7/20 16:45
    @Author : Aries
    @Site :
    @File : 20.py
    @Software: PyCharm
    默写任务
    # 1、将两个变量的值交换顺序
    x=10
    y=20
    x,y=y,x
    print(x,y)
    
    # 2、从[1,2,3,4,5,6,7]取出第一个值和最后两个值
    qq=[1,2,3,4,5,6,7]
    print(qq[0],qq[-2:])
    
    # 3循环打印如下字典的key和value
    msg_dic={
    'apple':10,
    'tesla':1000000,
    'mac':3000,
    'lenovo':30000,
    'chicken':10,
    }
    for k in msg_dic:
        print(k,msg_dic[k])
    4、用append+pop模拟队列和堆栈;用insert+pop模拟队列和堆栈
    l=[]
        #入列append+pop
    l.append('first')
    l.append('second')
    l.append('third')
    print(l)
        #出列
    print(l.pop(0))
    print(l.pop(0))
    print(l.pop(0))
    
    l=[]
            #入列insert+pop
    l.insert(0,'first')
    l.insert(1,'second')
    l.insert(2,'third')
    print(l)
            #出列
    print(l.pop())
    print(l.pop())
    print(l.pop())
    
    # 5,、循环取字典的key   循环取字典的value  循环取字典的items
    msg_dic={
    'apple':10,
    'tesla':1000000,
    'mac':3000,
    'lenovo':30000,
    'chicken':10,
    }
    for k in msg_dic.keys():
        print('key=',k)
    print('----------------------------')
    for v in msg_dic.values():
        print('value=',v)
    print('----------------------------')
    for k,v in msg_dic.items():
        print(k,v)


    # 作业一:
    # 打印99乘法表
    for i in range(1,10):
        for j in range(1,i+1):
            print('%s*%s=%s'%(i,j,i*j),end=' ')
        print('')

    作业二:
    简单的购物车
    实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数加入购物列表,如果输入为空或其他非法输入则要求用户重新输入  
    
    msg_dic={
    'apple':10,
    'tesla':100000,
    'mac':3000,
    'lenovo':30000,
    'chicken':10,
    }
    goods=[]
    while True:
        for k,v in msg_dic.items():
            print('商品名:',k,'价格:',v)
    
        name=input('请输入商品名:').strip()
        if name not in msg_dic:
            continue
        num=input('请输入商品的数量:').strip()
        if not num.isdigit():
            continue
        goods.append((name,msg_dic[name],num))
        print('购物车清单',goods)
        price=int(num)*msg_dic[name]
        print('总价是:',price)
        break

    作业三:
    字典练习
    1
    有如下值集合[11, 22, 33, 44, 55, 66, 77, 88, 99, 90...],将所有大于66的值保存至字典的第一个key中,将小于66的值保存至第二个key的值中。(2分)
    即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
    coll = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
    result = {
        "less": [],
        "greater": []
    }
    for pos in range(0, len(coll)):
        if coll[pos] < 66:
            result.get("less").append(coll[pos])
        elif coll[pos] > 66:
            result.get("greater").append(coll[pos])
    print(result)
    2
    统计s = 'hello alex alex say hello sb sb'中每个单词的个数
    结果如:{'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}
    s = 'hello alex alex say hello sb sb'
    print('hello 出现的次数是:',s.count('hello'))
    print('alex 出现的次数是:',s.count('alex'))
    print('sb 出现的次数是:',s.count('sb'))
    print('say 出现的次数是:',s.count('say'))
    
    
  • 相关阅读:
    数据库备份脚本
    redismyadmin安装(支持redis4 集群模式)
    elasticsearch ik安装
    centos7.2 +cloudstack 4.11 +KVM +ceph 安装配置(网卡带聚合)
    cloudstack4.11+KVM+4网卡bond5+briage 交换机不作配置
    web service:AxisFault faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
    根据WSDL生成客户端代码(XFire)
    Apache axis2 + Eclipse 开发 WebService
    The processing instruction target matching "[xX][mM][lL]" is not allowed.
    使用dom4j解析xml文件,并封装为javabean对象
  • 原文地址:https://www.cnblogs.com/52-qq/p/7214832.html
Copyright © 2020-2023  润新知