python数字运算规则_Python数字

python数字运算规则

Today we will explore about Python Numbers. Previously we learnt about Python package. If you want you can find it here.

今天,我们将探讨Python数字。 先前我们了解了Python包。 如果需要,可以在这里找到。

Python数字 (Python Numbers)

Data types that store numeric values are called Number. If you change the value of a number data type, this results in a newly allocated object. So you can call numbers immutable.

存储数值的数据类型称为数字。 如果更改数字数据类型的值,则会导致新分配一个对象。 因此,您可以拨打不可变的号码。

We can simply create a number object by assigning some value to a variable. Also we can manipulate them as shown in below example.

我们可以通过为变量分配一些值来简单地创建数字对象。 我们也可以如下例所示操作它们。

#create two number objects
var1=2
var2=3

# to print them
print(var1)
print(var2)

#to delete one of them
del var1

不同数值类型 (Different Numerical Type)

Python supports four different data types.

Python支持四种不同的数据类型。

  1. int – signed integers. Can hold both positive and negative value

    int –有符号整数。 可以同时容纳正值和负值
  2. long – long integers. Similar like int. But range of this data type is unlimited

    long –长整数。 类似int。 但是此数据类型的范围是无限的
  3. float– floating point real values. Holds real numbers.

    float浮点实数值。 保留实数。
  4. complex– values of the form r+ij. Holds complex numerical values.

    complex –形式为r + ij的值。 包含复数值。

Python数字–类型转换 (Python Numbers – Type Conversion)

We can convert data types from one to another. This often is called type casting also. Here are some example.

我们可以将数据类型从一种转换为另一种。 这通常也称为类型转换。 这是一些例子。

  1. int(x) – converts x to a plain integer.

    int(x) –将x转换为纯整数。
  2. long(x) – converts x to a long integer.

    long(x) –将x转换为长整数。
  3. float(x) – converts x to a floating point number.

    float(x) –将x转换为浮点数。
  4. complex(x) – converts x to a complex number with real part x and imaginary part zero.

    complex(x) –将x转换为具有实部x和虚部为零的复数。
  5. complex(x,y) – converts x to a complex number with real part x and imaginary part y.

    complex(x,y) –将x转换为具有实部x和虚部y的复数。

Here are some example of type casting.

这是类型转换的一些示例。

确定类型 (Determining type)

We can also determine what type of numeric value one variable holds.

我们还可以确定一个变量具有哪种类型的数值。

a = 25

# Output: 
print(type(a))

# Output: 
b=25.0
print(type(b))

# Output: True
print(isinstance(b, float))

The above code, if we run it will produce following output.

上面的代码,如果我们运行它,将产生以下输出。

带前缀的Python数字 (Python Numbers with Prefix)

In our day to day life we deal with decimal numbers(base 10). But in computer programs we may need to deal with numbers of other bases too like binary numbers(base 2), hexadecimal numbers(base 16), octal numbers(base 8) etc. We can represent these numbers with placing a prefix before the number like below-

在我们的日常生活中,我们处理十进制数字(以10为基数)。 但是在计算机程序中,我们可能还需要处理其他基数,例如二进制数(基数2),十六进制数(基数16),八进制数(基数8)等。我们可以在数字前加上前缀来表示这些数字像下面

# Output: 10
print(0b1010)

# Output: 15
print(0xF)

# Output: 13
print(0o15)

This code will produce output like below.

此代码将产生如下输出。

有关复数的更多信息 (More on Complex numbers)

There are some built-in accessors and functions to support complex number in python. Take a look at the following code for better understanding.

有一些内置的访问器和函数来支持python中的复数。 请看以下代码,以更好地理解。

# different complex numbers and their real and imaginary part
complex1 = (1,2)
print(complex1)

complex2=(2,-3)
print(complex2)

complex3= 3+4j
print(complex3)

complex4=2+3j
print(complex4)

#some built-in accessors

print(complex4.real) # gives the real part of a complex number

print(complex4.imag) #gives the imaginary part of a imaginary number

print(complex4.conjugate()) # gives the complex conjugate of a complex number

#some built-in functions for complex numbers
print(abs(complex3)) #gives the magnitude of a complex number

print(pow(complex3,2)) #raise a complex number to a power

The above code will produce the following output-

上面的代码将产生以下输出-

So that’s all for a quick roundup on python numbers. Make sure you run every piece of code on your own. Also it is better practice to explore things and think out of the box. If you have any doubt feel free to leave a comment.
#happy_coding 🙂

这样就可以快速汇总python数字。 确保自己运行每段代码。 另外,探索事物和跳出思路思考是一种更好的做法。 如果您有任何疑问,请随时发表评论。
#happy_coding🙂

翻译自: https://www.journaldev.com/14347/python-numbers

python数字运算规则

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值