• python函数学习


    函数定义和简单调用

    公司新来部分新员工,定义函数并循环打印欢迎新员工的消息

    employees = {"Chris": "NOC", "David": "PJM", "Brain": "SA", "John": "UX"}
    
    
    def introduce_employee(employee, post):
        print("Welcome our new colleague " + username + ", his post is " + job + " !")
    
    
    for key, value in employees.items():
        username = key
        job = value
        introduce_employee(username, job)

    输出

    Welcome our new colleague Chris, his post is NOC !
    Welcome our new colleague David, his post is PJM !
    Welcome our new colleague Brain, his post is SA !
    Welcome our new colleague John, his post is UX !
    View Code

     函数使用默认值

    def print_employees(employee, post="NOC"):
        print("Welcome our new colleague " + employee + ", his post is " + post + ".")
    
    
    print_employees("David", "UX")
    print_employees("Chris", "PJM")
    print_employees("Brain")
    print_employees("John")

     输出

    Welcome our new colleague David, his post is UX.
    Welcome our new colleague Chris, his post is PJM.
    Welcome our new colleague Brain, his post is NOC.
    Welcome our new colleague John, his post is NOC.

     使用return返回值

    def employees(username, post):
        employee_info = "Welcome our new colleague " + username + ", his post is " + post + " !"
        return employee_info
    
    
    employee = employees("Chris", "NOC")
    print(employee)

    输出

    Welcome our new colleague Chris, his post is NOC !
    View Code

    借助函数和while循环打印欢迎用户信息

    def get_full_name(first_name, last_name):
        full_name = first_name + "." + last_name
        return full_name.title()
    
    
    while True:
        f_name = input("Please input your first name: ")
        if f_name == "quit":
            print("Since you input '%s' as first name, will cancel and exit..." % f_name)
            break
        l_name = input("Please input your last name: ")
        if l_name == "quit":
            print("Since you input '%s' as last name, will cancel and exit..." % l_name)
            break
        format_name = get_full_name(f_name, l_name)
        print("Hello %s." % format_name)

    输出

    Please input your first name: zhang
    Please input your last name: lei
    Hello Zhang.Lei.
    Please input your first name: tian
    Please input your last name: liang
    Hello Tian.Liang.
    Please input your first name: quit
    Since you input 'quit' as first name, will cancel and exit...
    View Code

     列表和函数循环使用

    def print_cars(cars):
        for car in cars:
            print(car.title() + " is Japan automobile brand.")
    
    
    car_list = {'toyota', 'honda', 'mazda', 'nissan'}
    print_cars(car_list)

    输出

    Mazda is Japan automobile brand.
    Toyota is Japan automobile brand.
    Honda is Japan automobile brand.
    Nissan is Japan automobile brand.
    View Code

     输入任意数量的实参

    def print_cars(*cars):
        for car in cars:
            print(car.title() + " is Japan automobile brand.")
    
    
    print_cars("toyota")
    print_cars('toyota', 'honda', 'mazda', 'nissan')

    输出

    Toyota is Japan automobile brand.
    Toyota is Japan automobile brand.
    Honda is Japan automobile brand.
    Mazda is Japan automobile brand.
    Nissan is Japan automobile brand.
    View Code

      

  • 相关阅读:
    55种网页常用小技巧(javascript) (转)
    如何利用RadioButtonList实现datagrid列的单选 (转)
    实现数据分类汇总的SQL语句 (转)
    在ASP.Net中两种利用CSS实现多界面的方法. (转)
    ASP.NET 中 Session 实现原理浅析 [1] 会话的建立流程
    用户控件中使用客户端脚本的控件名称问题 (转)
    快速理解.NET Framework[翻译] (转)挺不错的翻译
    table的宽度,单元格内换行问题 (转)
    实现类似Windows资源管理器的DataGrid(转)
    vs.net web项目使用visual source safe进行源代码管理(转)
  • 原文地址:https://www.cnblogs.com/ilifeilong/p/12038839.html
Copyright © 2020-2023  润新知