Python笔记——数字及数学函数
来源:互联网 发布:学安卓要先学c语言吗 编辑:程序博客网 时间:2024/05/17 07:31
转载请注明出处:http://blog.csdn.net/wklken/archive/2011/04/10/6312876.aspx
Python笔记——数字及数学函数
一、python中的数字
1、 类型及运算符
Python中有四种类型的数
Ø 整数:一般意义上的数,包含八进制(0开头),十六进制(0x开头) eg. 2
Ø 长整数:无限大小的数,结尾添加l或L eg. 2012121200
Ø 浮点数:小数或用e/E表示的幂 eg. 3.23 50.2E2
Ø 复数:复数的虚部以字母J 或 j结尾 eg. 2+3i
运算符
+加法
-减法
*乘法
**幂次
/除法
//取整,商的整数部分
%取余
&位与
|位或
^位异或
~位翻转 x -> -(x+1)
<<左移
>>右移
运算符优先级,同级从左到右
2.基本运算及示例
- #基本运算开始
- print("3+5 = " + str(3+5) ) #不能直接+,转为string
- print("2.0-5 = " + str(2.0-5) )
- print("2 * 3 = " + str(2*3) )
- print("2 ** 3 = " + str(2**3))
- print("5 / 2 = " + str(5/2))
- print("5 // 2 = " + str(5//2))
- print("5 % 2 = " + str(5%2))
- print("2 >> 2 = " + str(2>>2))
- print("2 << 2 = " + str(2<<2))
- print("2 & 3 = " + str(2&3) )# 0010 & 0011 = 0010
- print("2 | 3 = " + str(2|3))
- print("2 ^ 3 = " + str(2^3))
- print("~2 = " + str(~2))
结果:
二、相关数学函数及使用示例
使用math模块
>>> import math
>>>dir(math) 这句可查看所有函数名列表
>>>help(math) 查看具体定义及函数原型
常用的数学函数:
ceil(x) 取顶
floor(x) 取底
fabs(x) 取绝对值
factorial (x) 阶乘
hypot(x,y) sqrt(x*x+y*y)
pow(x,y) x的y次方
sqrt(x) 开平方
log(x)
log10(x)
trunc(x) 截断取整数部分
isnan (x) 判断是否NaN(not a number)
degree (x) 弧度转角度
radians(x) 角度转弧度
另外该模块定义了两个常量:
DATA
e = 2.718281828459045
pi = 3.141592653589793
- print("-----------math functions-------------")
- #数学函数
- #取顶
- print(math.ceil(2.3))
- #取底
- print(math.floor(2.3))
- #取绝对值
- print(math.fabs(-1))
- #阶乘
- print(math.factorial(3))
- #求直角三角形斜边长
- print(math.hypot(3,4))
- #求x的y次方
- print(math.pow(2,3))
- #求x的开平方
- print(math.sqrt(4))
- #截断,只取整数部分
- print(math.trunc(2.3))
- #判断是否NaN(not a number)
- print(math.isnan(2.3333))
版权声明:本文为博主原创文章,未经博主允许不得转载。
- Python笔记——数字及数学函数
- Python笔记——数字及数学函数
- Python笔记——数字及数学函数
- 数字及数学函数
- python数字即数学函数
- python—数学函数/随机数函数/三角函数
- Python学习笔记(三)数学函数
- Python学习笔记 —— 字符串及常用函数
- Python学习笔记 —— 数字类型【Numbers】及常用方法
- Python学习笔记——数字
- [python标准库]math——数学函数
- python复习笔记[2]——数学运算
- python 笔记 数学计算与变量——12.21
- [Python入门及进阶笔记]Python-基础-数字小结
- Python学习笔记 — 函数
- R语言学习笔记2——常用数学函数
- python 数学函数
- python --数学函数记录
- 《深入理解计算机系统》学习笔记--001
- Unity基础小案例---射击小球
- websphere下log4j不起作用的问题
- Java基础---多线程
- 启动android模拟器时.有时会报The connection to adb is down, and a severe error has occur
- Python笔记——数字及数学函数
- <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_C
- UFT延长使用时间
- hive jdbc
- java架构师之路:JAVA程序员必看的15本书的电子版下载地址
- Python(七)python下的内部函数库和第三方函数库
- 遥感应用综述
- ALM需求跟踪矩阵导出出错解决
- Unique Binary Search Trees