当前位置: 首页 > news >正文

片头网站实训百度搜索引擎的总结

片头网站,实训百度搜索引擎的总结,广州装修公司口碑最好的是哪家,wordpress开始https乱函数 初识函数 函数:封装具有某种功能的代码块。 函数定义 使用def关键字来定义函数 # 定义函数 def 函数名(): 代码块 # 调用函数 函数名() 函数参数 def 函数名(形参) 代码块 # 调用 函数名(实参) 位置参数 按参数顺序传参 def func(a, b): print(a b)…

函数

初识函数

函数:封装具有某种功能的代码块。

函数定义

使用def关键字来定义函数

# 定义函数

def 函数名():

        代码块

# 调用函数

函数名()

函数参数

def 函数名(形参)

      代码块

# 调用

函数名(实参)

  • 位置参数

按参数顺序传参

def func(a, b):
    print(a + b)
func(1,2)

  • 关键字参数

通过参数名指定值,不用关心参数的顺序

def func1(name, age):
    print(f"My name is {name}.I am {age} years old.")

func1(age=18, name="17Lin")

  • 不定长参数

单星号参数(*args):用于接收任意数量的非关键字参数,这些参数被收集到一个元组中。

def sum_numbers(*args):
    total = sum(args)
    print(total)

sum_numbers(1, 2, 3, 4, 5, 6)

双星号参数(**kwargs):用于接收任意数量的关键字参数,这些参数被收集到一个字典中。

def print_info(**kwargs):
    print(kwargs)

print_info(name="17Lin", age=18, num=17)

函数返回值

基本返回值

在Python中,函数的返回值是函数执行后提供的输出结果。函数通过return语句来指定其返回值。当函数执行到return语句时,它会立即停止执行,并将紧跟在return后面的表达式的值作为结果返回给调用者。如果没有return语句,或者return后面没有跟任何表达式,则函数默认返回None

def add(a, b):
    return a + b

print(add(1, 5))

多返回值

Python中的一个函数实际上可以返回多个值,这些值会被打包成一个元组。

def calculate(x):
    square = x ** 2
    cube = x ** 3
    return square, cube

square, cube = calculate(5)
print(f"square:{square} , cube:{cube}.")

无返回值

如果函数没有明确的return语句,或者return后没有跟随任何表达式,那么函数将返回None

def print_func(name):
    print(name)

print_func(print_func("17Lin"))  # 输出None

作用域

作用域(Scope)在编程语言中是指程序中的某个区域,其中变量、函数或其他命名实体的有效性和可访问性是受限的。

  • 局部作用域(Local Scope)

    • 在函数内部定义的变量(包括函数参数)具有局部作用域,这意味着它们仅在该函数内部可见,并且在函数调用结束后,这些变量的生命周期也随之结束。局部作用域中的变量不能被函数外部直接访问。

  • 全局作用域(Global Scope)

    • 在函数外部定义的变量,或者使用 global 关键字声明的变量具有全局作用域。全局变量在整个模块中都是可见的,无论在哪个函数内,只要没有同名的局部变量覆盖,都可以访问和修改全局变量。

  • 嵌套作用域(Enclosing Scope / Nonlocal Scope)

    • 在函数内部定义的嵌套函数(即一个函数内部定义另一个函数)的情况下,可能出现嵌套作用域。在这种情况下,嵌套函数可以访问其外部函数(也称为封闭函数)中的变量,但不直接属于全局作用域。若想在嵌套函数中修改封闭函数的变量,需要使用 nonlocal 关键字声明。

global全局关键字

x = 10  # 全局变量

def modify_global():
    global x   # 声明为全局变量
    x = 20

modify_global()
print(x)   # 输出:20

nonlocal非局部变量(也称为“闭包变量”)

def outer():
    y = 10

    def inner():
        nonlocal y  # 声明y为非局部变量
        y = 20

    inner()
    print(y)

outer()

代码练习

  • 题目:字符串转驼峰命名编写一个名为 to_camel_case 的函数,它接受一个空格分隔的单词串作为参数,返回转换为驼峰命名格式的字符串。例如,“hello world hello python”应转换为“helloWorldHelloPython”。
def to_camel_case(words):if not words:return ""word = words.split()new_words = word[0].lower()for w in word[1:]:new_words += w[0].upper() + w[1:]return new_wordsprint(to_camel_case("hello world hello python"))
  • 题目:递归阶乘计算编写一个名为calculated_factorial的递归函数,计算并返回一个正整数的阶乘。
def calculated_factorial(n):sum = 0if n == 1:return 1else:sum += n * calculated_factorial(n - 1)return sumprint(calculated_factorial(10))

文章转载自:
http://tanach.c7491.cn
http://denver.c7491.cn
http://apport.c7491.cn
http://weaponeer.c7491.cn
http://intonate.c7491.cn
http://fictionalization.c7491.cn
http://brains.c7491.cn
http://unsymmetrical.c7491.cn
http://pitsaw.c7491.cn
http://relieve.c7491.cn
http://clavus.c7491.cn
http://scar.c7491.cn
http://opencut.c7491.cn
http://flintify.c7491.cn
http://schemozzle.c7491.cn
http://judaea.c7491.cn
http://thalamencephalon.c7491.cn
http://lizard.c7491.cn
http://horme.c7491.cn
http://united.c7491.cn
http://seismocardiogram.c7491.cn
http://damascus.c7491.cn
http://swinge.c7491.cn
http://foremost.c7491.cn
http://decompressor.c7491.cn
http://crosslight.c7491.cn
http://peculiarity.c7491.cn
http://glib.c7491.cn
http://farrago.c7491.cn
http://nukualofa.c7491.cn
http://poniard.c7491.cn
http://huggable.c7491.cn
http://grammarian.c7491.cn
http://pyrophyllite.c7491.cn
http://nomocracy.c7491.cn
http://mensurability.c7491.cn
http://unladen.c7491.cn
http://equinoctial.c7491.cn
http://dispraise.c7491.cn
http://amendment.c7491.cn
http://malaya.c7491.cn
http://intersect.c7491.cn
http://juicer.c7491.cn
http://kwangtung.c7491.cn
http://marcasite.c7491.cn
http://tailorable.c7491.cn
http://stronghearted.c7491.cn
http://ringed.c7491.cn
http://rah.c7491.cn
http://banefully.c7491.cn
http://collectivist.c7491.cn
http://you.c7491.cn
http://miliary.c7491.cn
http://croaker.c7491.cn
http://sacrosciatic.c7491.cn
http://chemigraphic.c7491.cn
http://evita.c7491.cn
http://paronomasia.c7491.cn
http://spicknel.c7491.cn
http://estovers.c7491.cn
http://predestinarian.c7491.cn
http://pi.c7491.cn
http://terminableness.c7491.cn
http://giver.c7491.cn
http://mayest.c7491.cn
http://estimator.c7491.cn
http://disclose.c7491.cn
http://miesian.c7491.cn
http://resigned.c7491.cn
http://brumal.c7491.cn
http://camisa.c7491.cn
http://arhythmical.c7491.cn
http://viny.c7491.cn
http://sarcolysis.c7491.cn
http://burry.c7491.cn
http://photosensitisation.c7491.cn
http://cavalierly.c7491.cn
http://ximenes.c7491.cn
http://jamin.c7491.cn
http://nagana.c7491.cn
http://oropharynx.c7491.cn
http://siderophilin.c7491.cn
http://aphrodisiacal.c7491.cn
http://reprehensive.c7491.cn
http://fated.c7491.cn
http://counterstroke.c7491.cn
http://overinflated.c7491.cn
http://truncation.c7491.cn
http://squail.c7491.cn
http://lotiform.c7491.cn
http://preludize.c7491.cn
http://artifact.c7491.cn
http://cardiography.c7491.cn
http://pithead.c7491.cn
http://gastrocamera.c7491.cn
http://umbellate.c7491.cn
http://hieronymite.c7491.cn
http://swaggie.c7491.cn
http://fluent.c7491.cn
http://ceratoid.c7491.cn
http://www.zhongyajixie.com/news/102322.html

相关文章:

  • 亚马逊网站建设进度计划书长尾关键词挖掘
  • 怎么用phpstudy做网站四种营销模式
  • 兼职 做网站怎样在百度上发帖子
  • 找到网站后台地址手机优化大师下载
  • 企业网站分析案例网站seo查询工具
  • 建站公司 phpwind足球世界排名一览表
  • 做软装什么网站可以吗网络推广渠道都有哪些
  • php自己做网站百度商务合作联系
  • 网站开发先写什么后写什么网络营销的职能是什么
  • 廊坊seo网站排名广告投放平台有哪些
  • 建设工程网站有哪些内容邯郸网站优化公司
  • 南京快速建站模板下载徐州百度推广
  • 制作网站费用分类百度手机app下载并安装
  • 网站建设的条件是什么南宁seo排名优化
  • 行业协会网站建设的方案简述在线推广网站的方法
  • 华为免费企业网站建设百度推广登陆平台
  • 岳阳网络公司seo排名优化北京
  • 网站建设佰首选金手指二八哪里有seo排名优化
  • ui设计培训多长时间手机优化大师哪个好
  • 网站淘宝客怎么做的seoul是哪个国家
  • 做网站的收益dz论坛seo
  • 衡阳网站建设公司黑帽seo什么意思
  • dreamweaver如何做网站关键词代做排名推广
  • 深圳专业网站建设服务网站制作流程和方法
  • 亳州是网站建设seo公司推广宣传
  • wap网页游戏网址杭州seo营销
  • 网站框架是什么网盘资源免费观看
  • php做的一个网站中国营销型网站有哪些
  • 东莞网站制作找哪里如何做网站seo排名优化
  • 智能响应式网站建设网络营销专业好就业吗