zoukankan      html  css  js  c++  java
  • python之再学习----简单的函数(1)

    # filename:python2.26(2).py
    # author:super
    # date:2018-2-26

    print('today to learn the function')


    def hello(username):
    print('hello, world '+username)


    def pet(animal1, animal2='pig'):
    print('first one is '+animal1)
    print('second one is '+animal2)


    def get_name(fisrt_name, last_name):
    full_name = fisrt_name + ' ' + last_name
    return full_name


    def get_formatted_name(first_name, last_name, middle_name=''):
    if middle_name:
    return first_name + middle_name + last_name
    else:
    return first_name + last_name


    def build_person(first_name, last_name):
    person = {'first': first_name, 'last': last_name}
    return person


    hello('super')
    # 位置实参, 顺序很重要
    pet('cat', 'dog')
    # 关键字实参, 就不需要考虑顺序
    pet(animal2='cat', animal1='dog')
    # 当函数有默认值的时候, 那个参数可以不用传进去
    pet('cat', 'piggggg')
    # 函数有返回值的时候, 要有个变量去接这个值
    name = get_name('su', 'super')
    print(name)
    # 让这个参数可用可不用的时候,可以让[参数]=''
    name2 = get_formatted_name('su', 'super', 'good')
    print(name2)
    name3 = get_formatted_name('su', 'super')
    print(name3)
    # 函数可以返回一个复杂的数据结构, 如字典列表等
    name4 = build_person('su', 'super')
    print(name4)

    # practice


    def make_ablum(song_name, songer, count=''):
    song = {'song_name': song_name, 'songer': songer}
    if count:
    song['count'] = count
    return song


    song1 = make_ablum('song1', 'singer1')
    print(song1)
    song2 = make_ablum('song2', 'singer2', 5)
    print(song2)

  • 相关阅读:
    AOJ 0189 Convenient Location (Floyd)
    AOJ 2170 Marked Ancestor[并查集][离线]
    POJ 1703 Find them, Catch them (并查集)
    复制构造函数 与 赋值函数 的区别(转)
    POJ 2236 Wireless Network (并查集)
    POJ 2010 Moo University
    POJ 3614 Sunscreen (优先队列)
    HTML页面下雪特效
    基于Jquery的XML解析器,返回定制的HTML
    HTML,CSS 无边框桌面窗口
  • 原文地址:https://www.cnblogs.com/superblog/p/8476927.html
Copyright © 2011-2022 走看看