python数字即数学函数
来源:互联网 发布:淘宝扣分处罚节点 编辑:程序博客网 时间:2024/05/17 06:50
Python笔记——数字及数学函数
转载请注明出处:http://blog.csdn.net/wklken/archive/2011/04/10/6312876.aspx
一、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 常用数学函数
- python常用数学函数
- Python数学函数
- python 数学函数
- python中的数学函数
- python基础--数学函数
- Python基础--函数、懒惰即美德
- Python-14 函数:灵活即强大
- Python语言的数学函数
- python学习005--数学函数
- [leetcode] Clone Graph
- “EXC_BAD_INSTRUCTION"异常的问题!
- javascript判断checkbox是否选中
- 老罗的android视频教程
- 判断字符串最后一个字符是否为.号
- python数字即数学函数
- mongodb中的更新和插入操作
- 线段树 基础
- Cracking coding iterview(1.6)Rotate NXN Matrix
- tomcat6源码分析三(请求处理过程)
- 设计模式学习笔记之装饰者模式
- Cocos2dx static变量的声明和初始化
- HIT OJ 2255 Not Fibonacci (矩阵快速幂)
- atitit.基于http json api 接口设计 最佳实践 总结o7