python学习笔记--8.函数的定义与使用

这是在学习Python的时候做的笔记,有些时间了,大概是按照一本挺实用的入门书籍学的,我学习编程的思路一般是掌握基础的变量类型,语法-分支结构 函数调用 类创建 结构体定义,记录一些简单的实例,剩下的就是需要用什么百度现学。

对我来说python的优势是,没有类型要求,不用声明,没有指针,万能数组,库很强大。

我这个顺序有点差,应该先说函数定义的,这是最基础的,关于如何定义函数,参数和返回值

代码

# 简单的定义函数
# region
def greet():
    print('hello')

# 参数
def greet_parameter(name):
    print('welcome '+name+'!!!')

# 带有默认值的
def describe(name, type = 'dog'):
    print('you have a ' + type + ' name ' + name)
describe('123', 'cat')
describe('lilei')  # 采用默认参数

# 用于返回值
def getfullname(firstname, secondname):
    temp = firstname + ' ' + secondname
    return temp.title()
print(getfullname('song', 'yu'))

# 可选参数 带有默认 放在参数最后
def getnamechoose(first_name, last_name, caps_look = 0):
    full_name = first_name + ' ' + last_name
    if caps_look == 0:
        return full_name.lower()
    else:
        return full_name.title()

# 传递列表参数
def printname(namelist):
    for name in namelist:
        print('welcome ' + name + '!!')
usernames = ['hannah', 'ty', 'margot']
printname(usernames)
# 禁止修改的列表
# 调用时输入列表usernames可以在函数中修改 输入usernames[:]只能修改副本

# 传入任意数量的参数
# 讲所有参数封装到元组中
def make_pizza(*toppings):
    print(toppings)
    s = ' '
    for topping in toppings:
        s = s + topping + '_'
    print(s)
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')

# 混合位置与任意关键字实参
def meg(name, password, **userinfo):
    info = {}
    info['username'] = name
    info['userpassword'] = password
    for key, value in userinfo.items():
        info[key] = value
    return info


test = meg('songyu', '123', agent = 'moliz', age = '22')
print(test)
# endregion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值