python基础 Day9

python Day9

  • 函数的初识
    #代码的可读性较好
    s=[1,2,3,4,5,5]
    def list_len(S):
        count=0
        for i in s:
            count+=1
        print(count)
    list_len(s)
    
    • 函数是以功能(完成一件事)为向导,登录,注册,len.一个函数就是一个共功能
  • 函数的结构和调用
    • def 关键字,定义函数
    • list_len 函数名:与变量设置相同,其有可描述性
    • 函数体:缩进。函数中尽量不要出现print
  • 函数的返回值(Return)
    • 在函数中遇到的return直接结束函数,终止函数

    • 将数据返回函数的执行者,调用者list_len()

      def test():
          print("注视对方")
          return "FDs"
      print(test())
      ###结果注视对方
      #FDs
      
    • return返回多个元素,是以元组的形式返回给函数的执行者,对多个返回值提取可以利用

  • 函数的传参:让函数封装这的这个功能,盘活。函数的传参分为实参和形参
    • 实参角度
      • 位置参数:从左到右一一对应

        def meet(sex,age):
            print("打开软件")
            print("性别:%s,年龄:%s"%(sex,age))
            return
        meet("男生","13")
        
      • 三元与运算符

        a=100
        b=10
        if a>b:
            c=a
        else:
            c=b
        print(c)
        ##改写为
        c=a if a>b else b
        print(c)
        
      • 关键字参数(必须满足一一对应,但是可以调换位置)

        def meet(name,age,hobby):
            print("姓名:%s,年龄:%s,爱好:%s"%(name,age,hobby))
            return
        meet(name="zhuxaiyu",hobby="football",age=19)
        
      • 混合传参(位置参数一定要在关键字参数的前面)

        def meet(name,age,hobby):
            print("姓名:%s,年龄:%s,爱好:%s"%(name,age,hobby))
            return
        meet("zhuxaiyu",hobby="football",age=19)
        
    • 形参角度(是实参角度中位置参数的一种)
      • def meet(name,age,weight,skill="fdsfsd"):
            print("姓名:%s,年龄:%s,体重:%s,技能:%s"%(name,age,weight,skill))
            return
        meet("zhuxiayu",19,55)
        
  • 作业

    • 写函数,计算传入函数的字符串中数字、字母、以及其他的个数

      #写函数,计算传入函数的字符串中数字、字母、以及其他的个数
      def count_fuc(str):
          count_number=0
          count_a=0
          count_b=0
          for i in str:
              if i.isdecimal():
                  count_number+=1
              elif i.isalpha():
                  count_a+=1
              else:
                  count_b+=1
          return print("数字:%s,字母:%s,其他:%s"%(count_number,count_a,count_b))
      str="fdsfdsf322@"
      count_fuc(str)
      ### 数字:3,字母:7,其他:1
      
    • 写一个函数,此函数只接收一个参数且参数必须是列表数据类型,此函数的完成功能是返回给调用者一个字典。eg:传入的列表为[1,2,3,4],返回的字典为

      def fun(list1):
          dict1={}
          for i in range(len(list1)):
              dict1[i]=list1[i]
          return dict1
      list1=[1,2,3,4]
      print(fun(list1))
      
      
    • 写函数,函数接收四个参数分别是:姓名,性别,年龄,学历,用户通过这四个内容传送到函数中,此函数接受这个这个内容并追加到student_messgae.txt这个文本中,支持用户持续输入,Q或者q退出

      def fuc(name,sex,age,qualifications):
          with open("student_message.txt",encoding="utf-8",mode="a") as f:
              f.write("%s,%s,%s,%s\n"%(name,sex,age,qualifications))
      while True:
          name=input("please input your name:")
          if name.upper()=="Q":
             break
          else:
              sex=input("please input your sex:")
              age = input("please input your age:")
              qualifications= input("please input your qualifications:")
              fuc(name,sex,age,qualifications)
      
      
    • 写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成整个函数的修改操作。

      import os
      def refile(file1,oldcontent,newcontent):
          with open(file1,encoding="utf-8") as f,\
              open("new{}".format(file1),encoding="utf-8",mode="w") as f1:
              for line in f:
                  content=line.replace(oldcontent,newcontent)
                  f1.write(content)
              # old_content=f.read()
              # new_content=old_content.replace(oldcontent,newcontent)
              # f1.write(new_content)
          os.remove(file1)
          os.rename("new{}".format(file1),file1)
      refile("student_message.txt","房贷首付","没钱")
      
      
posted @ 2020-08-13 15:41  大旺叫我来巡山  阅读(99)  评论(0编辑  收藏  举报