jupyter notebook for python (八)

本文介绍了Python中的函数参数和默认值。函数用于可重用的代码任务,可以带有或不带参数调用。参数是传递给函数的变量。通过在函数定义中设置默认参数,即使不提供实参,函数也能使用预设值。定义函数时,应使用以字母或下划线开头的名称,并遵循特定的语法结构,包括冒号和缩进。调用函数时,只需指定函数名后跟括号。print和len等内置函数接受参数,例如len函数接收一个对象并返回其类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Functions Arguments & Parameters

Functions are used for code tasks that are intended to be reused

Python allows us to create User Defined Functions and provides many Built-in Functions such as print()

  • print() can be called using arguments (or without) and sends text to standard output, such as the console.
  • print() uses Parameters to define the variable Arguments that can be passed to the Function.
  • print() defines multiple string/numbers parameters which means we can send a long list of Arguments to print(), separated by commas.
  • print() can also be called directly with just its name and empty parentheses and it will return a blank line to standard output

 

(2) 

def some_function():

use the  def  statement when creating a function

  • use a function name that starts with a letter or underscore (usually a lower-case letter)
  • function names can contain letters, numbers or underscores
  • parenthesis   ()   follow the function name
  • a colon   :   follows the parenthesis
  • the code for the function is indented under the function definition (use 4 spaces for this course)
def some_function():
   #code the function tasks indented here    

The end of the function is denoted by returning to no indentation

 

(3)Call a function by name

 Call a simple function using the function name followed by parenthesis. For instance, calling print is
print()

 

(4) Functions that have Parameters

print() and type() are examples of built-in functions that have Parameters defined

type() has a parameter for a Python Object and sends back the type of the object

an Argument is a value given for a parameter when calling a function

  • type is called providing an Argument - in this case the string "Hello"
    type("Hello")

Defining Function Parameters

  • Parameters are defined inside of the parenthesis as part of a function def statement
  • Parameters are typically copies of objects that are available for use in function code
    def say_this(phrase):  
        print(phrase)

Function can have default Arguments

  • Default Arguments are used if no argument is supplied
  • Default arguments are assigned when creating the parameter list
    def say_this(phrase = "Hi"):  
        print(phrase)

 

翻译:argument:论点 

           intialize:初始化

           define a variable for called phrase:为被调用的短语定义一个变量

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值