python 学习——def

一、基本用法

https://www.cnblogs.com/derezzed/articles/8119592.html

def定义函数

1、没有入参的函数

1 def function():
2     print('This is a function')
3     a = 1+2
4     print(a)

输出:

1 This is a function
2 3

调用:

1 function()

 

2、有入参的函数定义

1  def func(a, b):
2     c = a+b
3     print('the c is ', c)

 

3、有默认入参的函数定义

1 def sale_car(price, color='red', brand='carmy', is_second_hand=True):
2     print('price', price,
3           'color', color,
4           'brand', brand,
5           'is_second_hand', is_second_hand,)

二、注意

https://www.cnblogs.com/zmlctt/p/4207153.html

1、可以被其他变量覆盖的

def func():
    print 1
print func() # 1
func = 5
print func # 5
pring func() # 错误!函数对象已经被覆盖了

 

2、函数在未被定义前是不能使用的

print func() # 错误!函数未定义
def func():
    prnt 1

  如果定义函数的模块被其他模块包含,可以

# mod1
def func1():
    func2()
def func2():
    print 1
     
# mod2
import mod1
mod1.func1() # 这是可以的,因为整个模块被导入,所有的定义已经执行了

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值