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

函数基础 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 为什么使用函数:减少重复代码.使程序变的可扩展使程序变得易维护 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 函数基础 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段…
修改文件(原理)--回顾 #修改文件(原理) 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 申…
函数Ⅲ(内置函数&lambda表达式) 1.函数小高级 函数可以当做变量来使用: def func(): print(123) func_list = [func, func, func] # func_list[0]() # func_list[1]() # func_list[2]() for item in func_list: v = item() print(v) 函数可以当做参数进行传递,谁调用的函数返回值就给谁. def func(arg): print(arg) func(1)…
复习 什么是函数? 具体特定功能的代码块 - 特定功能代码作为一个整体,并给该整体命名,就是函数 函数的优点 : 1.减少代码的冗余,2.结构清晰,可读性强 3.具有复用性,开发效率高,维护成本高 如何定义一个函数 : 用def关键词来声明函数   def  fn(参数列表):|  函数体 | return 函数的返回值 定义函数时,函数体不会被执行 |  函数必须先定义后使用 函数的四部分 :1. 函数名:存放着函数的地址,是调用函数的就依据 2. 函数体:解决问题的代码块 3. 参数列表:外…
<!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="…
<!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…
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…