Python学习笔记——20170823
来源:互联网 发布:虚拟机无法桥接网络 编辑:程序博客网 时间:2024/05/23 23:37
函数
- 自定义函数
# 1.定义一个函数 def get_circle_area1(r): return 3.14 * r * r # 如果没有return,返回None print(get_circle_area1(10)) # 2.错误类型报错 def get_circle_area2(r): if not isinstance(r, (int, float)): raise TypeError('数据类型错误,请输入数值类型') return 3.14 * r * r # print(get_circle_area2('s')) # 3.添加文档注释 def get_circle_area3(r): """ 获取圆的面积 :param r: 半径 :return: 圆面积 """ if not isinstance(r, (int, float)): raise TypeError('数据类型错误,请输入数值类型') return 3.14 * r * r # 函数调用 area = get_circle_area3(10) print(area)
- 导包
import math print(math.sqrt(100)) # 导包作别名 import random as rd print(rd.randint(0, 3)) # 0到3,包括3 # 自定义函数导包 from python03.MyFun import get_circle_area area = get_circle_area(10) print(area)
- 返回多个参数
import math def move(x, y, step, angle=0.0): nx = x = step * math.cos(angle) ny = y = step * math.cos(angle) return x, y # 多个参数,返回元组 num = move(100, 100, 60, math.pi / 6) print(type(num))
函数传参
- 位置参数
def power1(x): return x * x def power2(x, n): n = int(n) if n >= 1: return x ** n
- 设置默认参数 (默认参数必须写在后面)
def power(x, n=2): return x ** n
- 可变参数
def get_sum(*numbers): num_sum = 0 for i in numbers: num_sum += i return num_sum numList = [1, 2, 3, 4, 5] print(get_sum(*numList)) numTuple = (1, 2, 3, 4, 5) print(get_sum(*numTuple)) # 或者 print(get_sum(6, 7, 8, 9, 10))
- 关键字参数 可传0个或多个
def stu_info(name, age, **other): return name, age, other otherDict = {'city': '重庆', 'email': 'zhangsan@163.com'} print(stu_info('张三', 18)) print(stu_info('张三', 18, **otherDict))
- 命名关键字参数 *号后面的参数必须写名字,写名后可以无视顺序
def stu_info(name, age, *, city, email): return name, age, city, email print(stu_info('小狼', 16, email='xiaolang@163.com', city='北京')) print(stu_info(age=16, name='小狼', email='xiaolang@163.com', city='北京'))
递归函数
- 求算术平方根
def sqrt(x, a): value = (a + x / a) / 2 if abs(value - a) < 0.000001: # 控制精度 return value else: return sqrt(x, value) print(sqrt(4, 1))
- 汉诺塔
list1 = [5, 4, 3, 2, 1] list2 = [] list3 = [] def move(n, a, b, c): if n == 1: print(a, '-->', c) c.append(min((a))) a.remove(min(a)) if len(c) == 5: print(c) else: move(n - 1, a, c, b) # 将前n-1个盘子从a移动到b上 move(1, a, b, c) # 将最底下的最后一个盘子从a移动到c上 move(n - 1, b, a, c) # 将b上的n-1个盘子移动到c上 move(5, list1, list2, list3)
生成
- 列表生成式
myList = [x * x for x in range(0, 100)] print(myList) list1 = [m + n for m in 'FKY' for n in 'UCO'] print(list1)
- 列表生成式读取文件和文件夹信息
import os print([d for d in os.listdir('d:/')])
- 练习
# 取出str类型, list5 = ['Free', 'Man', 12.7, 'Case', 15, None, 'Age'] list6 = [str(x).upper() for x in list5 if isinstance(x, str)] print(list6)
- 字典、集合生成式
# 字典 myDict = {x: x*x for x in range(0, 10)} print(myDict) # 集合 mySet = {x for x in range(0, 10)} print(mySet)
- 生成器
# 生成器 myGenerator = (i * i for i in range(0, 10)) for i in myGenerator: print(i) # 转换 myTuple = tuple(myGenerator) print(myTuple) # 生成器 def numbers(): for i in range(0, 10): yield i print(type(numbers()))
附加内容
- 斐波那契数列
# 方法1 # 递归 def getFibonacci(n1, n2, times, start=1): if start >= times: return n2 else: return getFibonacci(n2, n1 + n2, times, start + 1) print(getFibonacci(1, 1, 9)) # 方法2 # 数学公式 # rea5 = math.sqrt(5) # 根号5 # x = 3 # 第几个数 # y = int(1 / rea5 * (((1 + rea5) / 2) ** x - ((1 - rea5) / 2) ** x)) import math rea5 = math.sqrt(5) list7 = [int(1 / rea5 * (((1 + rea5) / 2) ** x - ((1 - rea5) / 2) ** x)) for x in range(1, 10)] print(list7)
- 杨辉三角
print("---杨辉三角---") list1 = [1] print(list1) allList = [list1] for i in range(1, 10): rowList = [] for j in range(0, i+1): if j == 0: n = allList[i - 1][0] rowList.append(n) elif j == i: rowList.append(allList[i - 1][i - 1]) else: rowList.append(allList[i - 1][j] + allList[i - 1][j - 1]) allList.append(rowList) print(rowList)
阅读全文
0 0
- Python学习笔记——20170823
- python 学习笔记——python globals()
- Python学习笔记 — 函数
- Python学习笔记—钱珺
- python学习笔记—输入输出
- OpenCV—Python 学习笔记
- Python学习笔记(—):初识python
- 【笔记】Python学习笔记 ——1
- Python学习笔记——Unicode
- python学习笔记——文件
- Python学习笔记——描述符
- Python学习笔记——切片操作
- python学习笔记——字典
- python学习笔记——字典
- Python学习笔记——常量定义
- 学习python笔记——基本概念
- 学习python笔记——表达式
- 学习python笔记——控制流
- springBoot 多数据源事务的管理以及回滚
- Android studio 命令行运行gradle命令
- (HDU
- Listview多条目的代码
- js原生数组工具方法总结
- Python学习笔记——20170823
- code force-27C---Unordered Subsequence (模拟+暴力)
- ES6新特性
- WHQL认证中搭建了服务器和客户端,计算机池中Selection中无任何选项的解决方案
- 心跳包
- [Leetcode] 368. Largest Divisible Subset 解题报告
- Linux下安装jboss并设置自启动服务
- GalaxyOJ-792 (思维)
- CSS实现loading加载效果