day 17 内置函数

1、print 的应用

print(1,2,3,4,5,sep="*")     #sep是指定多个要打印的内容之间的分隔符
# 1*2*3*4*5
print(1,2,sep=",")      #print("%s,%s"%(1,2))
# 1,2,3
print(1,end="")
print(2)
#12
print(1,end=",")
print(2)
# 1,2
f=open("a","w")
print("abc
",file=f)  #将abc写入文件a
print(2)
print的各类应用

进度条的例子

import time
for i in range(0,101,2):
    time.sleep(0.2)
    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="",flush=True)
print("再见")
进度条的例子1
import time
for i in range(0,101,2):
    time.sleep(0.2)
    char_num=i//2
    per_str="
%s%%:%s
"%(i,"*"*char_num)if i ==100 else"
%s%%:%s"%(i,"*"*char_num)
    print(per_str,end="",flush=True)
进度条 (三元运算符)

字体颜色的输出:

print("33[显示方式;前景色;背景色m")    格式
print("33[显示方式;前景色;背景色m") #格式
print("33[4;32;41m金老师33[0m")  
print("33[31;1mHello world,how are you33[0m")#Hello world,how are you
输出颜色字体的例子

2、字符串类型的代码执行

  (1)exec

codel="for i in range(0,10):print(i)"
compilel=compile(codel,"","exec")
exec(compilel)
流程性语句

  (2)eval

code2="1+2+3+4"
compile2=compile(code2,"","eval")
print(eval(compile2))
简单求值

   (3)single

code3=name=input("please input your name:")
compile3=compile(code3,"","single")
# name执行前name变量不存在
exec(compile3)
print(name)
View Code

 

 3、其他 

(1)帮助  help 

  ctr +左键单击:pycharm

 help:包含所有方法名以及其他的使用方法—不知道用法

 dir :只包含方法名——想查看某方法是否在这个数据类型中

 (2)hash 判断一个数据类型是否可以hash

     在一个程序执行的过程中,对同一个值hash的结果总是不变

   在多次执行,对同一个值的hash结果可能改变


原文地址:https://www.cnblogs.com/number1994/p/8004844.html