zoukankan      html  css  js  c++  java
  • python 函数

    形如

    def func():
      print("hello")
      return 0   #可选 不添加return时用空行表示结束
    

    参数

    def func(v="默认值")  :           #默认值
      print(v)
    
    func()
    func(4)
    
    默认值
    4
    

    看起来是空的,但实际是有值的对象

    def func(mylist=[]):
      mylist.append("end")
      return mylist
      
    list1=func()
    print(list1)
    list2=func()
    print(list2)
    list3=func()
    print(list3)
    
    ['end']
    ['end', 'end']
    ['end', 'end', 'end']
    

    修改

    def func(mylist=None):   #None
      if mylist==None:
        mylist=[]
      mylist.append("end")
      return mylist
      
    list1=func()
    print(list1)
    list2=func()
    print(list2)
    list3=func()
    print(list3)
    
    
    ['end']
    ['end']
    ['end']
    

    end 关键字
    关键字end可以用于将结果输出到同一行,或者在输出的末尾添加不同的字符,实例如下:

    实例(Python 3.0+)
    #!/usr/bin/python3
     
    # Fibonacci series: 斐波纳契数列
    # 两个元素的总和确定了下一个数
    a, b = 0, 1
    while b < 1000:
        print(b, end=',')
        a, b = b, a+b
    执行以上程序,输出结果为:
    
    1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
    
  • 相关阅读:
    JavaScript中Null和Undefined的区别
    javascript中的计算题
    数组去重
    javascript面向对象中继承实现的几种方式
    数列求值 题解
    首字母变大写 题解
    发工资咯:) 题解
    绝对值排序 题解
    数列有序 题解
    母牛的故事 题解
  • 原文地址:https://www.cnblogs.com/lqerio/p/11155275.html
Copyright © 2011-2022 走看看