强化整理初级部分内置函数

1 range(起始位置,终止位置,步长)

range(终止位置)

range(起始,终止位置)

range(起始,终止,步长)

range(5) [0,4] [0,5)

range(1,5) [1,4] [1,5)

range(1,10,2) [1,3,5,7,9]

range(0,10,2) [0,2,4,6,8]

2 next(迭代器) 是内置函数

next 是迭代器的方法

g.next() 带双下划线的魔术方法一般情况下不直接用

 # next(g)    之前所有的__next__都应该替换成next(g)
 # *带双下划线的所有的方法都可能和内置的函数有千丝万缕的联系

3 iter(可迭代的)

# __iter__
# 迭代器 = 可迭代的.__iter__()
# 迭代器 = iter(可迭代的)

其他

4 open(‘文件名’) 跟着操作系统走的

# 打开模式 默认是r
# 编码 默认是 操作系统的默认编码

# 打开模式 : r w a rb wb ab
# 编码 : utf-8(100%)
#         gbk

5 input(‘字符串数据类型的参数,提醒用户你要输入的内容’)

# python2
    # input() 还原你输入的值的数据类型
    # raw_input = py3.input
# python3
    # input() 输入的所有内容都是字符串类型
    # 阻塞: 等待某件事情发生,如果不发生一直等着
# input的返回值就是用户输入的内容
    # 输入的内容 = input('提示')

6 print(要打印的内容1,要打印的内容2,要打印的内容3,sep = ‘分隔符’,end = ‘结束符’)

# print(123)
# print('abc')
# print(123,'abc')
# print(123,'abc','aaa',sep = '|')
# print(123,'abc','aaa',end = '@')
# print(123,'abc','aaa',end = '@')
# print(123,'abc')
# f = open('file','w')
# print(123,'abc',file=f)   # print的本质 就是写文件 这个文件是pycharm的屏幕
# f.close()
# sep=' ' seperator

import time # 导入别人写好的代码

def func():

for i in range(0,101,2):

time.sleep(0.1) # 每一次在这个地方阻塞0.1,0.1秒结束就结束阻塞

char_num = i//2

if i == 100:

per_str = ‘\r%s%% : %s\n’ % (i, ‘*’ * char_num)

else:

per_str = ‘\r%s%% : %s’ % (i, ‘*’ * char_num)

print(per_str,end = ‘’)

func()

print(‘下载完成’)

# 解释过程:
# per_str = '\r%s%% : %s' % (10, '*' * 10)
# print(per_str,)
# time.sleep(1)
# per_str = '\r%s%% : %s' % (20, '*' * 20)
# print(per_str,end='')

7 hash函数

# 哈希 可哈希(不可变数据类型) 不可哈希(可变数据类型)
# 哈希是一个算法,导致了字典的快速寻址
# 'asjgkgfk'  复杂的算法 得到一个数字
# () --> 数字
# 数字 --> 数字
# 所有的数据要想得到不变的hash值,必须是一个不可变的数据类型

dir 函数 : 特殊的需求 / 研究或者了解一个新的数据类型 / 面向对象之后会介绍新的数据类型

print(dir(builtins)) # 内置的名字

‘’’
[‘ArithmeticError’, ‘AssertionError’, ‘AttributeError’, ‘BaseException’, ‘BlockingIOError’, ‘BrokenPipeError’,
‘BufferError’, ‘BytesWarning’, ‘ChildProcessError’, ‘ConnectionAbortedError’, ‘ConnectionError’, ‘ConnectionRefusedError’,
‘ConnectionResetError’, ‘DeprecationWarning’, ‘EOFError’, ‘Ellipsis’, ‘EnvironmentError’, ‘Exception’,
‘False’, ‘FileExistsError’, ‘FileNotFoundError’, ‘FloatingPointError’, ‘FutureWarning’, ‘GeneratorExit’,
‘IOError’, ‘ImportError’, ‘ImportWarning’, ‘IndentationError’, ‘IndexError’, ‘InterruptedError’,
‘IsADirectoryError’, ‘KeyError’, ‘KeyboardInterrupt’, ‘LookupError’, ‘MemoryError’, ‘ModuleNotFoundError’,
‘NameError’, ‘None’, ‘NotADirectoryError’, ‘NotImplemented’, ‘NotImplementedError’, ‘OSError’, ‘OverflowError’,
‘PendingDeprecationWarning’, ‘PermissionError’, ‘ProcessLookupError’, ‘RecursionError’, ‘ReferenceError’,
‘ResourceWarning’, ‘RuntimeError’, ‘RuntimeWarning’, ‘StopAsyncIteration’, ‘StopIteration’, ‘SyntaxError’,
‘SyntaxWarning’, ‘SystemError’, ‘SystemExit’, ‘TabError’, ‘TimeoutError’, ‘True’, ‘TypeError’,
‘UnboundLocalError’, ‘UnicodeDecodeError’, ‘UnicodeEncodeError’, ‘UnicodeError’, ‘UnicodeTranslateError’,
‘UnicodeWarning’, ‘UserWarning’, ‘ValueError’, ‘Warning’, ‘WindowsError’, ‘ZeroDivisionError’,
build_class’, ‘debug’, ‘doc’, ‘import’, ‘loader’, ‘name’, ‘package’, ‘spec’,
‘abs’, ‘all’, ‘any’, ‘ascii’, ‘bin’, ‘bool’, ‘bytearray’, ‘bytes’, ‘callable’, ‘chr’, ‘classmethod’,
‘compile’, ‘complex’, ‘copyright’, ‘credits’, ‘delattr’, ‘dict’, ‘dir’, ‘divmod’, ‘enumerate’, ‘eval’,
‘exec’, ‘exit’, ‘filter’, ‘float’, ‘format’, ‘frozenset’, ‘getattr’, ‘globals’, ‘hasattr’, ‘hash’, ‘help’,
‘hex’, ‘id’, ‘input’, ‘int’, ‘isinstance’, ‘issubclass’, ‘iter’, ‘len’, ‘license’, ‘list’, ‘locals’, ‘map’,
‘max’, ‘memoryview’, ‘min’, ‘next’, ‘object’, ‘oct’, ‘open’, ‘ord’, ‘pow’, ‘print’, ‘property’, ‘quit’,
‘range’, ‘repr’, ‘reversed’, ‘round’, ‘set’, ‘setattr’,‘slice’, ‘sorted’, ‘staticmethod’, ‘str’, ‘sum’,
‘super’, ‘tuple’, ‘type’, ‘vars’, ‘zip’]’’’

print(dir(123))

8 eval() 可以将字符串数据类型的python代码执行,通过拼接字符串的方式来执行不同的代码–简化代码

# eval\exec这个函数 不能直接操作文件当中读进来的 网络上传进来 用户输入的

eval(‘print(1+2+3+4)’) # 有返回值

ret = eval(‘1+2/3*4’)

print(ret)

字符串 -> 其他数据类型的转换

f = open(‘userinfo’)

content = f.read()

print(content,type(content))

ret = eval(content)

print(ret,type(ret))

print(ret[0])

员工信息表

def get_info(f):

if eval(‘33 %s 20’%f):

print(‘是符合条件的行’)

if ‘>’:

get_info(’>’)

if ‘<’ :

get_info(’<’)

9 exec()

exec(‘print(1+2+3+4)’) # 没有返回值

ret = exec(‘1+2/3*4’)

print(ret)

exec(‘for i in range(200):print(i)’)

10 compile 能够节省时间工具

先编译 python -编译-> 字节码(bytes) -解释-> 机器码 0101010100101

先整体编译

code1 = ‘for i in range(0,10): print (i)’ # 这是一句代码 字符串

compile1 = compile(code1,’’,‘exec’) # 预编译 python-> 字节码

exec (compile1) # 解释

exec (compile1)

exec (compile1)

exec (compile1)

exec (compile1)

exec(code1) # 编译+解释

exec(code1)

exec(code1)

exec(code1)

exec(code1)

code3 = ‘name = input(“please input your name:”)’

compile3 = compile(code3,’’,‘single’)

exec(compile3)

print(name)

help() 帮助你了解python的

# 方式一
# 输入help() 进入帮助页面,输入数据类型,帮助我们打印具体的帮助信息
# '123'.startswith()
# 输入q退出帮助

# 方式二
# print(help(str))
# print(help('abc'))

callable() 判断某一个变量是否可调用

def call(arg):

if callable(arg):

arg()

else:

print(‘参数不符合规定’)

def func():

print(‘in func’)

return 1

func2 = 1234

call(func)

把每一个函数是做什么的整理出来

并且课上的小例子 再敲一遍

call(func2)

print(callable(func))

print(callable(func2))

猜你喜欢

转载自blog.csdn.net/HZY199321/article/details/83832281