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 ...
随机推荐
- Java基础知识之常见关键字(1)
static 特点: 随着类的加载而加载 优先于对象存在 被所有对象所共享 可以直接被类名调用 注意点: 静态方法只能访问静态方法 但是非静态成员可以直接访问静态成员 静态方法中不可以使用this , ...
- selenium+python中,框架中,怎么返回上一个菜单
/退回上一级表单 driver.switchTo().defaultContent();
- Hibernate 的HQL和sql有什么区别
转自:https://blog.csdn.net/haozhugogo/article/details/54575802sql 面向数据库表查询 hql 面向对象查询 hql : from 后面跟的 ...
- Open Live Writer 代码插入插件测试
#-*-coding:utf-8-*- import urllib import json import sys import ssl ssl._create_default_https_contex ...
- 浏览器原生 form 表单POST 数据的两种方式
我们在提交表单的时候,form表单参数中会有一个enctype的参数.enctype指定了HTTP请求的Content-Type. 常用有两种:application/x-www-form-urlen ...
- 如何更改linux文件的拥有者及用户…
本文整理自: http://blog.163.com/yanenshun@126/blog/static/128388169201203011157308/ http://ydlmlh.iteye.c ...
- linux下gsoap的初次使用 -- c风格加法实例
摘自: http://blog.csdn.net/jinpw/article/details/3346844 https://www.cnblogs.com/dkblog/archive/2011/0 ...
- Matcher的replaceAll ()/appendReplacement()/appendTail()详细举例
直接上例子: package com.dajiangtai.djt_spider.util; import java.util.regex.Matcher;import java.util.regex ...
- LeetCode: 371 Sum of Two Integers(easy)
题目: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...
- 手机端处理布局rem
方法一 if (document.documentElement.clientWidth > 600) {//页面宽度大于600px让其宽度等于600px,字体大小等于60px,居中 docum ...