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 ...
随机推荐
- 知乎日报win10版 - 天天读报【开源】
业余时间写的一个知乎日报win10版客户端,支持收藏,评论,点赞等. 商店地址:https://www.microsoft.com/zh-cn/store/apps/%E5%A4%A9%E5%A4%A ...
- JavaScript的理解记录(3)
---接上篇 一.函数: 1. 函数定义后直接执行:var f = (function(x){ return x*10}(10)); 2. 函数的调用有四种方式: 作为函数:作为方法:作为构造函 ...
- 内网穿透神器ngrok——将本地项目驾到外网
相信做Web开发的同学们,经常会遇到需要将本地部署的Web应用能够让公网环境直接访问到的情况,例如微信应用调试.支付宝接口调试等.这个时候,一个叫ngrok的神器可能会帮到你,它提供了一个能够在公网安 ...
- eclipse技巧总结
如果遇到错误或警告,先试试统一的方法:在problems view中,右键error或者warnning,选择quick fix serial ID并不常用,如果不实现它,eclipse会给出一 ...
- Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo
更新了xcode后使用goland运行项目时提示 Agreeing to the Xcode/iOS license requires admin privileges, please re-run ...
- 41-tr 简明笔记
替换指定的字符 tr [options] string1 string2 参数 tr通常带有两个参数:string1 和 string2 ,当tr发现与string1中有匹配的字符时,它将使用stri ...
- HIbernate的增删改
数据库是oracle 以一对多为例:user50一的一方 order50是多的一方 首先是实体类: 这里的实体是双向关系,既通过user50可以找到order50,通过order50可以找到 ...
- Java文件拷贝
package com.lxm.demos; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io. ...
- Chrome开发工具Elements面板(编辑DOM和CSS样式)详解
Element 译为“元素”,Element 面板可以让我们动态查看和编辑DOM节点和CSS样式表,并且立即生效,避免了频繁切换浏览器和编辑器的麻烦. 我们可以使用Element面板来查看源代码,它不 ...
- kkt