python数值类型_Python数值类型

python数值类型

In programming, Data Types are an essential concept. Data of various types can be stored in variables as per the task we want the variables to perform.

在编程中,数据类型是必不可少的概念。 根据我们希望变量执行的任务,各种类型的数据可以存储在变量中。

The built-in data types in Python are

Python中的内置数据类型是

  • Text Type

    文字类型

  • Numeric Type

    数值类型

  • Mapping Type

    映射类型

  • Sequence Type

    序列类型

  • Set Type

    设定类型

  • Boolean Type

    布尔型

  • Binary Type

    二进制类型

In this tutorial, we are going to learn about the various numeric types in Python with examples.

在本教程中,我们将通过示例学习Python中各种数字类型

To store numeric values, we need specific numeric data types. Python has some of the data types to define numeric values – these numeric data types can be used to store various numeric values.

要存储数值,我们需要特定的数值数据类型。 Python具有一些用于定义数值的数据类型-这些数值数据类型可用于存储各种数值。

There are 3 numeric data types in Python:

Python中有3种数字数据类型:

  1. int

    整型

  2. float

    浮动

  3. complex

    复杂

1)int (1) int)

Integer numeric type is used to store signed integers with no decimal points, like -5, 2, 78, etc.

整数数字类型用于存储无小数点的带符号整数,例如--5、2、78等。

Example:

例:

# Assigning integer values to Variables
x = 8
y = -9

# Manipulating the value of x
x = x + y

# Prints the updated value of x
print("x= ", x)

# Prints the Data Type of x
print("Data type of x: ", type(x))

Output

输出量

x=  -1
Data type of x:  <class 'int'>

2)浮动 (2) float)

Float numeric type is used to store floating-point values like 6.66, 58.9, 3.14, etc. Floats can also be in scientific notation, with E or e indicating the power of 10 (3.6e3 = 3.6x 10= 3600).

浮点数字类型用于存储浮点值,例如6.66、58.9、3.14等。浮点数也可以用科学计数法表示,E或e表示10的幂(3.6e3 = 3.6x 10 3 = 3600)。

Example:

例:

# Assigning float values to Variables
x = 8.9
y = -9.1

# Manipulating the value of x
x = x - y

# Prints the updated value of x
print("x= ", x)

# Prints the Data Type of x
print("Data type of x: ", type(x))

Output

输出量

x=  18.0
Data type of x:  <class 'float'>

3)复杂 (3) complex)

Complex numeric type is used to store complex numbers like 3.14j, 8.0 + 5.3j, 2+6j etc.

复数类型用于存储复数,例如3.14j,8.0 + 5.3j,2 + 6j等。

Format: Real + Imaginary component j

格式:实数+虚数j

Note: The imaginary component of a complex number must be denoted by j. If we denote it using i it is considered as invalid syntax in Python.

注意:复数的虚部必须由j表示。 如果使用i表示它,则在Python中被视为无效语法。

Example:

例:

# Assigning complex values to Variables
x = 1+8.5j
y = 4+9j

# Manipulating the value of x
x = x + y

# Prints the updated value of x
print("x= ", x)

# Prints the Data Type of x
print("Data type of x: ", type(x))

Output

输出量

x=  (5+17.5j)
Data type of x:  <class 'complex'>

演示所有数值数据类型的示例 (Examples demonstrating all numeric data types)

Let's look at some simple Python programs to demonstrate the numeric data types:

让我们看一些简单的Python程序来演示数字数据类型:

Note: type() is a function used to determine the type of a variable

注意: type()是用于确定变量类型的函数

Example 1: Program to print the data types of variables

示例1:打印变量数据类型的程序

# Assigning Values to Variables
a = 10
b = -1
c = 15.9
d = 6 + 8j

# Printing data type of the Variables
print("Data Type of a: ", type(a))
print("Data Type of b: ", type(b))
print("Data Type of c: ", type(c))
print("Data Type of d: ", type(d))

Output

输出量

Data Type of a:  <class 'int'>
Data Type of b:  <class 'int'>
Data Type of c:  <class 'float'>
Data Type of d:  <class 'complex'>

Example 2: Program to add complex numbers to integer and float type numbers

示例2:将复数添加到整数和浮点型数字的程序

a = 2 + 9j  # complex number
b = 2.8     # floating point number
c = 9       # integer

# addition of complex and integer
comp_plus_int = a + c
# addition of complex and float
comp_plus_float = a + b

# Printing result of addition and 
# datatype of the result
print("Sum of complex and integer values= ",comp_plus_int)
print("Data Type After addition: ", type(comp_plus_int))
print()

print("Sum of complex and float values= ", comp_plus_float)
print("Data Type After addition: ", type(comp_plus_float))

Output

输出量

Sum of complex and integer values=  (11+9j)
Data Type After addition:  <class 'complex'>

Sum of complex and float values=  (4.8+9j)
Data Type After addition:  <class 'complex'>


翻译自: https://www.includehelp.com/python/numeric-types.aspx

python数值类型

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值