day17 内置函数

一、内置函数接下来,我们就一起来看看python里的内置函数

分类图

1、作用域函数  

基于字典的形式获取局部变量和全局变量
globals()——获取全局变量的字典
locals()——获取执行本方法所在命名空间内的局部变量的字典
2、字符串代码执行
# eval() 将字符串类型的代码执行并返回结果
# print(eval('1+2+3+4'))exec()将自字符串类型的代码执行
# print(eval("3 > 1" ))  # 注意不能接受一个等式,返回的是一个值,如果是输入内容则返回,一般用于简单求值表达式用eval
# exec("print('hello,world')")
#exec 将自字符串类型的代码执行     没有返回值。格式和正常代码格式一样不能随意修改
# exec( '''a = input('请输入》》')
# if a != 1:
#     print(a)''')
# a = 123
# print(a)
# compile  将字符串类型的代码编译  # 编译成code类
 #交互语句用single
# code3 = 'name = input("please input your name:")'
# compile3 = compile(code3,'','exec')

#name #执行前name变量不存在
# Traceback (most recent call last):
#   File "<pyshell#29>", line 1, in <module>
#     name
# NameError: name 'name' is not defined
# exec(compile3) #执行时显示交互命令,提示输入
# print(name)
# name #执行后name变量有值
# "'pythoner'"
# a = "input('>>>')"
# b = compile(a,'','exec')
# print(b,type(b),sep='**',)
# exec (b)
# cmp_code = compile('print("single")','','single')
# exec(cmp_code)

# eval_code='1+2'            #如果用模式用single 将变成会返回值的一种 其他的都变成 有返回值  经过compile
# cmp_code2=compile(eval_code,'','eval')
# print(exec(cmp_code2))

#single  单一交互语句,多个交互语句报错
c = '''input('>>>')
input('>>>')'''
cmp_code=compile(c,'','single')
exec(cmp_code)
# multiple statements found while compiling a single statement

    其他:

#1.迭代器相关range  ,next(),iter(),send
#可调用函数 callable 返回True or Flase
#ctrl + 左键单击 :pycharm
#5.help:包含所有方法名以及他的使用方法 —— 不知道用法
#6.dir:只包含方法名 —— 想查看某方法是否在这个数据类型中
#7.import 函数
import time
import os
import urllib.request
import os.path
import copy
#8.open 文件操作
# f = open('文件名','w',encoding='utf-8')
#打开模式:r、w、a、rb、wb,ab
#9.内存地址 id()
#10 hash地址 hash()
#11#数据的存储和查找
#模块:hashlib
# {'k':'v'}
# [1,2,3,4,5,6,]

# hash([1,2,3,4,5,6,])
#hash 判断一个数据类型是否可以hash
#在一个程序执行的过程中,对同一个值hash的结果总是不变
#多次执行,对同一个值的hash结果可能改变
#12 用户交互 input() 返回的值是字符串
# with open('a','a') as f:
#     f.write('asdf')
# 13 pow 幂运算
# print(pow(3,2.5)) #15.588457268119896
#14 slice
# l = [1,2,23,213,5612,342,43]
# sli = slice(1,5,2)   #实现了切片的函数
# print(l[sli])
# 15 bytearray
# ret = bytearray('alex',encoding='utf-8')  #对比较长的字符串做修改的时候,指定某一处进行修改,不会改变这个bytearry的内存地址
# print(id(ret))
# print(ret[0])
# ret[0] = 65
# print(ret)
# print(id(ret))
#16 memoryview 内存中的切片  所谓内存查看对象,是指对支持缓冲区协议的数据进行包装,在不需要复制对象基础上允许Python代码访问。
#. Python内置对象中支持缓冲区协议的对象有bytes和bytearray。
#切片
# l = [1,2,3,4,5,60]
# l[1:3]
# ret = memoryview(bytes('你好',encoding='utf-8'))
# print(ret)
# print(len(ret))
# print(ret[:3])
# print(bytes(ret[:3]).decode('utf-8'))
# print(bytes(ret[3:]).decode('utf-8'))
# 17 repr  显示表现形式  ascii  显示二进制表现形式
print(repr(1))
print(repr('1'))
print('name : %r'%('金老板'))
print(ascii(1))
print(ascii('1'))
print('name : %r'%('金老板'))
import time
for i in range(0,101,2):
     time.sleep(0.1)
     char_num = i//2      #打印多少个'*'
     per_str = '
%s%% :33[1;40m %s33[0m
' % (i, '*' * char_num) if i == 100 else '
 %s%% : 33[1;40m%s33[0m'%(i,'*'*char_num)
     print(per_str,end='', flush=True)
打印进度条

  

  

原文地址:https://www.cnblogs.com/zjchao/p/7811340.html