Python学习笔记函数之def语句和参数

随笔记录方便自己和同路人查阅。

#------------------------------------------------我是可耻的分割线-------------------------------------------

  如果调用print()或len()函数,你会传入一些值,放在括号之间,在这里称为“参数”。也可以自己定义接收参数的函数。

函数总会包含以下部分:

  (1)def关键字

  (2)函数名(和变量名同样的命名规则)

  (3)()圆括号

  (4)冒号

  (5)缩进的代码块

#------------------------------------------------我是可耻的分割线-------------------------------------------

  1、传入字符串,代码示例:  

#
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
#import random
def hello(name):#使用def关键字,定义一个名为hello的函数
    print('hello %s'%name)#函数功能为接收传入的名字,并在名字前面加hello打印

hello("Li Rong Yang")#函数调用

  运行结果:

  1、传入整数,代码示例:

#
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
#import random
def Square(length,width):#使用def关键字,定义一个名为hello的函数
    square = length * width#求length和width的平方
    print('Square is:%d '%square)#打印length和width的平方

Square(2,5)#函数调用

  运行结果:

猜你喜欢

转载自www.cnblogs.com/lirongyang/p/9526285.html
今日推荐