• python学习7——函数


    1、函数。

    def profit(volume, price,cost):
        print("We sell %d ice cream a day." % volume)
        print("You can buy an ice cream at the price of %d dollars" % price)
        print("Make an ice cream will cost us %d dollars." % cost)
        print("Then our daily profit can be %d dollars." %((price-cost)*volume))
    print("We can just give the function numbers directly:" )
    profit(500,20,10)
    print("Or we can use variables from our script:")
    volume = 200
    price  =10
    cost  = 3
    profit(volume,price,cost)

    输出结果:

     2、函数和文件。

    from sys import argv
    script, input_file = argv
    def print_all(f):
      print(f.read())
    def rewind(f):
      f.seek(0)
    def print_a_line(line_count,f):
      print(line_count,f.readline())
    current_file = open(input_file)
    print("First let's print the whole file:
    ")
    print_all(current_file)
    print("Now let's rewind, kind of like a tape).")
    rewind(current_file)
    print("Let's print three lines:")
    current_line = 1
    print_a_line(current_line,current_file)
    current_line = current_line+1
    print_a_line(current_line,current_file)
    current_line = current_line+1
    print_a_line(current_line,current_file)

    输出结果:

    为了知道+=的作用,我将代码修改为:

    from sys import argv
    script, input_file = argv
    def print_all(f):
      print(f.read())
    def rewind(f):
      f.seek(2)
    def print_a_line(line_count,f):
      print(line_count,f.readline())
    current_file = open(input_file)
    print("First let's print the whole file:
    ")
    print_all(current_file)
    print("Now let's rewind, kind of like a tape.")
    rewind(current_file)
    print("Let's print three lines:")
    current_line = 1
    print_a_line(current_line,current_file)
    current_line+= current_line+1
    print_a_line(current_line,current_file)

    输出结果:

    可以看到,第二行的内容没有被打印出来。

    结论:

    a+=b 即为 a=a+b

    3、return

    def add(a,b):
     print("ADDING %d + %d" % (a,b))
     return a+b
    def subtract(a,b):
     print("SUBTRACTING %d-%d" % (a,b))
     return (a-b)
    def multiply(a,b):
     print ("MULTIPLYING %d * %d"%(a,b))
     return (a*b)
    def divide(a,b):
     print("DIVIDING %d / %d" %(a,b))
     return (a/b)
    print("Let's do some math with just functions!")
    age = add(30,5)
    height = subtract(70,4)
    weight=multiply(90,2)
    iq = divide(150,2)
    print("Age: %d, Height: %d, Weight: %d, IQ: %d" %(age,height,weight,iq))
    print("Here is a puzzle.")
    what = add(age, subtract(height,multiply(weight, divide(iq,2))))
    print("That becomes:",what,"
     Can you do it by hand?")

    输出结果:

  • 相关阅读:
    python自学第13天 hashlib,re模块
    python自学第12天 模块
    python自学第12天 模块定义,导入,内置模块
    python自学第11天-单线程并发、迭代器,序列化,获取路径
    python自学第10天,生成器
    python自学第9天,装饰器
    python自学第8天,变量,递归
    python自学第7天,函数,参数
    彻底搞懂Session与Cookie的异同!
    你真的搞懂了Java中的<<、>>、>>>运算符嘛?
  • 原文地址:https://www.cnblogs.com/shannon-V/p/9529490.html
Copyright © 2020-2023  润新知