Python基本数据类型之int
一、int的范围
2.7:
32位:-231~231-1 64位:-263~263-1
3.5:
在3.5中init长度理论上是无限的
二、python内存机制
在一般情况下当变量被赋值后,内存和变量的关系如下:
#方式一
n1 = 123
n2 = 123

#方式二
n1 = 123
n2 = n1

python内的优化机制(不论是2.7还是3.5都有):
在-5~257之间的数,如果使用第一种赋值方式,那么他们依然属于同一块内存
print(id(n1)) #查看变量的内存地址
二、进制转换
bin() #把变量转换为2进制
oct() #把变量转换为8进制
int() #把变量转换为10进制
hex() #把变量转换为16进制
三、源码
class int(object):
"""
int(x=0) -> integer
int(x, base=10) -> integer
Convert a number or string to an integer, or return 0 if no arguments
are given. If x is a number, return x.__int__(). For floating point
numbers, this truncates towards zero.
If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base. The literal can be preceded by '+' or '-' and be surrounded
by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
"""
def bit_length(self): ; restored from __doc__
"""
int.bit_length() -> int
Number of bits necessary to represent self in binary.
>>> bin(37)
'0b100101'
>>> (37).bit_length()
6
"""
return 0
def conjugate(self, *args, **kwargs):
""" Returns self, the complex conjugate of any int. """
pass
@classmethod # known case
def from_bytes(cls, bytes, byteorder, *args, **kwargs): ; NOTE: unreliably restored from __doc__
"""
int.from_bytes(bytes, byteorder, *, signed=False) -> int
Return the integer represented by the given array of bytes.
The bytes argument must be a bytes-like object (e.g. bytes or bytearray).
The byteorder argument determines the byte order used to represent the
integer. If byteorder is 'big', the most significant byte is at the
beginning of the byte array. If byteorder is 'little', the most
significant byte is at the end of the byte array. To request the native
byte order of the host system, use `sys.byteorder' as the byte order value.
The signed keyword-only argument indicates whether two's complement is
used to represent the integer.
"""
pass
def to_bytes(self, length, byteorder, *args, **kwargs): ; NOTE: unreliably restored from __doc__
"""
int.to_bytes(length, byteorder, *, signed=False) -> bytes
Return an array of bytes representing an integer.
The integer is represented using length bytes. An OverflowError is
raised if the integer is not representable with the given number of
bytes.
The byteorder argument determines the byte order used to represent the
integer. If byteorder is 'big', the most significant byte is at the
beginning of the byte array. If byteorder is 'little', the most
significant byte is at the end of the byte array. To request the native
byte order of the host system, use `sys.byteorder' as the byte order value.
The signed keyword-only argument determines whether two's complement is
used to represent the integer. If signed is False and a negative integer
is given, an OverflowError is raised.
"""
pass
def __abs__(self, *args, **kwargs):
""" abs(self) """
pass
def __add__(self, *args, **kwargs):
""" Return self+value. """
pass
def __and__(self, *args, **kwargs):
""" Return self&value. """
pass
def __bool__(self, *args, **kwargs):
""" self != 0 """
pass
def __ceil__(self, *args, **kwargs):
""" Ceiling of an Integral returns itself. """
pass
def __divmod__(self, *args, **kwargs):
""" Return divmod(self, value). """
pass
def __eq__(self, *args, **kwargs):
""" Return self==value. """
pass
def __float__(self, *args, **kwargs):
""" float(self) """
pass
def __floordiv__(self, *args, **kwargs):
""" Return self//value. """
pass
def __floor__(self, *args, **kwargs):
""" Flooring an Integral returns itself. """
pass
def __format__(self, *args, **kwargs):
pass
def __getattribute__(self, *args, **kwargs):
""" Return getattr(self, name). """
pass
def __getnewargs__(self, *args, **kwargs):
pass
def __ge__(self, *args, **kwargs):
""" Return self>=value. """
pass
def __gt__(self, *args, **kwargs):
""" Return self>value. """
pass
def __hash__(self, *args, **kwargs):
""" Return hash(self). """
pass
def __index__(self, *args, **kwargs):
""" Return self converted to an integer, if self is suitable for use as an index into a list. """
pass
def __init__(self, x, base=10): # known special case of int.__init__
"""
int(x=0) -> integer
int(x, base=10) -> integer
Convert a number or string to an integer, or return 0 if no arguments
are given. If x is a number, return x.__int__(). For floating point
numbers, this truncates towards zero.
If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base. The literal can be preceded by '+' or '-' and be surrounded
by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
# (copied from class doc)
"""
pass
def __int__(self, *args, **kwargs):
""" int(self) """
pass
def __invert__(self, *args, **kwargs):
""" ~self """
pass
def __le__(self, *args, **kwargs):
""" Return self<=value. """
pass
def __lshift__(self, *args, **kwargs):
""" Return self<<value. """
pass
def __lt__(self, *args, **kwargs):
""" Return self<value. """
pass
def __mod__(self, *args, **kwargs):
""" Return self%value. """
pass
def __mul__(self, *args, **kwargs):
""" Return self*value. """
pass
def __neg__(self, *args, **kwargs):
""" -self """
pass
@staticmethod # known case of __new__
def __new__(*args, **kwargs):
""" Create and return a new object. See help(type) for accurate signature. """
pass
def __ne__(self, *args, **kwargs):
""" Return self!=value. """
pass
def __or__(self, *args, **kwargs):
""" Return self|value. """
pass
def __pos__(self, *args, **kwargs):
""" +self """
pass
def __pow__(self, *args, **kwargs):
""" Return pow(self, value, mod). """
pass
def __radd__(self, *args, **kwargs):
""" Return value+self. """
pass
def __rand__(self, *args, **kwargs):
""" Return value&self. """
pass
def __rdivmod__(self, *args, **kwargs):
""" Return divmod(value, self). """
pass
def __repr__(self, *args, **kwargs):
""" Return repr(self). """
pass
def __rfloordiv__(self, *args, **kwargs):
""" Return value//self. """
pass
def __rlshift__(self, *args, **kwargs):
""" Return value<<self. """
pass
def __rmod__(self, *args, **kwargs):
""" Return value%self. """
pass
def __rmul__(self, *args, **kwargs):
""" Return value*self. """
pass
def __ror__(self, *args, **kwargs):
""" Return value|self. """
pass
def __round__(self, *args, **kwargs):
"""
Rounding an Integral returns itself.
Rounding with an ndigits argument also returns an integer.
"""
pass
def __rpow__(self, *args, **kwargs):
""" Return pow(value, self, mod). """
pass
def __rrshift__(self, *args, **kwargs):
""" Return value>>self. """
pass
def __rshift__(self, *args, **kwargs):
""" Return self>>value. """
pass
def __rsub__(self, *args, **kwargs):
""" Return value-self. """
pass
def __rtruediv__(self, *args, **kwargs):
""" Return value/self. """
pass
def __rxor__(self, *args, **kwargs):
""" Return value^self. """
pass
def __sizeof__(self, *args, **kwargs):
""" Returns size in memory, in bytes """
pass
def __str__(self, *args, **kwargs):
""" Return str(self). """
pass
def __sub__(self, *args, **kwargs):
""" Return self-value. """
pass
def __truediv__(self, *args, **kwargs):
""" Return self/value. """
pass
def __trunc__(self, *args, **kwargs):
""" Truncating an Integral returns itself. """
pass
def __xor__(self, *args, **kwargs):
""" Return self^value. """
pass
denominator = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
"""the denominator of a rational number in lowest terms"""
imag = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
"""the imaginary part of a complex number"""
numerator = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
"""the numerator of a rational number in lowest terms"""
real = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
"""the real part of a complex number"""
Python基本数据类型之int的更多相关文章
- Python基础数据类型之int、bool、str
数据类型:int bool str list 元祖 dict 集合 int:整数型,用于各种数学运算. bool:只有两种,True和False,用户判断. str:存储少量数据,进行操作 ...
- 8、Python简单数据类型(int、float、complex、bool、str)
一.数据类型分类 1.按存值个数区分 单个值:数字,字符串 多个值(容器):列表,元组,字典,集合 2.按可变不可变区分 可变:列表[],字典{},集合{} 不可变:数字,字符串,元组().bool, ...
- Python基本数据类型之int 、 float
首先要明确的是:在python中,一切皆为对象. 从底层角度看,对象就是保存在内存中的一个数据块.从抽象层看,对象就是我们的代码模拟出的一个类的独立个体. 在python中变量不需要声明类型,也不需要 ...
- python基本数据类型,int,bool,str
一丶python基本数据类型 1.int 整数,主要用来进行数学运算. 2.str 字符串,可以保存少量数据并进行相应的操作 3.bool 判断真假.True.False 4.list 存储大量数据, ...
- python学习之数据类型(int,bool,str)
第三章 数据类型 3.1 Python基本数据类型 类型 含义 描述 int 整数 主要用来进⾏数学运算 str 字符串 可以保存少量数据并进⾏相应的操作 bool 布尔值 判断真假,True,Fal ...
- python中的基本数据类型之 int bool str
一.基本数据类型 1. int ==> 整数.主要用来进行数学运算. 2.str ==> 字符串.可以保存少量的数据,并进行相应的操作. 3.bool => 布尔值.判断 ...
- python 基本数据类型分析
在python中,一切都是对象!对象由类创建而来,对象所拥有的功能都来自于类.在本节中,我们了解一下python基本数据类型对象具有哪些功能,我们平常是怎么使用的. 对于python,一切事物都是对象 ...
- python常用数据类型内置方法介绍
熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功. 下面介绍了python常用的集中数据类型及其方法,点开源代码,其中对主要方法都进行了中文注释. 一.整型 a = 100 a.xx ...
- python自学笔记(二)python基本数据类型之字符串处理
一.数据类型的组成分3部分:身份.类型.值 身份:id方法来看它的唯一标识符,内存地址靠这个查看 类型:type方法查看 值:数据项 二.常用基本数据类型 int 整型 boolean 布尔型 str ...
随机推荐
- requirejs:性能优化-及早并行加载
为了提高页面的性能,通常情况下,我们希望资源尽可能地早地并行加载.这里有两个要点,首先是尽早,其次是并行. 通过data-main方式加载要尽可能地避免,因为它让requirejs.业务代码不必要地串 ...
- jquery 选择器大全
jquery 选择器大体上可分为4 类: 1.基本选择器 2.层次选择器 3.过滤选择器 4.表单选择器 其中过滤选择器可以分为: 1.简单过滤选择器 2.内容过滤选择器 3.可见性过滤选择器 4.属 ...
- [BZOJ1564][NOI2009]二叉查找树(区间DP)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1564 分析: 首先因为每个点的数据值不变,所以无论树的形态如何变,树的中序遍历肯定不变 ...
- 准确率P 召回率R
Evaluation metricsa binary classifier accuracy,specificity,sensitivety.(整个分类器的准确性,正确率,错误率)表示分类正确:Tru ...
- Android开发之Menu和actionBar
一.通过Menu目录下创建一个布局文件: 先看代码meu/main.xml: <?xml version="1.0" encoding="utf-8"?& ...
- Linux终端更改提示符
打开~/.bashrc可以看到命令提示的内容为:\u@\h\w\$ \u表示用户名,\h表示主机名,\w表示当前目录,\$表示命令提示符(普通用户$,超级用户#) 这个命令提示符有点长,很碍事,\u@ ...
- 屠龙之路_任生活如何虐你,屠龙之路还得继续_SeventhDay
摘要 :屠龙少年在"罢工"了一天,在周末客栈补给和放纵之后,突然想起来说好的和公主私奔呢?(此处出现了为何上篇随笔不见公主)咋想之下,貌似公主还在恶龙Alpha的手中.为此,屠龙少 ...
- Beta--项目冲刺第七天
胜利在望-- 队伍:F4 成员:031302301 毕容甲 031302302 蔡逸轩 031302430 肖阳 031302418 黄彦宁 会议内容: 1.站立式会议照片: 2.项目燃尽图 3.冲刺 ...
- KVM 内存虚拟化
内存虚拟化的概念 除了 CPU 虚拟化,另一个关键是内存虚拟化,通过内存虚拟化共享物理系统内存,动态分配给虚拟机.虚拟机的内存虚拟化很象现在的操作系统支持的虚拟内存方式,应用程序看到邻近的内存 ...
- “Ceph浅析”系列之二——Ceph概况
本文将对Ceph的基本情况进行概要介绍,以期读者能够在不涉及技术细节的情况下对Ceph建立一个初步印象. 1. 什么是Ceph? Ceph的官方网站Ceph.com上用如下这句话简明扼要地定义了Cep ...