1、int

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
 # 1、没有参数转换为0
print(int())
print('*' * 50) # 2、一个数字转换为整数
print(int(10.6))
print('*' * 50) # 3、将一个字符串转换为整数
print(int('', 16))
print(int('', 10))
print(int('', 8))
print(int('', 2))
print('*' * 50) # 4、将一个字节流或字节数组转换为整数
print(int(b'', 16))
print(int(b'', 10))
print(int(b'', 8))
print(int(b'', 2))
print('*' * 50) # 5、base=0时,按字面值进行转换
print(int('0x111', 0))
print(int('', 0))
print(int('0o111', 0))
print(int('0b111', 0))
print('*' * 50)
 0
**************************************************
10
**************************************************
273
111
73
7
**************************************************
273
111
73
7
**************************************************
273
111
73
7
**************************************************

2、bool

class bool(int)
| bool(x) -> bool
|
| Returns True when the argument x is true, False otherwise.
| The builtins True and False are the only two instances of the class bool.
| The class bool is a subclass of the class int, and cannot be subclassed.

3、float

class float(object)
| float(x) -> floating point number
|
| Convert a string or number to a floating point number, if possible.
 print(float(123))
print(float(''))
 123.0
123.0

4、str

class str(object)
| str(object='') -> str
# 对象转字符串
| str(bytes_or_buffer[, encoding[, errors]]) -> str
# 字节流转字符串
|
| Create a new string object from the given object. If encoding or
| errors is specified, then the object must expose a data buffer
| that will be decoded using the given encoding and error handler.
| Otherwise, returns the result of object.__str__() (if defined)
| or repr(object).
| encoding defaults to sys.getdefaultencoding().
| errors defaults to 'strict'.
 # 1、对象转字符串
# 如果对象存在__str__方法,就调用它,否则调用__repr__
print(str([1, 2, 3])) # 调用了列表的__str__
print([1, 2, 3].__str__())
print(str(type)) # 调用了列表的__repr__
print(type.__repr__(type))
print('*' * 50)
# 注意
print(str(b'')) # 这个是对象转字符串,不是字节流转字符串
print(b''.__str__())
print('*' * 50) # 2、字节流转字符串
# 第一个参数是字节流对象
# 第二个参数是解码方式,默认是sys.getdefaultencoding()
# 第三个参数是转换出错时的处理方法,默认是strict
import sys
print(sys.getdefaultencoding())
print(str(b'', encoding='utf-8')) # 字节流转字符串的第一种方法
# 字节流转换字符串
print(b''.decode(encoding='utf-8')) # 字节流转字符串的第二种方法
 [1, 2, 3]
[1, 2, 3]
<class 'type'>
<class 'type'>
**************************************************
b''
b''
**************************************************
utf-8
123456
123456

5、bytearray

class bytearray(object)
| bytearray(iterable_of_ints) -> bytearray<br>
# 元素必须为[0 ,255] 中的整数
| bytearray(string, encoding[, errors]) -> bytearray<br>
# 按照指定的 encoding 将字符串转换为字节序列
| bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer<br>
# 字节流
| bytearray(int) -> bytes array of size given by the parameter initialized with null bytes<br>
# 返回一个长度为 source 的初始化数组
| bytearray() -> empty bytes array<br>
# 默认就是初始化数组为0个元素
|
| Construct a mutable bytearray object from:
| - an iterable yielding integers in range(256)
# bytearray([1, 2, 3])
| - a text string encoded using the specified encoding
# bytearray('你妈嗨', encoding='utf-8')
| - a bytes or a buffer object
# bytearray('你妈嗨'.encode('utf-8'))
| - any object implementing the buffer API.
| - an integer
# bytearray(10)
 # 1、0个元素的字节数组
print(bytearray())
print('*' * 50) # 2、指定个数的字节数组
print(bytearray(10))
print('*' * 50) # 3、int类型的可迭代对象,值在0-255
print(bytearray([1, 2, 3, 4, 5]))
print('*' * 50) # 4、字符串转字节数组
print(bytearray('', encoding='utf-8'))
print('*' * 50) # 5、字节流转字符串
print(bytearray(b''))
print('*' * 50)
 bytearray(b'')
**************************************************
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
**************************************************
bytearray(b'\x01\x02\x03\x04\x05')
**************************************************
bytearray(b'')
**************************************************
bytearray(b'')
**************************************************

6、bytes

class bytes(object)
| bytes(iterable_of_ints) -> bytes<br> # bytes([1, 2, 3, 4]) bytes must be in range(0, 256)
| bytes(string, encoding[, errors]) -> bytes<br> # bytes('你妈嗨', encoding='utf-8')
| bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer<br> #
| bytes(int) -> bytes object of size given by the parameter initialized with null bytes
| bytes() -> empty bytes object
|
| Construct an immutable array of bytes from:
| - an iterable yielding integers in range(256)
| - a text string encoded using the specified encoding
| - any object implementing the buffer API.
| - an integer
 # 1、一个空字节流
print(bytes())
print('*' * 50) # 2、一个指定长度的字节流
print(bytes(10))
print('*' * 50) # 3、int类型的可迭代对象,值0-255
print(bytes([1, 2, 3, 4, 5]))
print('*' * 50) # 4、string转bytes
print(bytes('', encoding='utf-8'))
print('*' * 50) print(''.encode(encoding='utf-8'))
print('*' * 50) # 5、bytearry转bytes print(bytes(bytearray([1, 2, 3, 4, 5])))
print('*' * 50)
 b''
**************************************************
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
**************************************************
b'\x01\x02\x03\x04\x05'
**************************************************
b''
**************************************************
b''
**************************************************
b'\x01\x02\x03\x04\x05'
**************************************************

7、list

class list(object)
| list() -> new empty list
| list(iterable) -> new list initialized from iterable's items
 # 1、返回一个空列表
print(list()) # 2、将一个可迭代对象转换成列表
print(list('string'))
print(list([1, 2, 3, 4]))
print(list({'one': 1, 'two': 2})) # 3、参数是一个列表
l = [1, 2, 3, 4]
print(id(l))
l2 = list(l) # 创建了一个新的列表,与元组不同
print(id(l2))
 []
['s', 't', 'r', 'i', 'n', 'g']
[1, 2, 3, 4]
['one', 'two']
35749640
35749704

字典转列表:

 d = {'one': 1, 'two': 2}

 for i in d:
print(i)
print('*'*50) for i in d.keys():
print(i)
print('*'*50) for i in d.values():
print(i)
print('*'*50) for i in d.items():
print(i)
print('*'*50) print(list(d))
print(list(d.keys()))
print(list(d.values()))
print(list(d.items()))
print('*'*50) print(d)
print(d.keys())
print(d.values())
print(d.items())
print('*'*50) print(type(d))
print(type(d.keys()))
print(type(d.values()))
print(type(d.items()))
print('*'*50)
 one
two
**************************************************
one
two
**************************************************
1
2
**************************************************
('one', 1)
('two', 2)
**************************************************
['one', 'two']
['one', 'two']
[1, 2]
[('one', 1), ('two', 2)]
**************************************************
{'one': 1, 'two': 2}
dict_keys(['one', 'two'])
dict_values([1, 2])
dict_items([('one', 1), ('two', 2)])
**************************************************
<class 'dict'>
<class 'dict_keys'>
<class 'dict_values'>
<class 'dict_items'>
**************************************************

8、tuple

class tuple(object)
| tuple() -> empty tuple
# 创建一个空的元组
| tuple(iterable) -> tuple initialized from iterable's items
# 将一个可迭代对象转成元组
|
| If the argument is a tuple, the return value is the same object.
# 如果参数时一个元组,返回一个相同的对象
 # 1、返回一个空元组
print(tuple()) # 2、将一个可迭代对象转换成元组
print(tuple('string'))
print(tuple([, , , ]))
print(tuple({'one': , 'two': })) # 3、参数是一个元组
tp = (, , , )
print(id(tp))
tp2 = tuple(tp)
print(id(tp2))
 ()
('s', 't', 'r', 'i', 'n', 'g')
(1, 2, 3, 4)
('one', 'two') # 参数是字典的时候,返回的是键值的元组
35774616
35774616

字典转元组:

 d = {'one': 1, 'two': 2}

 for i in d:
print(i)
print('*'*50) for i in d.keys():
print(i)
print('*'*50) for i in d.values():
print(i)
print('*'*50) for i in d.items():
print(i)
print('*'*50) print(tuple(d))
print(tuple(d.keys()))
print(tuple(d.values()))
print(tuple(d.items()))
print('*'*50) print(d)
print(d.keys())
print(d.values())
print(d.items())
print('*'*50) print(type(d))
print(type(d.keys()))
print(type(d.values()))
print(type(d.items()))
print('*'*50)
one
two
**************************************************
one
two
**************************************************
1
2
**************************************************
('one', 1)
('two', 2)
**************************************************
('one', 'two')
('one', 'two')
(1, 2)
(('one', 1), ('two', 2))
**************************************************
{'one': 1, 'two': 2}
dict_keys(['one', 'two'])
dict_values([1, 2])
dict_items([('one', 1), ('two', 2)])
**************************************************
<class 'dict'>
<class 'dict_keys'>
<class 'dict_values'>
<class 'dict_items'>
**************************************************

9、dict

class dict(object)
| dict() -> new empty dictionary # 空字典
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs<br> # dict([('one', 1), ('two', 2)])
| dict(iterable) -> new dictionary initialized as if via:
| d = {}
| for k, v in iterable:
| d[k] = v
| dict(**kwargs) -> new dictionary initialized with the name=value pairs
| in the keyword argument list. For example: dict(one=1, two=2)
 # 1、空字典
print(dict())
print('*' * 50) # 2、从一个映射对象创建字典
print(dict([('one', 1), ('two', 2)]))
print('*' * 50) # 3、从一个带key和value的可迭代对象创建字典
d = {'one': 1, 'two': 2}
dt = {}
for k, v in d.items():
dt[k] = v
print(dt)
print('*' * 50) # 4、以name=value对创建字典
print(dict(one=1, two=2))
 {}
**************************************************
{'one': 1, 'two': 2}
**************************************************
{'one': 1, 'two': 2}
**************************************************
{'one': 1, 'two': 2}

10、set

class set(object)
| set() -> new empty set object
| set(iterable) -> new set object
 # 1、创建空集合
print(set()) # 2、可迭代对象转换为集合
print(set([1, 2, 1, 2]))
 set()      # 空集合这样表示,{}表示字典
{1, 2}

11、frozenset

class frozenset(object)
| frozenset() -> empty frozenset object
| frozenset(iterable) -> frozenset object
|
| Build an immutable unordered collection of unique elements.
# 与set的区别是不可变得
 # 1、创建空集合
print(frozenset()) # 2、可迭代对象转换为集合
fs = frozenset([1, 2, 1, 2])
print(fs) # 3、为set和frozenset对象添加元素
s = set((100, ))
s.add(10)
print(s) fs.add(10)
 frozenset()
frozenset({1, 2})
{10, 100}
Traceback (most recent call last):
File "1.py", line 14, in <module>
fs.add(10)
AttributeError: 'frozenset' object has no attribute 'add'

12、complex

class complex(object)
| complex(real[, imag]) -> complex number
|
| Create a complex number from a real part and an optional imaginary part.
| This is equivalent to (real + imag*1j) where imag defaults to 0.
 print(complex(10))
print(complex(10, 10))
a = 10 + 0j
print(a)
print(a == 10)
 (10+0j)
(10+10j)
(10+0j)
True

13、type

class type(object)
| type(object_or_name, bases, dict)
# 创建一个类
| type(object) -> the object's type
# 查看这个对象的类型
| type(name, bases, dict) -> a new type
#
 # type功能1,判断对象的类型
print(type(int))
print(type()) # type功能2,动态创建类
# 第一个参数是字符串
# 第二个参数是父类的元组
# 第三个参数是属性的字典
class Animal():
pass Dog = type('Dog', (Animal,), {})
dog = Dog() print(type(Dog))
print(type(dog))

python 基本类型的创建方法的更多相关文章

  1. python字符类型的一些方法

    python 字符串和字节互转换.bytes(s, encoding = "utf8") str(b, encoding = "utf-8") i.isspac ...

  2. python pandas ---Series,DataFrame 创建方法,操作运算操作(赋值,sort,get,del,pop,insert,+,-,*,/)

    pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包 pandas 也是围绕着 Series 和 DataFrame 两个核心数据结构展开的, 导入如下: from panda ...

  3. Python 错误类型及解决方法

    SyntaxError: invalid syntax          表示“语法错误:不正确的语法” 检查代码的缩进,代码格式是否正确,Python的缩进一般为四个空格,tab键尽量不要用. In ...

  4. python序列类型字符串的方法L.index()与L.find()区别

    首先官方解释 S.index(sub[, start[, end]]) -> int Like S.find() but raise ValueError when the substring ...

  5. gluster 卷的类型及创建方法

    基本卷: 分布式卷 文件随机分布在brick中,提升读写性能 不提供数据冗余,最大化利用磁盘空间 # gluster volume create test-volume server1:/exp1 s ...

  6. python基础===列表类型的所有方法

    链表类型有很多方法,这里是链表类型的所有方法: append(x) 把一个元素添加到链表的结尾,相当于a[len(a):] = [x] extend(L) 通过添加指定链表的所有元素来扩充链表,相当于 ...

  7. Python序列类型各自方法

    在Python输入dir(str).dir(list).dir(tuple)可查看各种序列类型的所有方法. 对于某个方法不懂怎么使用的情况,可以直接help(str.split)对某个方法进行查询. ...

  8. Python:数字类型和字符串类型的内置方法

    一.数字类型内置方法 1.1 整型的内置方法 作用 描述年龄.号码.id号 定义方式 x = 10 x = int('10') x = int(10.1) x = int('10.1') # 报错 内 ...

  9. Python 变量类型

    Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据 ...

随机推荐

  1. C#语法复习1

    一.C#与.net框架 .net是语言无关的. 程序的执行流程: .net兼容语言的源代码文件 .net兼容编译器 程序集(公共中间语言(CIL)common intermediate languag ...

  2. Effective C++ 条款三 尽可能使用const

    参考资料:http://blog.csdn.net/bizhu12/article/details/6672723      const的常用用法小结 1.用于定义常量变量,这样这个变量在后面就不可以 ...

  3. MySQL和MongoDB的性能测试

    软硬件环境 MySQL版本:5.1.50,驱动版本:5.1.6(最新的5.1.13有很多杂七杂八的问题) MongoDB版本:1.6.2,驱动版本:2.1 操作系统:Windows XP SP3(这个 ...

  4. AIX下RAC搭建 Oracle10G(二)主机配置

    AIX下RAC搭建系列 AIX下RAC搭建 Oracle10G(二)主机配置 环境 节点 节点1 节点2 小机型号 IBM P-series 630 IBM P-series 630 主机名 AIX2 ...

  5. HashTable源代码剖析

    <span style="font-size:14px;font-weight: normal;">public class Hashtable<K,V> ...

  6. C语言restrict关键字的使用----可以用来优化代码

    C99中新增加了restrict修饰的指针:由restrict修饰的指针是最初唯一对指针所指向的对象进行存取的方法,仅当第二个指针基于第一个时,才能对对象进行存取.对对象的存取都限定于基于由restr ...

  7. UICollectionViewController xcode6.1 自定义Cell

    本文转载至 http://blog.csdn.net/daleiwang/article/details/40423219 UICollectionViewContAutolayoutstoryboa ...

  8. python day- 10 动态参数 函数的嵌套 命名空间和作用域 global和nolocal

    一.动态参数: 动态参数是形参的一类 分为:动态位置参数(* + 函数名)表示 调用后返回的是元祖 动态关键字参数(** + 函数名)表示 形参的排列顺序: 位置参数     >   动态位置参 ...

  9. luogu3379 【模板】最近公共祖先(LCA) 倍增法

    题目大意:给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 整体步骤:1.使两个点深度相同:2.使两个点相同. 这两个步骤都可用倍增法进行优化.定义每个节点的Elder[i]为该节点的2^k( ...

  10. RabbitMQ 使用

    安装步骤略过. 启动 启动很简单,找到安装后的 RabbitMQ 所在目录下的 sbin 目录,可以看到该目录下有6个以 rabbitmq 开头的可执行文件,直接执行 rabbitmq-server ...