Python笔记--数值与运算符

根据《Python编程入门经典》(James Payne著,张春晖译)整理。

 

Python中的数值

Python提供了三种类型的可用数值:整型、浮点型和虚数。

type

使用type()函数可以确定数值的分类。

>>> type(1)
<class 'int'>
>>> type(1.0)
<class 'float'>

虚数的尾部带有字母j,虚数与非虚数结合就是一个复数

>>> type(1+1j)
<class 'complex'>

 

%

使用占位符输出。

>>> a = 10
>>> print("the number is %d" %a)
the number is 10
>>> a = 10.0
>>> print("the number is %f" %a)
the number is 10.000000

如果想要输出%,要在前面多加一个%,起到了转义的作用。

>>> print("the number is %%f %f" %a)
the number is %f 10.000000

 

+ 、-、*、 /、%

>>> 10 + 15
25
>>> 10 - 15
-5
>>> 10 * 15
150
>>> 10 / 15
0.6666666666666666
>>> 15 % 10
5

 

保留特定的小数位数

>>> print("$ %.03f" %23.309438)
$ 23.309

多个占位符

>>> print("$ %.03f  %d" %(34.08734, 12))
$ 34.087  12

 

posted @ 2021-08-23 14:25  奋斗的小仔  阅读(63)  评论(0)    收藏  举报