zoukankan      html  css  js  c++  java
  • 自动化学习-Day05

    深拷贝&浅拷贝

    import copy
    l = [1,1,1,2,3,4,5]
    l2 = copy.deepcopy(l)# 深拷贝
    
    l2 = l #浅拷贝
    l2 = l.copy() #浅拷贝
    
    print(id(l))
    print(id(l2))
    
    a='abcd'
    b = a
    a='eeee'
    
    print(a) #eeee
    print(b) #abcd

    非空即真

    #非空即真,非零即真
    name = input('请输入名称:').strip()
    #a=''
    #l=[]
    #d={}
    #t=()
    #b=Noneif name:
        print('输入正确')
    else:
        print('name不能为空')

    默认值参数

    def my(name,sex=''):
        #name 必填参数、位置参数
        #sex 默认值参数
        #可变参数
        #关键字参数
        pass

    可变参数

    def send_sms(*args):
        #可变参数,参数组
        #1、不是必传的
        #2、它把传入的元素全部都放到了一个元组里面
        #3、不限制参数个数
        #4、它用在参数比较多的情况下
        for p in args:
            print(p)

    关键字参数

    def send_sms2(**kwargs):
        #1、不是必传的
        #2、不限制参数个数
        print(kwargs)
    
    send_sms2()
    send_sms2(name='xiaohei',sex='nan')
    send_sms2(addr='北京',country='中国',c='abc',f='kkk')
    def my(name,country='China',*args,**kwargs):
        #1、位置参数 2、默认值参数 3、可变参数 4、关键字
        print(name)
        print(country)
        print(args)
        print(kwargs)
    # my('xiaojun','Japan','beijing','天通苑',color='红色',age=32)

    函数练习

    def my(name,sex):
        #位置参数,必填
        #默认值参数
         #函数体
        return name
    def db_connect(ip,port=3306):
        print(ip,port)
    
    res = db_connect('118.24.3.40',3307)
    def my2():
        for i in range(50):
            return i
    def my3():
        a = 1
        b = 2
        c = 3
        return a,b,c
    import json
    def op_file_tojson(file_name,dic=None):
        if dic:
            with open(file_name,'w',encoding='utf-8') as fw:
                json.dump(dic,fw)
        else:
            f = open(file_name,encoding='utf-8')
            content = f.read()
            if content:
                res = json.loads(content)
            else:
                res = {}
            f.close()
            return res
    a,b,c = 1,2,3
    a = b = c = 1
    str1 = 'aaa'
    str1 = str2
    str1 = 'bbb'
    print(str2) #aaa
    print(str1) #bbb

    #return 有2个作用 #1、结束函数,只要函数里面遇到return,函数立即结束运行 #2、返回函数处理的结果
    def check_float(s):
        '''
        这个函数的作用就是判断传入的字符串是否是合法的小数
        :param s: 传入一个字符串
        :return: True/false
        '''
        s = str(s)
        if s.count('.')==1:
            s_split = s.split('.')
            left,right = s_split
            if left.isdigit() and right.isdigit():
                return True
            elif left.startswith('-') and left[1:].isdigit() 
                and right.isdigit():
                return True
        return False
    print(check_float(1.3))
    print(check_float(-1.3))
    print(check_float('01.3'))
    print(check_float('-1.3'))
    print(check_float('-a.3'))
    print(check_float('a.3'))
    print(check_float('1.3a3'))
    print(check_float('---.3a3'))

    递归

    def test1():
        num = int(input('please enter a number:'))
        if num%2 == 0:
            return True
        print('不是偶数请重新输入')
        return test1()
    print(test1())

    集合

    #集合,
        # 1、天生可以去重
        # 2、集合是无序
    
    l2=[1,1,2,2,3,3]
    l2.add('s')#添加元素
    l2.remove('a')#删除指定的元素
    l2.pop()#随机删除一个元素
    for l in l1:
        print(l)

    模块

    #模块
    # 1、标准模块,不需要你单独安装,python自带的模块
    # 2、第三方模块
    # 3、自己写的python
    #一个python文件就是一个模块
    import random
    print(random.randint(100000,999999)) #随机取一个整数
    print(random.uniform(1,900))#取一个小数
    stus = ['xiaojun','hailong','yangfan','tanling','yangyue','cc']
    print(random.choice('abcdefg'))#随机取一个元素
    print(random.sample(stus,2)) #随机取N个元素
    
    l = list(range(1,101))
    print('洗牌之前的',l)
    print(random.shuffle(l))#洗牌,这个就只能传list了
    print('洗牌之后的',l)

    作业

    FILENAME='products.json'
    #常量
    import json
    def get_file_content():
        with open(FILENAME,encoding='utf-8') as f :
            content = f.read()
            if content:
                res = json.loads(content)
            else:
                res = {}
        return res
    
    
    def write_file_content(dic):
        with open(FILENAME,'w',encoding='utf-8') as fw:
            json.dump(dic,fw,indent=4,ensure_ascii=False)
    
    def check_digit(st:str):
        if st.isdigit():
            st = int(st)
            if st>0:
                return st
        return 0
    
    def add_product():
        product_name = input('请输入商品名称:').strip()
        count = input('请输入商品数量:').strip()
        price = input('请输入商品价格:').strip()
        all_products = get_file_content()
        if check_digit(count) == 0:
            print('数量输入不合法')
        elif check_digit(price) == 0:
            print('价格输入不合法')
        elif product_name in all_products:
            print('商品已经存在')
        else:
            all_products[product_name] = {"count":int(count),
                                          "price":int(price)}
            write_file_content(all_products)
            print('添加成功!')
    
    
    def show_product():
        product_name = input('请输入要查询的商品名称:').strip()
        all_products = get_file_content()
        if product_name=='all':
            print(all_products)
        elif product_name not in all_products:
            print('商品不存在')
        else:
            print(all_products.get(product_name))
    
    
    
    def del_product():
        product_name = input('请输入要删除的商品名称:').strip()
        all_products = get_file_content()
        if product_name in all_products:
            all_products.pop(product_name)
            print('删除成功')
            write_file_content(all_products)
        else:
            print('商品不存在')
    
    
    
    
    
    
    choice = input('请输入你的选择:
     1、添加商品 2、删除商品 3、查看商品信息')
    if choice=="1":
        add_product()
    elif choice=="2":
        del_product()
    elif choice=="3":
        show_product()
    else:
        print('输入错误,请重新输入!')
    def test1():
        num = int(input('please enter a number:'))
        if num%2 == 0:
            return True
    print('不是偶数请重新输入')
        return test1()
    print(test1())
  • 相关阅读:
    2020软件工程作业04
    2020软件工程作业03
    2020软件工程作业02
    2020软件工程作业01
    2020软件工程作业02
    2020软件工程作业01
    WEB学习路线2019完整版(附视频教程+网盘下载地址)
    (2018干货系列七)最新大数据学习路线整合
    (2018干货系列六)最新云计算学习路线整合
    (2018干货系列五)最新UI设计学习路线整合
  • 原文地址:https://www.cnblogs.com/rongpeng/p/12014493.html
Copyright © 2011-2022 走看看