python数据类型之内置方法

python有六大数据类型,分别为整型、字符串、列表、字典、元祖和集合,这些基本数据类型都内置了很多方法,接下来一一探寻。

一 整型

python中整型有两种:int和float

1 int

使用dir函数查看有多少内置方法

# python3.x
dir(int)
# [‘__abs__‘, ‘__add__‘, ‘__and__‘, ‘__bool__‘, ‘__ceil__‘, ‘__class__‘, ‘__delattr__‘, ‘__dir__‘, ‘__divmod__‘, ‘__doc__‘, ‘__eq__‘, ‘__float__‘, ‘__floor__‘, ‘__floordiv__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__index__‘, ‘__init__‘, ‘__int__‘, ‘__invert__‘, ‘__le__‘, ‘__lshift__‘, ‘__lt__‘, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__neg__‘, ‘__new__‘, ‘__or__‘, ‘__pos__‘, ‘__pow__‘, ‘__radd__‘, ‘__rand__‘, ‘__rdivmod__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rfloordiv__‘, ‘__rlshift__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__ror__‘, ‘__round__‘, ‘__rpow__‘, ‘__rrshift__‘, ‘__rshift__‘, ‘__rsub__‘, ‘__rtruediv__‘, ‘__rxor__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__sub__‘, ‘__subclasshook__‘, ‘__truediv__‘, ‘__trunc__‘, ‘__xor__‘, ‘bit_length‘, ‘conjugate‘, ‘denominator‘, ‘from_bytes‘, ‘imag‘, ‘numerator‘, ‘real‘, ‘to_bytes‘]

# python 2.x
dir(int)
# [‘__abs__‘, ‘__add__‘, ‘__and__‘, ‘__class__‘, ‘__cmp__‘, ‘__coerce__‘, ‘__delattr__‘, ‘__div__‘, ‘__divmod__‘, ‘__doc__‘, ‘__float__‘, ‘__floordiv__‘, ‘__format__‘, ‘__getattribute__‘, ‘__getnewargs__‘, ‘__hash__‘, ‘__hex__‘, ‘__index__‘, ‘__init__‘, ‘__int__‘, ‘__invert__‘, ‘__long__‘, ‘__lshift__‘, ‘__mod__‘, ‘__mul__‘, ‘__neg__‘, ‘__new__‘, ‘__nonzero__‘, ‘__oct__‘, ‘__or__‘, ‘__pos__‘, ‘__pow__‘, ‘__radd__‘, ‘__rand__‘, ‘__rdiv__‘, ‘__rdivmod__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rfloordiv__‘, ‘__rlshift__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__ror__‘, ‘__rpow__‘, ‘__rrshift__‘, ‘__rshift__‘, ‘__rsub__‘, ‘__rtruediv__‘, ‘__rxor__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__sub__‘, ‘__subclasshook__‘, ‘__truediv__‘, ‘__trunc__‘, ‘__xor__‘, ‘bit_length‘, ‘conjugate‘, ‘denominator‘, ‘imag‘, ‘numerator‘, ‘real‘]

因为是整型类型,所以有加减乘除、取绝对值的方法,像__abs__是取绝对值,__add__是加法等,其中重要的几个方法是:bit_length、conjugate、from_bytes、imag、numerator、real、to_bytes

digit = 123
print(digit.bit_length())
# 进入源码,Number of bits necessary to represent self in binary
# 首先把int型转为二进制,然后计算二进制的位数

bit_length

num = 2.3 - 2.5j
result = num.real       #复数的实部
print(result)   #打印输出2.3
result = num.imag    #复数的虚部
print(result)   #打印输出2.5j

result = num.conjugate()   #返回该复数的共轭复数
print(result)  #打印输出(2.3+2.5j)

conjugate

print(int.from_bytes(bytes=b‘A‘, byteorder=‘little‘)

#打印输出 65  ,即将字符A转换为十进制

from_bytes

num = 2
result = num.to_bytes(5,byteorder=‘little‘)
print(result)
#打印输出b‘\x02\x00\x00\x00\x00‘
for i in result:
    print(i)

#打印输出2\n0\n0\n0\n0
#\n表示回车

to_bytes

imag、real分别是计算出复制的实部和虚部,conjugate得出共轭复数,numerator不知道什么鬼。

2 str

#python3.5
dir(str)
#[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘capitalize‘, ‘casefold‘, ‘center‘, ‘count‘, ‘encode‘, ‘endswith‘, ‘expandtabs‘, ‘find‘, ‘format‘, ‘format_map‘, ‘index‘, ‘isalnum‘, ‘isalpha‘, ‘isdecimal‘, ‘isdigit‘, ‘isidentifier‘, ‘islower‘, ‘isnumeric‘, ‘isprintable‘, ‘isspace‘, ‘istitle‘, ‘isupper‘, ‘join‘, ‘ljust‘, ‘lower‘, ‘lstrip‘, ‘maketrans‘, ‘partition‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rpartition‘, ‘rsplit‘, ‘rstrip‘, ‘split‘, ‘splitlines‘, ‘startswith‘, ‘strip‘, ‘swapcase‘, ‘title‘, ‘translate‘, ‘upper‘, ‘zfill‘]

#python2.7
dir(str)
#[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__getslice__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘_formatter_field_name_split‘, ‘_formatter_parser‘, ‘capitalize‘, ‘center‘, ‘count‘, ‘decode‘, ‘encode‘, ‘endswith‘, ‘expandtabs‘, ‘find‘, ‘format‘, ‘index‘, ‘isalnum‘, ‘isalpha‘, ‘isdigit‘, ‘islower‘, ‘isspace‘, ‘istitle‘, ‘isupper‘, ‘join‘, ‘ljust‘, ‘lower‘, ‘lstrip‘, ‘partition‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rpartition‘, ‘rsplit‘, ‘rstrip‘, ‘split‘, ‘splitlines‘, ‘startswith‘, ‘strip‘, ‘swapcase‘, ‘title‘, ‘translate‘, ‘upper‘, ‘zfill‘]

# 计算字符串中某个字符的总数
str1 = ‘aaabc‘
print(str1.count(‘a‘))
# 输出结果为 3

count

capitalize

# 编码 默认为utf-8(Unicode)
str1 = ‘aaabc‘
print(str1.encode())
# 输出结果为 b‘aaabc‘

encode

# 判断字符串是否已某个字符结束,返回值为布尔值
str1 = ‘aaabc‘
print(str1.endswith(‘c‘))
# 输出结果为 True

endswitch

# 找到字符串中某个字符第一次出现的索引
str1 = ‘aaabc‘
print(str1.find(‘a‘))
# 输出结果为 0

find

# 返回某个字符第一次出现的索引
str1 = ‘aaabc‘
print(str1.index(‘a‘))
# 输出结果为 0

index

# 判断字符串中字符是否都是数字,返回一个布尔值
str1 = ‘aaabc‘
print(str1.isdigit())
# 输出结果为 False

str2 = ‘123‘
print(str2.isdigit())
# 输出结果为 True

isdigit

# 判断字符串中的字符是否全为小写
S = ‘abc‘
print(S.islower())
# 输出结果为True

S = ‘123‘
print(S.islower())
# 输出结果为False

islower

# 判断S中字符是否全为大写
S = ‘ABC‘
print(S.isupper())
# 输出结果为True

S = ‘123‘
print(S.isupper())
# 输出结果为False

isupper

# 将S中的字符用分隔符分隔
S = ‘abcd‘
print(‘.‘.join(S))
# 输出结果为 a.b.c.d

join

# 将S中的字符全改为小写
S = ‘AbCd123‘
print(S.lower())
# 输出结果为 abcd123

lower

# 将S中左边的空格去掉
S = ‘  AbCd123‘
print(S.lstrip())
# 输出结果为 AbCd123

lstrip

# 将S中的字符用字符替换
S = ‘AbCd123‘
print(S.replace(‘A‘, ‘a‘))
# 输出结果为 abCd123

replace

S = ‘AbCd123‘
print(S.split(‘d‘))
# 输出结果为 [‘AbC‘, ‘123‘]

split

# 判断S是否以某个字符开始,返回一个布尔值
S = ‘AbCd123‘
print(S.startswith(‘A‘))
# 输出结果为 True

S = ‘AbCd123‘
print(S.startswith(‘d‘))
# 输出结果为 False

startswitch

# 将S左右两边的空格去掉
S = ‘  AbCd123   ‘
print(S.strip())
# 输出结果为 AbCd123

strip

# 将S中所有的字符大写
S = ‘AbCd123   ‘
print(S.upper())
# 输出结果为 ABCD123

upper

3 list

# python 3.x
dir(list)
# [‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__iadd__‘, ‘__imul__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘append‘, ‘clear‘, ‘copy‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sort‘]

# python 2.x
dir(list)
# [‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__delslice__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getslice__‘, ‘__gt__‘, ‘__hash__‘, ‘__iadd__‘, ‘__imul__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__setslice__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘append‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sort‘]

# 往列表追加元素
lis = [‘a‘, ‘b‘, ‘c‘, 1, 2, 3]
lis.append(4)
print(lis)
# 输出结果 [‘a‘, ‘b‘, ‘c‘, 1, 2, 3, 4]

append

# 清空列表
lis = [‘a‘, ‘b‘, ‘c‘, 1, 2, 3]
lis.clear()
print(lis)
# 输出结果 []

clear

# 拷贝一个列表并返回该列表
lis = [‘a‘, ‘b‘, ‘c‘, 1, 2, 3]
lis1 = lis.copy()
print(lis1)
# 输出结果 [‘a‘, ‘b‘, ‘c‘, 1, 2, 3]

copy

# 计算列表中某个元素的数量
lis = [‘a‘, ‘b‘, ‘c‘, 1, 2, 3]
lis1 = lis.copy()
print(lis1)
# 输出结果 [‘a‘, ‘b‘, ‘c‘, 1, 2, 3]

count

# 合并列表
lis1 = [‘a‘, ‘b‘, ‘c‘, 1, 2, 3]
lis2 = [4, 5, 6, 7, 8]
lis1.extend(lis2)
print(lis1)
# 输出结果 [‘a‘, ‘b‘, ‘c‘, 1, 2, 3, 4, 5, 6, 7, 8]

extend

# 返回列表中元素的第一个出现位置的索引
lis1 = [‘a‘, ‘a‘, ‘b‘, ‘c‘, 1, 2, 3]
ind = lis1.index(‘a‘)
print(ind)
# 输出结果 0

index

# 插入到列表中某个索引位置
lis1 = [‘a‘, ‘a‘, ‘b‘, ‘c‘, 1, 2, 3]
lis1.insert(4, ‘d‘)
print(lis1)
# 输出结果 [‘a‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, 1, 2, 3]

insert

# 弹出列表中某个索引位置的值,默认弹出最后一个
lis1 = [‘a‘, ‘a‘, ‘b‘, ‘c‘, 1, 2, 3]
lis1.pop()
print(lis1)
# 输出结果 [‘a‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, 1, 2, ]

lis1 = [‘a‘, ‘a‘, ‘b‘, ‘c‘, 1, 2, 3]
lis1.pop(4)
print(lis1)
# 输出结果 [‘a‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, 2, 3]

pop

# 删除列表中的元素
lis1 = [‘a‘, ‘a‘, ‘b‘, ‘c‘, 1, 2, 3]
a = lis1.remove(‘a‘)
print(lis1)
# 输出结果 [‘a‘, ‘b‘, ‘c‘, 1, 2, 3]
# 和pop方法不同的地方有两个:pop通过索引取出值,有返回值;remove通过元素删除,没有返回值

remove

# 反转列表
lis1 = [‘a‘, ‘a‘, ‘b‘, ‘c‘, 1, 2, 3]
lis1.reverse()
print(lis1)
# 输出结果 [3, 2, 1, ‘c‘, ‘b‘, ‘a‘, ‘a‘]

reverse

# 排序
lis1 = [‘a‘, ‘a‘, ‘b‘, ‘c‘, ‘r‘, ‘c‘, ‘g‘, ‘l‘]
lis1.sort()
print(lis1)
# 输出结果 [‘a‘, ‘a‘, ‘b‘, ‘c‘, ‘c‘, ‘g‘, ‘l‘, ‘r‘]

sort

4 dict

#python3.x
dir(dict)
#[‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘clear‘, ‘copy‘, ‘fromkeys‘, ‘get‘, ‘items‘, ‘keys‘, ‘pop‘, ‘popitem‘, ‘setdefault‘, ‘update‘, ‘values‘]

#python2.x
dir(dict)
#[‘__class__‘, ‘__cmp__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘clear‘, ‘copy‘, ‘fromkeys‘, ‘get‘, ‘has_key‘, ‘items‘, ‘iteritems‘, ‘iterkeys‘, ‘itervalues‘, ‘keys‘, ‘pop‘, ‘popitem‘, ‘setdefault‘, ‘update‘, ‘values‘, ‘viewitems‘, ‘viewkeys‘, ‘viewvalues‘]

# 清空字典
dic = {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘}
dic.clear()
print(dic)
# 输出结果 {}

clear

# 拷贝字典
dic = {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘}
dic1 = dic.copy()
print(dic1)
# 输出结果 {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘}

copy

# 返回一个新的字典
dic = {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘}
dic1 = dic.fromkeys((‘l‘), (‘hashangda‘))
print(dic1)
print(dic)
# 输出结果 [‘l‘: ‘hashangda‘}
# {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘}
# 不改变前面的字典

fromkeys

# 通过key取值,如果取不到也不报错
dic = {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘}
name1 = dic.get(‘name‘)
other = dic.get(‘school‘)
print(name1)
print(other)
# 输出结果 szz,None

get

# 获取字典的key和value
dic = {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘}
for key, value in dic.items():
    print(key, value)
# 输出结果 name szz age 18 addr shanghai

items

# 获取字典所有的键值
dic = {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘}
keys = dic.keys()
print(keys)
# 输出结果 dict_keys([‘name‘, ‘age‘, ‘addr‘])

keys

# 弹出字典中的键值对
dic = {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘, ‘school‘: ‘hashangda‘}
item = dic.pop(‘name‘)
print(item)
print(dic)
# 输出结果 szz {‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘, ‘school‘: ‘hashangda‘}

pop

# 弹出字典中最后一个键值对
dic = {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘, ‘school‘: ‘hashangda‘}
item = dic.popitem()
print(item)
print(dic)
# 输出结果 (‘school‘, ‘hashangda‘) {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘}
# 弹出的键和值用列表存储

popitem

# 给字典设置默认值
dic = {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘, ‘school‘: ‘hashangda‘}
dic.setdefault(‘male‘, 1)
print(dic)
# 输出结果 {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘, ‘school‘: ‘hashangda‘, ‘male‘: 1}

setdeafault

# 更新字典中的键值对
dic = {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘, ‘school‘: ‘hashangda‘}
dic1 = {‘age‘: 23}
dic.update(dic1)
print(dic)
# 输出结果 {‘name‘: ‘szz‘, ‘age‘: 23, ‘addr‘: ‘shanghai‘, ‘school‘: ‘hashangda‘}

update

# 得到字典所有的值
dic = {‘name‘: ‘szz‘, ‘age‘: ‘18‘, ‘addr‘: ‘shanghai‘, ‘school‘: ‘hashangda‘}
values = dic.values()
print(values)
# 输出结果 dict_values([‘szz‘, ‘18‘, ‘shanghai‘, ‘hashangda‘])

values

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 7.5px Helvetica; color: #929292; background-color: #393939 }
span.s1 { color: #b7c4d1 }
span.s2 { color: #7aa8c7 }
span.s3 { color: #9a9cd1 }

原文地址:https://www.cnblogs.com/zuanzuan/p/9651821.html

时间: 2024-08-15 13:28:44

python数据类型之内置方法的相关文章

python进阶之内置方法

python进阶之内置方法 字符串类型的内置方法 常用操作与方法: 按索引取值 str[index] 切片 ste[start:stop:step] 长度 len(str) 成员运算in和not in str1 in str2; str1 not in str2 移除空白 str.strip() 切分 str.split() 循环 for i in str : 需要掌握的: 左/右去除空白 lstrip/rstrip 截掉 string 左/右边的空格 大/小写 upper/lower 转换 s

Python基础之内置方法

目录 字符串的内置方法 按索引取值 切片(顾头不顾尾,步长) 长度len 成员运算 移除两边空白strip 切分split 循环 lower&upper startswith & endswith replace join isdigit 列表类型的内置方法 按索引取值(正反向取值同字符串) 切片(同字符串) 长度(同字符串) 成员运算(同字符串) 追加值 删除 循环(同字符串) insert() pop() remove() count() index() extend() revers

python常用数据类型内置方法介绍

熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功. 一.整型 a = 100 a.xxx() class int(object): def bit_length(self): ##如果将某个整数用2进制表示,返回这个2进制所占bit位数. return 0 def conjugate(self, *args, **kwargs): ##共轭复数 @classmethod # known case def from_bytes(cls, bytes, byteorder, *ar

python学习第四周之内置方法详解

1.python的内置方法有很多,用的时候可以自行百度,我只写几个我感兴趣的(任性.) 2.(1)bin(),将十进制转变为二进制 >>> bin(2) '0b10' (2)chr(),查看数字所对应的字母, >>> chr(98) 'b' (3)ord(),查看字母对应的数字 >>> ord('a') 97 (4)hex(),转换成十六进制 >>> hex(255) '0xff' (5)oct(),转成成八进制 >>&g

python基础之内置函数与匿名函数

python基础之内置函数与匿名函数 内置函数68个如下图 重点的关注的内置函数len,sorted,enumerate,all,any,zip,filter,map,reversed,slice len(o):参数为O,返回数据类型的长度sorted():      sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list iterable:是可迭代类型; cmp:用于比较的函数,比较什么由key决定; ke

数据类型内置方法

目录 数据类型内置方法总结 数字类型内置方法 整型int() 浮点型 float() 字符串类型内置方法 列表类型内置方法 元组类型内置方法 字典类型内置方法 集合类型内置方法 数据类型总结 拷贝 深浅拷贝 数据类型内置方法总结 数字类型内置方法 整型int() 定义方式 age = int(28) int()函数可以将数字转换为整型,直接省去小数部分 常用操作 算术操作 + 比较运算符 长整型 python2 中有长整型概念,python3中没有 存一个值 or 多个值 : 一个值 可变 or

Python的内置方法,abs,all,any,basestring,bin,bool,bytearray,callable,chr,cmp,complex,divmod

Python的内置方法 abs(X):返回一个数的绝对值,X可以是一个整数,长整型,或者浮点数,如果X是一个复数,此方法返回此复数的绝对值(此复数与它的共轭复数的乘积的平方根) >>> abs(3+2j) 3.605551275463989 >>> abs(3-2j) 3.605551275463989 all(iterable):如果迭代器的所有元素都是true,或者空迭代器,则此方法返回true. any(iterable):迭代器只要有一个元素为false,或者空

数据类型内置方法之数据类型与字符串类型

数据类型内置方法 数字类型内置方法 整型 1.用途:年龄.身份证号码.手机号... 2.定义方式: age1 = 18 age2 = int(18) print(age1) print(id(age1)) print(type(age1)) print(type(age2)) 18 1723559936 <class 'int'> <class 'int'> 3.常用操作和内置方法:+ - * / // ** % :无内置方法 4.存一个值or多个值:一个值 5.有序or无序(有索

数据类型内置方法(1)

数据类型内置方法 为什么要有数据类型 对于不同类型的数据需要不同的数据类型去描述 数字类型内置方法 整形 作用:描述身高.体重.号码等整数 定义:id = 111111 方法:+ - * / % // 存储一个值or多个值:一个值 有序or无序:没有此概念 可变or不可变:不可变 有序无序指数据是否有索引,可变不可变指数据改变后内存地址是否改变 浮点型 作用:描述金额.导弹轨迹等效等小数 定义:salary = 2222.222 方法:+ - * / % // 存储一个值or多个值:一个值 有序