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数值类型

python数值类型_Python数值类型相关推荐

  1. python 异常分类_Python异常类型

    python标准异常 异常名称描述 BaseException所有异常的基类 SystemExit解释器请求退出 KeyboardInterrupt用户中断执行(通常是输入^C) Exception常 ...

  2. python指定变量类型_Python 变量类型详解

    变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据类型,这些变量可以存储整 ...

  3. python小数乘法_python小数类型

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 在python中,数据采用了对象的形式(无论是python内置对象还是使用pyt ...

  4. 4j是合法python数字类型_python数字类型

    在python中,数据采用了对象的形式(无论是python内置对象还是使用python工具和像C语言自行创建的对象). Python数字类型工具:整数和浮点数 复数 固定精度的十进制数 有理分数 集合 ...

  5. python 类 字典_python基础类型—字典

    字典 字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必 ...

  6. python的元祖类型_python元组类型

    元组类型简介 使用括号包围的数据结构是元组(tuple).例如: >>> (1,2,3) (1, 2, 3) >>> T = (1,2,3,) >>&g ...

  7. python序列类型包括哪三种映射类型_Python序列类型包括字符串、列表和元组三种,列表是Python中唯一的映射类型...

    Python序列类型包括字符串.列表和元组三种,列表是Python中唯一的映射类型 更多相关问题 [填空题] 秦以后的地方基本建制为().但西汉因分封形成了(). [单选] 药物过敏性口炎的临床特征为 ...

  8. python字符型_python字符类型

    1.整型(Int) Int,整数,范围为 -2 ** 31 到 2 ** 31 - 1 ,超出这个范围便是长整型,有2进制,8进制,10进制,16进制.用8进制表示整数时,前面要加'0'的前缀,16进 ...

  9. python合法变量类型_Python 变量类型

    变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据类型,这些变量可以存储整 ...

最新文章

  1. C++ primer 第三章笔记
  2. 除了海思麒麟 华为在ARM芯片上还有哪些成就?
  3. 谷歌和Facebook正在吸走欧洲的人才
  4. Basis,去中心化央行?
  5. HDU - 4856 Tunnels(哈密顿路径+状压dp)
  6. Java微服务篇1——SpringBoot
  7. yii2得到的数据对象转化成数组
  8. 一起来看小米发布会!
  9. 候选公示!高工智能汽车金球奖第二批入围年度产品/方案亮相
  10. 米思齐Mixly图形化编程---数管码时钟
  11. XTU 1205 Range
  12. window计算机截屏快捷键,电脑截图是ctrl加什么键win7快捷键截图方法详解
  13. 为博聆网用户编写的userscript
  14. NetBeans的下载、安装
  15. Sandboxie免费开源沙箱软件下载与详细使用教程
  16. 【xinput1_3.dll下载】xinput1_3.dll丢失怎么修复win10
  17. DP(Nietzsche)的hu测 T1(状压dp)
  18. 【LeetCode】一年中的第几天
  19. 笔记本电脑黑屏后如何重装系统,解决笔记本电脑黑屏
  20. jQuery中index的用法

热门文章

  1. Codeforces Round #326 (Div. 2) B. Pasha and Phone C. Duff and Weight Lifting
  2. MBR、DBR、FAT32基础小知识
  3. max std value 宏_Rust Macro/宏 新手指南
  4. ssh免密登录配置方法及配置
  5. [分布式训练] 单机多卡的正确打开方式:PyTorch
  6. autojs 云控_autojs websocket 核心示例代码,云控技术
  7. mysql临时表 清空_在数据库中临时表什么时候会被清除呢
  8. sql同时操作两列_怎么在两列同时筛选数据库
  9. 重新学习Ubuntu -- 截图软件的选择和安装
  10. BlockingQueue详解