Heim  >  Artikel  >  Backend-Entwicklung  >  Python中数字以及算数运算符的相关使用

Python中数字以及算数运算符的相关使用

WBOY
WBOYOriginal
2016-06-06 11:14:161253Durchsuche

Python数字
数字数据类型用于存储数值。
他们是不可改变的数据类型,这意味着改变数字数据类型会分配一个新的对象。
当你指定一个值时,Number对象就会被创建:

var1 = 1
var2 = 10


您也可以使用del语句删除一些对象引用。
del语句的语法是:

del var1[,var2[,var3[....,varN]]]]


您可以通过使用del语句删除单个或多个对象。例如:

del var
del var_a, var_b


Python支持四种不同的数值类型:

  1. int(有符号整型)
  2. long(长整型[也可以代表八进制和十六进制])
  3. float(浮点型)
  4. complex(复数)

实例
一些数值类型的实例:

长整型也可以使用小写"L",但是还是建议您使用大写"L",避免与数字"1"混淆。Python使用"L"来显示长整型。
Python还支持复数,复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型。

Python算术运算符
以下假设变量a为10,变量b为20:

以下实例演示了Python所有算术运算符的操作:

#!/usr/bin/python

a = 21
b = 10
c = 0

c = a + b
print "Line 1 - Value of c is ", c

c = a - b
print "Line 2 - Value of c is ", c 

c = a * b
print "Line 3 - Value of c is ", c 

c = a / b
print "Line 4 - Value of c is ", c 

c = a % b
print "Line 5 - Value of c is ", c

a = 2
b = 3
c = a**b 
print "Line 6 - Value of c is ", c

a = 10
b = 5
c = a//b 
print "Line 7 - Value of c is ", c

以上实例输出结果:

Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 8
Line 7 - Value of c is 2

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn