Beginning Python Chapter 2 Notes
Python基本数据类型用Python官方说法应该叫Python内建数据类型,英文叫built-in type。下面稍微总结了一下我看到过的Python内建数据类型。
| 数据类型 | 具体的数据类型 | 说明 |
| Numbers | Integer => int | Python2.3开始数据超出int范围可以自动转到long,而之前版本Python解释器会报错 |
| Long Integer => long | Python3.x版本均将long型归入int型,即type(10**20)结果为<class 'int'> | |
| Float => float | Python储存浮点数实数只有float一种,没有double型 | |
| Complex => complex | Python提供了面向工程师和科学家的虚数类型,因而将数据类型从实数域扩展到了复数域 | |
| Boolean => bool | Python使用了True、False用于逻辑判断 | |
| Decimal => decimal | Python2.4版本将十进制浮点数作为内建类型加入Python基本数据类型,用十进制精确表示 浮点数 | |
| String | String => str | Python字符串包含了字符类型,并且可以使用切片(Slice)操作 |
| Sets | Set => set | set是无可重复集合 |
| FrozenSet => frozenset | frozenset是可重复集合 | |
|
Tuple |
Tuple => tuple | 元组是不可变的有序集 |
| List | List => list | 列表是可变的有序集 |
| Dictionaries | Dict => dict |
字典是通过键值对映射形式储存数据,类似于Java中的HashMap |
当然本文主要还是谈谈数值类型(Numbers)和运算符(Operators)。Python由于版本的更新,有些数据类型消失了,有些数据类型被创造出来了,因此本文兼顾多个版本。
1.type函数
type函数是Python内置的特殊函数,用于告诉使用者其正在使用的数据类型。格式为:>>>type(数据),输出<class '数据类型'>。

2.int/long类型
在Python2.3版本以前,int和long类型是完全独立的两个数据类型。如果int型数据超出其取值范围,不会自动向long型转换,而是报错。在Python2.3版本以后,这个问题解决了,int型数据超出int范围后会自动向long型转换。而在Python3.x版本中,long型被并入int型中。
在Python2.x版本中long型和别的编程语言中的long型不同的是:Python中的long型只受限于计算机中虚拟内存的总数,表示的数据范围远大于Java中的long型,而是类似于Java中的BigInteger。当然这个最大值只能够估计,不能够精确输出,因此在Python3.x中int最大值无法输出。
>>> type(2**100) 输出为<class 'long'>
Python2.x版本的int取值范围为-2147483648~2147483647,这个最大值可以通过sys模块中的maxint获取。

3.complex类型
Python将虚数作为内建数据类型引入,从而将原来的实数域扩充为复数域。由于复数的存在,使得Python能够更好地为工程师和科学工作人员服务,处理更为复杂的数学和物理问题。不过Python的复数与传统数学定义上的复数有区别,因为Python允许复数的实部和虚部为float类型,这和Matlab类似。当然导致这个现象的原因是因为计算机内部储存数据可以为float类型的缘故。

当然,Python还提供了复数构造函数complex(real,imag)来创建复数。

complex类型的实部和虚部是独立的。在cmath(complex math)模块中定义了很多相应处理复数的操作。

4.浮点数
对于浮点数,我只想再重提一下所有编程语言中共同的问题:舍入误差!!!
5.运算符
operand(操作数)和operator(操作符)是编程语言绕不开的话题。Python提供了丰富的操作符来进行数学计算。

值得注意的是,在Python2.x中"/"操作符和Java是相同的功能,只有在操作数中存在浮点数时结果才能使浮点数:>>> 5 / 2 结果为:2 >>> 5.0 / 2 结果为:2.5 ;不过在Python3.x中"/"操作符作用结果默认均为浮点数: >>> 5 / 2 结果为:2.5 。这是因为Python3.x引入了取整除法"//"的缘故,因此原来的除法就改成了浮点数结果: >>> 5 // 2 结果为:2 , >>> 5.2 // 2.7 结果为:1.0 。
6.Infinity(无穷大)
如果数据超出虚拟内存所能承受的最大数值,则会显示inf。
>>> 2e400 结果为:inf >>> 2e304 结果为:2e+304
7.数值格式转化
7.1)将数值格式转化为16进制

7.2)将数值格式转化为8进制

Beginning Python Chapter 2 Notes的更多相关文章
- Beginning Python Chapter 1 Notes
James Payne(American)编写的<Beginning Python>中文译作<Python入门经典>,堪称是Python的经典著作. 当然安装Python是很简 ...
- Beginning Python Chapter 3 Notes
变量(variable)是储存数据的实体,在Python中也被称为"名称"(name). 1.Python"名称"基本命名法则 1.1) "名称&qu ...
- <Web Scraping with Python>:Chapter 1 & 2
<Web Scraping with Python> Chapter 1 & 2: Your First Web Scraper & Advanced HTML Parsi ...
- Think Python - Chapter 18 - Inheritance
In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...
- Think Python - Chapter 17 - Classes and methods
17.1 Object-oriented featuresPython is an object-oriented programming language, which means that it ...
- Think Python - Chapter 16 - Classes and functions
16.1 TimeAs another example of a user-defined type, we’ll define a class called Time that records th ...
- Think Python - Chapter 12 Tuples
12.1 Tuples are immutable(元组是不可变的)A tuple is a sequence of values. The values can be any type, and t ...
- Think Python - Chapter 11 - Dictionaries
Dictionaries A dictionary is like a list, but more general. In a list, the indices have to be intege ...
- Think Python - Chapter 10 - Lists
10.1 A list is a sequenceLike a string, a list is a sequence of values. In a string, the values are ...
随机推荐
- Python list的定义和删改
需要用到list.取回参数 . sys.argv返回的是个元组. 最后发现用for循环好像没用. a=0 for i in sys.argv[1:]: qh[a]=sys.argv[a] a=a+1 ...
- C#窗体控件拖动
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 集成hibernateDaoSupport实现增删改查
1. package edu.jlu.fuliang.dao.impl; import java.util.List; import org.springframework.orm.hibernate ...
- 动态调用dll遇到的问题
问题:动态调用第三方提供的dll报错. Run-Time Check Failure #0 - The value of ESP was not properly saved across a fun ...
- uva 12452 Plants vs. Zombies HD SP (树DP)
Problem I: Plants vs. Zombies HD Super Pro Plants versus Zombies HD Super Pro is a game played not a ...
- js中push(),pop(),unshift(),shift()的用法
js中push(),pop(),unshift(),shift()的用法小结 1.push().pop()和unshift().shift() 这两组同为对数组的操作,并且会改变数组的本身的长度及 ...
- HDU - 5534 Partial Tree(每种都装的完全背包)
Partial Tree In mathematics, and more specifically in graph theory, a tree is an undirected graph in ...
- android实现简单计算器
前台代码如下 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...
- [UE4]Montage动画设置Slot
最后一张图看下,配合官网motage教程,容易理解push anim具体用法 http://aigo.iteye.com/blog/2277545 如何新建一个Montage的步骤这里省略了,网上很多 ...
- 51nod1117(简单huffman tree)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1117 题意:中文题诶- 思路:简单huffman tree ...