zoukankan      html  css  js  c++  java
  • 内置函数(一)

    数据类型相关:

    type基础数据类型

    1 print(type(list))
    2 #<class 'type'>
    type

    type(o) 返回变量o的数据类型

    内存相关:

    id(o) o是参数,返回一个变量的内存地址

    hash(o) o是参数,返回一个可hash变量的哈希值,不可hash的变量被hash之后会报错。

    1 t = (1,2,3)
    2 l = [1,2,3]
    3 print(hash(t))  #可hash
    4 print(hash(l))  #会报错
    5 
    6 '''
    7 结果:
    8 TypeError: unhashable type: 'list'
    9 '''
    hash实例

    range

     1 for i in range(10):
     2     print(i)
     3 # 0
     4 # 1
     5 # 2
     6 # 3
     7 # 4
     8 # 5
     9 # 6
    10 # 7
    11 # 8
    12 # 9
    13 
    14 a = [i for i in range(10)]
    15 print(a)
    16 #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    17 
    18 a = (i for i in range(10))
    19 print(list(a))
    20 #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    range

    字符串类型:(eval和exec比较重要)

    1.eval() 将字符串类型的代码执行并返回结果

    1 print(eval('1+2+3'))
    2 #6
    3 
    4 print(eval('[1,2,3]'))
    5 #[1, 2, 3]
    6 
    7 print(eval('1'))
    8 #1
    eval

    2.exec()将自字符串类型的代码执行,结果不放回

    1 print(exec('1'))
    2 #None表示没有返回值
    View Code

    3.compile  将字符串类型的代码编译。代码对象能够通过exec()语句来执行或者eval()进行求值。

     1 code1 = 'for i in range(0,10): print (i)'
     2 compile1 = compile(code1,'','exec')
     3 eval(compile1)
     4 #0
     5 #1
     6 #2
     7 #3
     8 #4
     9 #5
    10 #6
    11 #7
    12 #8
    13 #9
    14 
    15 exec(compile1)
    16 #0
    17 #1
    18 #2
    19 #3
    20 #4
    21 #5
    22 #6
    23 #7
    24 #8
    25 #9
    compile
    1 #流程语句使用exec
    2 code1 = 'for i in range(0,5): print (i)'
    3 compile1 = compile(code1,'','exec')
    4 exec(compile1)
    5 #0
    6 # 1
    7 # 2
    8 # 3
    9 # 4
    流程语句使用exec
    1 #简单求值表达式用eval
    2 code2 = '1 + 2 + 3 + 4'
    3 compile2 = compile(code2,'','exec')
    4 exec(compile2)
    简单求值表达式用eval没有返回值这时
    1 #交互语句用single
    2 #没有定义是打印name:NameError: name 'name' is not defined
    3 code3 = 'name = input("please input your name:")'
    4 compile3 = compile(code3,'','exec')
    5 eval(compile3)
    6 print(name)
    7 #please input your name:1111
    8 #1111
    #交互语句用single

    input() 输入

    1 s = input("请输入内容 : ")  #输入的内容赋值给s变量
    2 print(s)  #输入什么打印什么。数据类型是str
    input

    print() 输出

     1 def progress(percet,width=50):
     2     if percet >=1:
     3         percet =1
     4     show_str = ('[%%-%ds]' % width) % ('#'*int(width*percet))
     5     print('
    %s %d%%' %(show_str,int(100*percet)),end='')
     6 recv_size = 0 #初始大小
     7 total_size = 102321#总大小
     8 while recv_size < total_size:
     9     time.sleep(0.1)
    10     recv_size+=1024
    11     progress(recv_size/total_size)
    打印进度条
    1 import time
    2 for i in range(0,101,2):  
    3      time.sleep(0.1)
    4      char_num = i//2      #打印多少个'*'
    5      per_str = '
    %s%% : %s
    ' % (i, '*' * char_num) if i == 100 else '
    %s%% : %s'%(i,'*'*char_num)
    6      print(per_str,end='', flush=True)
    7 #小越越  : 
     可以把光标移动到行首但不换行
    打印进度条

     文件操作相关

    open()  打开一个文件,返回一个文件操作符(文件句柄)

    操作文件的模式有r,w,a,r+,w+,a+ 共6种,每一种方式都可以用二进制的形式操作(rb,wb,ab,rb+,wb+,ab+)

    可以用encoding指定编码.

    数字类型

    abs绝对值

    1 print(abs(-5))
    2 print(abs(5))
    3 #5
    4 #5
    abs

    divmod商余函数

    1 print(divmod(100,40))
    2 print(divmod(10,2))
    3 #(2, 20)
    4 #(5, 0)
    商余函数

    round默认取整,小数精确 会四舍五入

    1 print(round(3.1514,1))#默认取整,小数精确 会四舍五入
    2 print(round(5.3222222,3))#默认取整,小数精确 会四舍五入
    3 #3.2
    4 #5.322
    默认取整,小数精确 会四舍五入
    pow幂运算
    1 print(pow(3,2))     # (3**3)
    2 print(pow(3,3,3)) # (2**3)%5
    3 print(pow(3,3,2)) # (3**3)%2
    4 #9
    5 #0
    6 #1
    幂运算
  • 相关阅读:
    张硕组兽棋测试报告(整理——郑云飞)
    5.6任务进展情况(整理——郑云飞)
    5.5任务进展情况(整理——祁子梁)
    android-读取MediaProvider
    android-effect
    window下载android 最新源码
    android-mediaplayer播放
    android-基础编程-Notification
    android-基础编程-ViewPager
    android-glsurfaceview Activity框架程序
  • 原文地址:https://www.cnblogs.com/zhaojingyu/p/8934808.html
Copyright © 2011-2022 走看看