GVKun编程网logo

Python 教程(四) —— 数字(python的数字)

4

本文将介绍Python教程(四)——数字的详细情况,特别是关于python的数字的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于CMake不断从cy

本文将介绍Python 教程(四) —— 数字的详细情况,特别是关于python的数字的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取、Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod、Error: Can‘t find Python executable “python“, you can set the PYTHON env variable、ERROR: Command "python setup.py egg_info" python-nss的知识。

本文目录一览:

Python 教程(四) —— 数字(python的数字)

Python 教程(四) —— 数字(python的数字)

Python 教程(四) —— 数字

Python 数字分为:int(整型),float 和 Decimal(浮点型),Fractions(分数)以及 Complex(复数)

文章目录

  • Python 教程(四) —— 数字
  • 一、整数
  • 二、浮点数
  • 三、复数
  • 四、分数
  • 五、Decimal 十进制浮点运算(对象不可变)
    • 1. 自定义位数
    • 2. 其他数字转换为Decimal计算
      • (一)整数及字符串转换
      • (二)浮点数转换
      • (三)元组转换


一、整数

Int 或整数是完整的数字,正数或负数,没有小数,长度不限。

a = 12
b = -56
c = 6484613213548489

print(type(a))
print(type(b))
print(type(c))

输出:
>>> <class 'int'>
>>> <class 'int'>
>>> <class 'int'>

二、浮点数

“浮点数”是包含小数的正数或负数。同时浮点数也可以是带有“e”的科学数字,表示 10 的幂。

a = 12.63
b = -56.56
c = 6484613213548489.549849489

# 科学数字
d = 894e2

print(type(a))
print(type(b))
print(type(c))
print(type(d))

输出:
>>> <class 'float'>
>>> <class 'float'>
>>> <class 'float'>
>>> <class 'float'>

三、复数

复数用 “j” 作为虚部编写:

a = 5+6j
b = 6j
c = -6j

print(type(a))
print(type(b))
print(type(c))

输出:
<class 'complex'>
<class 'complex'>
<class 'complex'>

四、分数

Python支持分数的计算:

from fractions import Fraction

print(Fraction(5, 6))
print(Fraction(5, -6))
print(Fraction(10, 8))

print(Fraction(10, 8) + 1.2)
print(Fraction(10, 8) * 2)
print(Fraction(10, 8) / 2)


输出:
5/6
-5/6
5/4
2.45
5/2
5/8

五、Decimal 十进制浮点运算(对象不可变)

1. 自定义位数

相比较 float 浮点数的计算,Decimal 更加趋向于现实数字的计算,更加精确,通常用于对于精度要求比较高的金融以及科学计算上。

而且 Decimal 具有有效位的概念,能自由决定保留位数,可以通过下面的例子来看:可以看到,decimal 可以通过 quantize 方法来限定位数。

from decimal import Decimal

a = Decimal(5.698).quantize(Decimal('.1'), rounding='ROUND_DOWN')
b = Decimal(6.23).quantize(Decimal('0.01'), rounding='ROUND_UP')

print(a)
print(b)

输出:
5.6
6.24

2. 其他数字转换为Decimal计算

(一)整数及字符串转换

普通的整数以及字符串转换直接赋值到 Decimal 构造就行。当然,整数及字符串都转换成 Decimal 以后可以直接进行数学运算.

from decimal import Decimal

a = Decimal(2)
b = Decimal('5')
c = Decimal('5.26')
e = Decimal(2 + 5)

print(a)
print(b)
print(c)
print(e)

# 数字运算
print(a + c)


输出:
2
5
5.26
7

7.26

(二)浮点数转换

浮点数的转换因为二进制转换为十进制,所以需要53位或更多位数来填充精度。在转换的时候如果有精度要求,可以直接用 quantize 方法预设精度。

from decimal import Decimal

print(Decimal(1.2))
print(Decimal(2.5))
print(Decimal.from_float(3.6))
print(Decimal(3.6).quantize(Decimal('0.001'), rounding='ROUND_DOWN'))

输出:
1.1999999999999999555910790149937383830547332763671875
2.5
3.600000000000000088817841970012523233890533447265625
3.600

(三)元组转换

元组转换成 Decimal 需要特定的格式来进行:(符号, (tuple), 精度)

名词符号解释
符号010 表示正数,1 表示负数
tuple(1, 2, 5, 7)中间 tuple 部分表示要拼接的内容
精度正整数或负整数元组最后一位开始精确到第几位(正整数是小数点向后计算n位,
负整数表示小数点向前计算n位)
from decimal import Decimal

print(Decimal((1, (1, 5, 2), 0)))
print(Decimal((1, (1, 5, 2), 1)))
print(Decimal((0, (1, 5, 2), 1)))
print(Decimal((0, (1, 5, 2, 4), -2)))

输出:
-152
-1520
1520
15.24

CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取

CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取

如何解决CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取

我有一个看起来像这样的 CMake 脚本:

  1. find_program(PYTHON_COMMAND NAMES python3 python)

问题是它检测到安装在 Cygwin 安装中的 python。 输出总是:

  1. -- PYTHON_PATH:C:/cygwin64/bin/python3

我希望它取自:

  1. c:\\python36-64\\python

在windows PATH变量中,Cygwin bin在路径的最后一个,windows安装在第一个 但它只检测到 Cygwin python,
怎么改?

Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod

Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod

It's important to understand the Python execution model and precisely when function deFinitions and other important events occur when a module is imported or executed. Here, we show execution of our Python module as it's imported in a graphical debugging environment. We step through the top‑level statements in the module. What's important to realize here is that the def used for the fetch_words function isn't merely a declaration. It's actually a statement, which when executed in sequence with the other top‑level model scope code, causes the code within the function to be bound to the name of the function. When modules are imported or run, all of the top‑level statements are run, and this is the means by which the function within the module namespace are defined. We are sometimes asked about the difference between Python modules, Python scripts, and Python programs. Any .py file constitutes a Python module. But as we've seen, modules can be written for convenient import, convenient execution, or using the if dunder name = dunder main idiom, both. We strongly recommend making even simple scripts importable since it eases development and testing so much if you can access your code from within the REPL. Likewise, even modules, which are only ever meant to be imported in production settings, benefit from having executable test code. For this reason, nearly all modules we create have this form of defining one or more importable functions with a postscript to facilitate execution. Whether you consider a module to be a Python script or Python program is a matter of context and usage. It's certainly wrong to consider Python to be merely a scripting tool, in the vein of Windows batch files or UNIX Shell scripts, as many large and complex applications are built exclusively with python.

 

  • def不仅仅是一个declaration声明,更是一条statement语句。它将其中的python代码于函数名绑定在一起
  • 一个py文件就是一个模块,这个模块包含类或函数。你写python,要尽量将代码包装成函数和类,方便各种import
  • 一个py文件也可看作是一个脚本,在系统命令行中运行
  • python不仅仅是脚本语言,很多大型程序都是用python构建的

Error: Can‘t find Python executable “python“, you can set the PYTHON env variable

Error: Can‘t find Python executable “python“, you can set the PYTHON env variable

在启动vue项目的时候,安装node.js组件node-sass过程中报错了,错误提示如下
Error: Can’t find Python executable “python”, you can set the PYTHON env variable

由错误提示可知:Node.js 在安装模块组件node-sass的时候,node.js缺少Visual Studio2015 Build Tools相关的组件和python的环境,如果安装了vs2015组件的小伙伴们就不用安装Visual Studio2015 Build Tools相应的组件,只用安装python2.7即可解决缺少的python组件的问题。

欲安装python2.7,请至python官网:www.python.org 下载,然后配置好python的环境变量即可。

不过博主我并不推荐上述的解决方案,因为对于程序员来说,效率第一,上述的问题一个命令就可以轻松解决你所遇到的麻烦,前面说了那么多,无非就是想告诉在看本篇博客的同仁们放下浮躁的心,遇到问题首先不是急着去解决问题,而是分析为什么会这样,然后才能水到聚成的去找到解决问题的方法。

运行下面这个命令即可解决你们遇到的Error问题

npm install --global --production windows-build-tools

:上面讲述了一堆就是为了讲述此命令是干嘛的,上面已经描述很详细了,就不再赘述了,该操作与上述的一堆操作无异,效果却是一样的。

然后运气不好的小伙伴可能接着会遇到一个坑,那就是执行了:npm install --global --production windows-build-tools这个命令的人细心点会发现执行到一半就卡住了,这个卡住了没有红字重点提示,而且下方还有英文在等待中,粗心的小伙伴可能以为是命令执行完了,组件安装好了,其实不然,我这边已经解决了,就无法复现了,具体点就是中文的提示,提示我们由于有类似组件在运行或者下载导致无法继续下载安装组件了。稳妥点的解决办法是,将电脑重启,将底层正在运行的模块干掉,待电脑重启后再执行npm install --global --production windows-build-tools这条命令即可,博主我就是这样解决的,稳稳的幸福就会浮现在你面前如下图所示,你的可能和我不一样,因为我已经跑成功过一次了,没有你的那么多细节的log打印。
在这里插入图片描述

然后就是在你的项目下shift+鼠标右击你的项目运行npm run dev即可启动vue项目了。

ERROR: Command

ERROR: Command "python setup.py egg_info" python-nss

[root@localhost ~]# pip install python-nss

DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won''t be maintained after that date. A future version of pip will drop support for Python 2.7.
Looking in indexes: http://pypi.douban.com/simple
Collecting python-nss
  Downloading http://pypi.doubanio.com/packages/6b/29/629098e34951c358b1f04f13a70b3590eb0cf2df817d945bd05c4169d71b/python-nss-1.0.1.tar.bz2 (222kB)
     |████████████████████████████████| 225kB 31kB/s 
    ERROR: Complete output from command python setup.py egg_info:
    ERROR: Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-JGnrT5/python-nss/setup.py", line 409, in <module>
        sys.exit(main(sys.argv))
      File "/tmp/pip-install-JGnrT5/python-nss/setup.py", line 333, in main
        nss_include_dir  = find_include_dir([''nss3'', ''nss''],   [''nss.h'',  ''pk11pub.h''], include_roots=include_roots)
      File "/tmp/pip-install-JGnrT5/python-nss/setup.py", line 94, in find_include_dir
        raise ValueError("unable to locate include directory containing header files %s" % include_files)

    ValueError: unable to locate include directory containing header files [''nss.h'', ''pk11pub.h'']

ERROR: Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-JGnrT5/python-nss/


查看错误日志缺少头文件

进入python-nss官网,写着To build python-nss you the C language header files and libraries for both NSPR and NSS will need to be installed. This is system and distribution specific, as such we cannot give you explicit instructions. On Linux typically these packages are called:

  • nss-devel
  • nspr-devel

yum install nss-devel -y

yum install nspr-devel -y

今天关于Python 教程(四) —— 数字python的数字的分享就到这里,希望大家有所收获,若想了解更多关于CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取、Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod、Error: Can‘t find Python executable “python“, you can set the PYTHON env variable、ERROR: Command "python setup.py egg_info" python-nss等相关知识,可以在本站进行查询。

本文标签: