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 ...
随机推荐
- 开源磁力搜索爬虫dhtspider原理解析
开源地址:https://github.com/callmelanmao/dhtspider. 开源的dht爬虫已经有很多了,有php版本的,python版本的和nodejs版本.经过一些测试,发现还 ...
- C# 值类型和引用类型
一.基本概念 C#只有两种数据类型:值类型和引用类型 值类型在线程栈分配空间,引用类型在托管堆分配空间 值类型转为引用类型称成为装箱,引用类型转为值类型称为拆箱 以下是值类型和引用类型对照表 从上图可 ...
- 你能熟练使用Dictionary字典和List列表吗?(转)
命名空间System.Collections.Generic中有两个非常重要,而且常用的泛型集合类,它们分别是Dictionary<TKey,TValue>字典和List<T> ...
- 知乎日报win10版 - 天天读报【开源】
业余时间写的一个知乎日报win10版客户端,支持收藏,评论,点赞等. 商店地址:https://www.microsoft.com/zh-cn/store/apps/%E5%A4%A9%E5%A4%A ...
- HDInsight - 1,简介
最近工作需要,要看HDInsight部分,这里要做笔记.自然是官网资料最权威,所以内容都从这里搬过来:https://azure.microsoft.com/en-us/documentation/a ...
- Paris Traceroute
Paris Traceroute is a new version of the well-known network diagnosis and measurement tool. Why shou ...
- Collections的应用
Collection : 接口 Collections : 集合的工具类 Arrays (数组的工具类) 只能操作list集合 说出Collection和Collections 的区别 ...
- 1118sync_binlog innodb_flush_log_at_trx_commit 浅析
转自 http://blog.itpub.net/22664653/viewspace-1063134/ innodb_flush_log_at_trx_commit和sync_binlog 两个参 ...
- iOS常用---NSArray,NSMutabuleArray
// 创建一个普通的数组,数组中可存储 id(任意)类型 NSArray *array =[[NSArray alloc]initWithObjects:",@"c" ...
- asp.net mvc输出自定义404等错误页面,非302跳转。
朋友问到一个问题,如何输出自定义错误页面,不使用302跳转.当前页面地址不能改变. 还要执行一些代码等,生成一些错误信息,方便用户提交反馈. 500错误,mvc框架已经有现成解决方法: filters ...