Python学习day12-函数基础(2)】的更多相关文章

函数基础 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 为什么使用函数:减少重复代码.使程序变的可扩展使程序变得易维护 1.定义一个函数 #定义一个函数 def func(): #使用def关键字定义函数 ,接函数名,括号内可以传递参数 print("hello world") #函数体 return #返回值:函数在执行过程中只要遇到return语句,就会停止执行并返回结果,如果未在函数中指定return,那这个函数的返回值为…
函数基础 ### 函数简介 - 定义:具有特定功能的一段代码 - 优点: - 可以减少代码的重复书写 - 可以将功能的实现着和使用者分开,可以提高开发效率 - 分类: - 库函数:print.input.type等 - 自定义:用户自己封装的函数 ### 函数使用 - 函数定义格式 ```python def 函数名([参数列表]): 函数体 ``` > []表示参数是可选的,参数可以有,也可以没有,有也可以是一个或多个. - 函数名: - 像变量名一样,只要符合标识符命名规范即可. - 函数调…
函数Ⅰ(基础) 三目运算 基本结构 v =  前面  if 条件 else 后面    #条件为真v=前面,条件为假v=后面.​#等同于if 条件: v = '前面'else:    v = '后面'    #示例:# 让用户输入值,如果值是整数,则转换成整数,否则赋值为None​data = input('>>>')value =  int(data) if data.isdecimal() else None 函数基础 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段…
复习 1.字符串的比较: -- 按照从左往右比较每一个字符,通过字符对应的ascii进行比较 2. 函数的参数 : 1)实参与形参:       -- 形参:在函数定义时()中出现的参数        -- 实参:在函数调用时()中出现的参数 2)实参的分类       -- 位置实参:给位置形参,必须按位置       -- 关键字实参:给位置与关键字形参,不需要按顺序 3)形参的分类      -- 无值位置      -- 有值位置      -- 可变长位置      -- 有无值关键字…
修改文件(原理)--回顾 #修改文件(原理) with open('name','r',encoding='utf-8') as f,\ open('password','w+',encoding='utf-8') as f1: for line in f: if '开始' in line: line = line.replace('开始','千字文')#替换字符 f1.write(line) f1.seek(0) print(f1.read()) #import后续再详解 import os…
第一个python函数: >>> def func_1(): ... print 'hello python' ... >>> func_1() hello python 变量:根据作用域不同,分为局部变量和全局变量 局部变量:在函数内定义的变量,局部变量作用域在函数之内 全局变量:定义在函数之外,在函数内使用global关键字标记,全局变量作用域为整个模块:全局变量尽量少用 示例:局部变量: #!/usr/bin/env python # coding=utf-8 申…
函数Ⅳ (闭包&内置模块) 1.内置函数(补充) lambda表达式也叫匿名函数. 函数与函数之间的数据互不影响,每次运行函数都会开一个辟新的内存. item = 10 def func(): item = 2 def inner(): print(item) for item in range(10): pass inner() func() 函数销毁条件: 函数运行完毕 函数内部元素没有被其他使用 可迭代数据类型:可被for循环的类型 chr,将十进制数字转换成 unicode 编码中的对应…
<!doctype html>day12博客 figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max-width: 100%; vertical-align: middle; } button, input, select, textarea { color: inherit; font: inherit; } input[type="che…
<!doctype html>day16 - 博客 figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max-width: 100%; vertical-align: middle; } button, input, select, textarea { color: inherit; font: inherit; } input[type="…
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max-width: 100%; vertical-align: middle; } button, input, select, textarea { color: inherit; font: inherit; } input[type="checkbox"], input[type=&quo…