Python basic programming built-in functions

Built-in functions

Built-in functions (be sure to remember and proficient)

  • print () screen output

  • int():pass

  • str():pass

  • bool():pass

  • set(): pass

  • list () will be converted into a list iterable

  • tuple () will be converted into a iterable tuple

  • dict () to create a dictionary corresponding way.

    # 创建字典的几种方式
    #直接创建
    dic = {1: 2}
    #字典推导式
    print({i: 1 for i in range(3)})
    dict()
    #dict创建
    dic = dict(one=1, two=2, three=3)
    print(dic)
    #fromekeys 方法创建
    fromkeys()
  • abs () returns the absolute value of

  • sum () sums

print(sum([1,2,3]))     #4
print(sum((1,2,3),100)) # 104 从100开始算总和
  • min () for the minimum
通过设置key去使用min
# 返回值是什么就按照什么比较最小的。
例1:
找年龄最小的元组:l1 = [('alex', 73, 170), ('太白', 18, 185), ('武大', 35, 159),]
print(min(l1,key=lambda x:x[1]))        #('太白', 18, 185)
#key = 函数名,#min 会自动的将可迭代对象的每一个元素作为实参传给x,
# 将遍历的那个元素即是 ('太白', 18) 返回
  • max () selecting the maximum value

  • the reversed () for an iterative inversion object, returns an iterator

  • bytes () the string into bytes Type

    bin: to convert decimal to binary and back.

    oct: converted to decimal octal string and returns.

    hex: decimal converted to a hexadecimal string and returns.

print(bin(10),type(bin(10)))  # 0b1010 <class 'str'>
print(oct(10),type(oct(10)))  # 0o12 <class 'str'>
print(hex(10),type(hex(10)))  # 0xa <class 'str'>

zip () method of the slide fastener

lst1 = [1,2,3]
lst2 = ['a','b','c','d']
lst3 = (11,12,13,14,15)
for i in zip(lst1,lst2,lst3):
    print(i)
(1, 'a', 11)
(2, 'b', 12)
(3, 'c', 13)

sorted sort function

lst=[9,8,6,48,23,615,]
new_lst=sorted(lst)     #返回的新列表是经过排序的
print(new_lst)          #[6, 8, 9, 23, 48, 615]  #原列表不会改变
lst3 = sorted(lst,reverse=True)
print(lst3)   #倒叙                       
与匿名函数配合使用
lst = [{'id':1,'name':'alex','age':18},
    {'id':2,'name':'wusir','age':17},
    {'id':3,'name':'taibai','age':16},]
print(sorted(lst,key=lambda x:x['age']))
#[{'id': 3, 'name': 'taibai', 'age': 16}, {'id': 2, 'name': 'wusir', 'age': 17}, {'id': 1, 'name': 'alex', 'age': 18}]

filtering filtering filter returns a generator

生成器表达式的筛选模式
lst = [{'id':1,'name':'alex','age':18},
        {'id':1,'name':'wusir','age':17},
        {'id':1,'name':'taibai','age':40},]
print(list(filter(lambda x:x['age']<20,lst)))
#[{'id': 1, 'name': 'alex', 'age': 18}, {'id': 1, 'name': 'wusir', 'age': 17}]
function: 用来筛选的函数,在filter中会自动的把iterable中的元素传递给function,然后根据function返回的True或者False来判断是否保留此项数据

map returns an iterator that generates equivalent expressions: cyclic pattern

lst = [1,2,3,4,5]
#生成器表达式的筛选模式
print(list(map(lambda s:s*s,lst)))      #[1, 4, 9, 16, 25]

reduce

from functools import reduce
'''
第一次:x,y 1,2 求和 3 记录到内存
第二次:x,y 3,3 求和 6 记录到内存
第三次:x,y 6,4 .........

'''
# print(reduce(lambda x,y: x+y,[1,2,3,4,5]))
# print(reduce(lambda x,y: 2*x+y, [1,2,3]))
# print(reduce(lambda x,y: x+y, ['alex','s','b']))  # 可以用字符串拼接

Built-in functions (understanding, try to remember)

  • eval: type of code execution string, and returns the final result.
eval('2 + 2')  # 4
n=81
eval("n + 4")  # 85
eval('print(666)')  # 666
  • exec: execution string types.
s = '''
for i in [1,2,3]:
    print(i)
'''
exec(s)
  • help: function module or function for viewing purposes described in detail.
print(help(list))
print(help(str.split))
  • int: function is used to convert a string or an integer number.
print(int())  # 0
print(int('12'))  # 12
print(int(3.6))  # 3
print(int('0100',base=2))  # 将2进制的 0100 转化成十进制。结果为 4
  • callable: function is used to check whether an object is callable. If it returns True, object may still call fails; but if it returns False, the calling object ojbect will never succeed.
name = 'alex'
def func():
    pass
print(callable(name))  # False
print(callable(func))  # True
  • int: function is used to convert a string or an integer number.
  • float: function is used to convert the integer to floating point and string.
  • complex: a function to create the complex value real + imag * j or transformed string or a plural number. If the first parameter is a string, there is no need to specify the second parameter. .
  • divmod: the divisor and the dividend calculation result, returns a tuple of the quotient and remainder of (a // b, a% b).
  • round: retention of floating-point decimal places, default retention integer.
  • pow: find the X- the y-th power. (Three parameters of x results of z I y taken)
  • bytes: for conversion between different coding.
  • ord: Enter the characters to find the position of the character encoding
  • chr: Enter the numbers to find out the location of the corresponding character
  • repr: Returns a string form of the object (true colors).
# %r  原封不动的写出来
# name = 'taibai'
# print('我叫%r'%name)

# repr 原形毕露
print(repr('{"name":"alex"}'))
print('{"name":"alex"}')
  • all: iterables in all True is True
  • any: iterables, there is a True True

Guess you like

Origin www.cnblogs.com/llwwhh/p/11317997.html