day16 Pyhton学习

1.range(起始位置)

  range(终止位置)

  range(起始,终止位置)

  range(起始,终止,步长)

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 gbk

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

  python2 

    input() 还原你输入的值的数据类型

    raw_input = py3.input

  python3

    input() 输入的所有内容都是字符串类型

    阻塞:等待某件事情发生,如果不发生一直等着

  input的返回值就是用户输入的内容

    输入的内容 = input("提示")

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

  print(123,"abc","阿达",sep = "|")

  f= open("file","w")

  print(123,"abc",file=f)  #print的本质,就是写文件,这个文件是pycharm的屏幕

# 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 = '
%s%% : %s
' % (i, '*' * char_num)
#          else:
#              per_str = '
%s%% : %s' % (i, '*' * char_num)
#          print(per_str,end = '')
# func()
# print('下载完成')

7.hash函数

  哈希 可哈希(不可变数据类型) 不可哈希(可变数据类型)

  哈希是一个算法,导致了字典的快速寻址

  "addafaf" 通过hash函数,得到一个数字

  不可变的数据可以通过hash函数的到hash值

8.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']'''

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

# evalexec这个函数 不能直接操作文件当中读进来的 网络上传进来 用户输入的
# 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) -解释 ->机器码0101010101

# 先整体编译
# 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)
11 help() 函数
#
help() 帮助你了解python的 # 方式一 # 输入help() 进入帮助页面,输入数据类型,帮助我们打印具体的帮助信息 # '123'.startswith() # 输入q退出帮助 # 方式二 # print(help(str)) # print(help('abc'))

12. 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))

 

  

原文地址:https://www.cnblogs.com/pythonz/p/9927483.html