[原创]Python入门教程1. 基本运算【四则运算、变量、math模块等】


Posted in Python onOctober 28, 2018

在熟悉了Python的基本安装与环境配置之后,我们来看看Python的基本运算操作。

1. 基本运算

>>>6 # 这里的‘#'是注释符号,不参与运算
6
>>>666666666666666 #整数类型,原样输出
666666666666666
>>>3.14 #浮点数类型
3.14

>>>id(6) #id()函数用于查看内存地址
1409471616
>>>help(id) #help()函数可用于查看函数文档
Help on built-in function id in module builtins:

id(obj, /)
  Return the identity of an object.
  
  This is guaranteed to be unique among simultaneously existing objects.
  (CPython uses the object's memory address.)

>>> 5+1
6
>>>5.0+1 #这里运算结果会自动转换为浮点型
6.0
>>>10/2
5.0
>>>10/3 #这里由于计算机是将数字转换为二进制进行计算时,浮点数转换偏差造成的
3.3333333333333335
>>>2.5*2
5.0
>>>2.5**2 #符号**用指数计算,例如这里计算2.5的2次方
6.25
>>>5//2 # 符号//可用于计算相除的结果再进行取整
2
>>>5%2 #取余,没啥好说的
1
>>>5.0%2 #浮点数的取余运算,同理
1.0
>>>(5 + 6) * 2 - 2 ** 3 + 5//2 - 5 % 3 #综合计算(表达式计算)
14

2. 变量与变量类型

>>>a=6 #变量定义与赋值
>>>a
6
>>>b = 3*a #变量运算与赋值
>>>b
18
>>>type(a) #type函数用于检测变量类型
<class 'int'>
>>> b = True #布尔类型
<class 'bool'>
>>> c = 3.14 #浮点数类型
>>> type(c)
<class 'float'>
>>> d = '3water.com'
>>> type(d)
<class 'str'>
>>> e = ['a','b','c'] #列表类型
>>> type(e)
<class 'list'>
>>> f = ('x','y','z') #元组类型
>>> type(f)
<class 'tuple'>
>>> g = {'a':'1','b':'2','c':'3'} #字典类型
>>> type(g)
<class 'dict'>
>>>

3. 专业计算模块:math

sin(x) 求x的正弦
cos(x) 求x的余弦
asin(x) 求x的反正弦
acos(x) 求x的反余弦
tan(x) 求x的正切
atan(x) 求x的余切、反正切
hypot(x,y) 求直角三角形的斜边长
fmod(x,y) 求x/y的余数
ceil(x) 取不小于x的最小整数(向上取整)
floor(x) 取不大于x的最大整数(向下取整)
fabs(x) 求绝对值
exp(x) 求e的x次幂
pow(x,y) 求x的y次幂
log10(x) 求x以10为底的对数
sqrt(x) 求x的平方根
pi 圆周率π的值(常量)
>>> abs(-2) #求绝对值(系统函数)
2
>>> pow(2,4) #计算2的4次方(系统函数)
16.0
>>> round(3.4) #round四舍五入运算(系统函数)
3
>>> round(3.5) #round四舍五入运算
4
>>> import math #使用import语句可以引入math模块进行运算
>>> dir(math) #查看库中所有东西
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>> pi
Traceback (most recent call last):
 File "<pyshell#1>", line 1, in <module>
  pi
NameError: name 'pi' is not defined
>>> math.pi
3.141592653589793
>>> from math import *
>>> pi
3.141592653589793
>>>>>> sqrt(9) #sqrt计算开方
3.0
>>> ceil(3.1) #ceil向上取整
4
>>> floor(3.9) #floor向下取整
3
>>> fmod(7,4) # fmod取余数
3.0

简单入门教程~

基本一看就懂~O(∩_∩)O~

未完待续~~欢迎讨论!!

Python 相关文章推荐
解读Python中degrees()方法的使用
May 18 Python
Python中list初始化方法示例
Sep 18 Python
13个最常用的Python深度学习库介绍
Oct 28 Python
python实现员工管理系统
Jan 11 Python
基于Django与ajax之间的json传输方法
May 29 Python
查看python下OpenCV版本的方法
Aug 03 Python
在python下读取并展示raw格式的图片实例
Jan 24 Python
Python面向对象程序设计类的多态用法详解
Apr 12 Python
python暴力解压rar加密文件过程详解
Jul 05 Python
python global关键字的用法详解
Sep 05 Python
Python容器使用的5个技巧和2个误区总结
Sep 26 Python
Python使用Matlab命令过程解析
Jun 04 Python
pycharm 配置远程解释器的方法
Oct 28 #Python
解决每次打开pycharm直接进入项目的问题
Oct 28 #Python
Python OpenCV读取png图像转成jpg图像存储的方法
Oct 28 #Python
解决python opencv无法显示图片的问题
Oct 28 #Python
pycharm中成功运行图片的配置教程
Oct 28 #Python
pycharm使用matplotlib.pyplot不显示图形的解决方法
Oct 28 #Python
Pycharm无法显示动态图片的解决方法
Oct 28 #Python
You might like
模拟OICQ的实现思路和核心程序(一)
2006/10/09 PHP
php实现批量修改文件名称的方法
2016/07/23 PHP
laravel 数据验证规则详解
2019/10/23 PHP
php利用ZipArchive类操作文件的实例
2020/01/21 PHP
PHP const定义常量及global定义全局常量实例解析
2020/05/28 PHP
jQuery focus和blur事件的应用详解
2014/01/26 Javascript
jQuery学习笔记之总体架构
2014/06/03 Javascript
jquery移动节点实例
2015/01/14 Javascript
JS/Jquery判断对象为空的方法
2015/06/11 Javascript
AngularJS基础 ng-selected 指令简单示例
2016/08/03 Javascript
js关于getImageData跨域问题的解决方法
2016/10/14 Javascript
原生JS实现图片轮播切换效果
2016/12/15 Javascript
详解angular中的作用域及继承
2017/05/31 Javascript
详解Vue.js搭建路由报错 router.map is not a function
2017/06/27 Javascript
Vue-路由导航菜单栏的高亮设置方法
2018/03/17 Javascript
vue实现组件之间传值功能示例
2018/07/13 Javascript
微信小程序实现折叠展开效果
2018/07/19 Javascript
JavaScript中的连续赋值问题实例分析
2019/07/12 Javascript
原生js无缝轮播插件使用详解
2020/03/09 Javascript
使用python 获取进程pid号的方法
2014/03/10 Python
Python通过90行代码搭建一个音乐搜索工具
2015/07/29 Python
Python实现将字符串的首字母变为大写,其余都变为小写的方法
2019/06/11 Python
对python特殊函数 __call__()的使用详解
2019/07/02 Python
python软件都是免费的吗
2020/06/18 Python
详解python实现可视化的MD5、sha256哈希加密小工具
2020/09/14 Python
Django中template for如何使用方法
2021/01/31 Python
护理学毕业生求职信
2013/11/14 职场文书
酒店行政人事部经理职务说明书
2014/02/26 职场文书
迎新晚会主持词
2014/03/24 职场文书
转让协议书范本
2014/04/15 职场文书
2014年三万活动总结
2014/04/26 职场文书
大学迎新生标语
2014/10/06 职场文书
2015年平安创建工作总结
2015/04/29 职场文书
导游词之四川武侯祠
2019/10/21 职场文书
Spring Boot 启动、停止、重启、状态脚本
2021/06/26 Java/Android
Meta增速拉垮,元宇宙难当重任
2022/04/29 数码科技