zoukankan      html  css  js  c++  java
  • 函数初识 09

    1. 函数初识

      • 写一个获取字符串总个数的代码,不能用len:
      s1 = 'fjkdsfjdssudafurpojurojregreuptotuproq[t'
      # python没有len
      count = 0
      for i in s1:
          count += 1
      print(count)
      
      • 写一个获取列表总个数的代码:
      l1 = [1, 2, 3, 4, 5, 6]
      count = 0
      for i in l1:
          count += 1
      # print(count)
      

      这样写代码low ,重复代码很多。代码可读性差。

      • 利用函数写出上面功能:

        s1 = 'fsjkdafshdjfsdhafjksda'
        l1 = [1,2,3,4,5,6]
        
        def my_len(s):
        	count = 0
        	for i in s:
        		count += 1
        	print(count)
        my_len(s1)
        my_len(l1)
        
      • 函数:以功能(完成一件事)为导向,登陆,注册,len,一个函数就是一个功能。随调随用

      • 函数优点:

        • 减少代码的重复量
        • 增强代码的可读性
    2. 函数的结构与调用

      • 结构:def 关键字 ,定义函数。holting 函数名:与变量设置相同,具有可描述性。函数体缩进。函数中尽量不要出现print
      • 函数什么时候执行?
        • 当函数遇到函数名() 函数才会执行
    3. 函数的返回值

      • 在函数中,终止函数。
      • return 可以给函数的执行者返回值:
        • return 单个值 单个值
        • return 多个值 (多个值,)
    4. 函数的参数

      函数的传参:让函数封装的这个功能,盘活。分两个角度:实参,形参。

      • 实参角度

        1. 参数位置:从左到右,一一对应。

          写一个函数,只接受两个int的参数,函数的功能是将较大的数返回。
          def compile(a,b):
              c = 0
              if a > b:
                  return c
              else:
                  return c
          print(compile(10,20))
          print(compile(1000,1))
          compile(1000,20)
          
          三元与运算符: 简单的if else
          a = 1000
          b = 2000
          if a > b:
              c = a
          else:
              c = b
          print(c)
          
          a = 1000
          b = 2000
          c = a if a > b else b
          
        2. 关键字参数:一一对应。

          函数:传入两个字符串参数,将两个参数拼接完成后形成的结果返回。
          def func(a,b):
              return a + b
          print(func(b='holting',a='牛皮'))
          
        3. 混合参数

          # 混合参数
          # 位置参数一定要在关键字参数的前面。
          
      • 形参角度:

        1. 位置参数:

          写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
          def func(l):
              if len(l) > 2:
                  return l[:2]
              else:
                  return l
          # print(func([1,2,3,4,5]))
          print(func([1,]))
          
          def func(l):
              c = l[:2] if len(l) > 2 else l
              return c
          print(func([1,2,3,4,5]))
          print(func([1,]))
          
          def func(l):
              return l[:2]
          # l1 = [1,]
          # print(l1[:2])
          
        2. 默认参数

          默认参数设置的意义:普遍经常使用的。

          未完待续 下一篇。。

  • 相关阅读:
    Python实现MapReduce,wordcount实例,MapReduce实现两表的Join
    structure needs cleaning
    Lifecycle of an ASP.NET MVC 5 Application
    ASP.NET Integration with IIS 7
    Execution order of modules in IIS7
    Assembly Binding redirect: How and Why?
    Cannot See Worker Processes Icon in IIS
    What is the main difference between a key, an IV and a nonce?
    核心玩法的三要素
    ruby各种循环输出数组元素
  • 原文地址:https://www.cnblogs.com/zhaoxinblog/p/13221540.html
Copyright © 2011-2022 走看看