zoukankan      html  css  js  c++  java
  • python学习Day14--内置函数

    【主要内容】

    整理内置函数思维导图

    【代码】

    1、内置函数1

      1 # def func():
      2 #     a = 10
      3 #     print(locals())  # 当前作用域中的内容
      4 #     print(globals())  # 全局作用域中的内容
      5 #     print("今天内容很简单")
      6 # func()
      7 
      8 
      9 # for i in range(20,15,-3):
     10 #     print(i)
     11 
     12 # lst = ["大阳哥", "喜欢", "私密的徒步"]
     13 # it = iter(lst)  #  __iter__()
     14 # print(it.__next__())
     15 # print(next(it)) # __next__()
     16 # print(next(it))
     17 # print(next(it))
     18 
     19 # print("李嘉诚", "黄花菜", "马云", sep="*", end="大阳哥") # seperator
     20 # input("提示语")
     21 
     22 # hash算法:
     23     # 目的是唯一性
     24     # dict 查找效率非常高, hash表.用空间换的时间 比较耗费内存
     25 
     26 # s = "大阳哥"
     27 # print(hash(s))
     28 # a = 12
     29 # print(hash(a))
     30 
     31 # lst = [1,2,3,4,5,6,7]
     32 # # print(hash(lst))    # 列表是不可哈希的
     33 # print(hash((1,)))
     34 # print(hash("呵呵"))
     35 # print(hash("哈哈"))
     36 
     37 # 让用户输入一个要导入的模块
     38 # import os
     39 # name = input("请输入你要导入的模块:")
     40 # __import__(name)    # 可以动态导入模块
     41 
     42 # print(help(str))
     43 
     44 # print(dir(str))
     45 
     46 # a = 11.25
     47 # print(type(a))
     48 
     49 # bin() 把一个十进制的数, 转换成二进制
     50 # print(bin(10))  # 二进制
     51 # print(hex(10))  # 十六进制
     52 # print(oct(10))  # 八进制
     53 
     54 
     55 # a = 10
     56 # print(callable(a))
     57 #
     58 # def func():
     59 #     print("马化腾")
     60 # print(callable(func))   # 函数是可以被调用的
     61 
     62 # s = input("请输入a+b:")
     63 # print(eval(s))  # 可以动态的执行代码. 代码必须有返回值
     64 
     65 # s = "25*4"
     66 # a = eval(s) # 运算
     67 # print(a)
     68 
     69 # s = "for i in range(10): print(i)"
     70 # a = exec(s) # exec 执行代码不返回任何内容
     71 # print(a)
     72 
     73 # 动态执行代码
     74 # exec("""
     75 # def func():
     76 #     print(" 我是周杰伦")
     77 # """ )
     78 # func()
     79 
     80 # code1 = "for i in range(10): print(i)"
     81 # com = compile(code1, "", mode="exec")   # compile并不会执行你的代码.只是编译
     82 # exec(com)   # 执行编译的结果
     83 #
     84 # code2 = "5+6+7"
     85 # com2 = compile(code2, "", mode="eval")
     86 # print(eval(com2))
     87 #
     88 # code3 = "name = input('请输入大阳哥的名字:')"
     89 # com3 = compile(code3, "", mode="single")
     90 # exec(com3)
     91 # print(name)
     92 
     93 
     94 # print(abs(-2))  # 绝对值
     95 # print(abs(2))
     96 
     97 # print(divmod(20,3)) # 求商和余数
     98 
     99 # print(round(4.50))   # 五舍六入 => 四舍五入
    100 
    101 # print(pow(10,2,3))  # 如果给了第三个参数. 表示最后取余
    102 
    103 # print(sum([1,2,3,4,5,6,7,8,9,10]))  # 求和
    104 
    105 # lst = "你好啊"
    106 # it = reversed(lst)   # 不会改变原列表. 返回一个迭代器, 设计上的一个规则
    107 # print(list(it))
    108 
    109 # lst = [1, 2, 3, 4, 5, 6, 7]
    110 # print(lst[1:3:1])
    111 #
    112 # s = slice(1, 3, 1)  #  切片用的
    113 # print(lst[s])
    114 
    115 
    116 # name = "你好. 
    我叫%s周润发" % "李嘉诚"
    117 # print(name)
    118 # print(repr(name))   # 原样输出,过滤掉转义字符 
     	 
     不管百分号
    119 
    120 # print(ord('a')) # 97, 返回字母a在编码表中的码位
    121 # print(ord('中')) # 20013 中国的中字在编码表中的位置
    122 
    123 # print(chr(65)) # 已知码位. 计算字符
    124 # print(chr(20018))
    125 #
    126 # for i in range(65536):
    127 #     print(chr(i), end=" ")
    128 
    129 # print(ascii("房"))
    130 
    131 # s = "李嘉诚的爸爸"
    132 # a = s.encode("UTF-8")
    133 # print(a)
    134 # print(a.decode("GBK"))
    135 
    136 
    137 # bs = bytes("大阳哥今天很厉害", encoding="utf-8")
    138 # print(bs.decode("utf-8"))
    139 
    140 # ret =  bytearray("alex" ,encoding ='utf-8')
    141 # print(ret[0])
    142 # ret[0] = 65
    143 # print(str(ret))
    144 
    145 # s = memoryview("麻花藤".encode( "utf-8")) # 查看内存
    146 # print(s)

    2、内置函数2

     1 # s = "我叫王尼玛"
     2 # print(format(s, "^20"))
     3 # print(format(s, "<20"))
     4 # print(format(s, ">20"))
     5 
     6 # print(format(3, 'b' ))   # ⼆进制
     7 # print(format(97, 'c' ))   # 转换成unicode字符
     8 # print(format(11, 'd' ))   # ⼗进制 %d
     9 # print(format(11, 'o' ))   # ⼋进制  8
    10 # print(format(11, 'x' ))   # ⼗六进制(⼩写字⺟)
    11 # print(format(11, 'X' ))   # ⼗六进制(⼤写字⺟)
    12 # print(format(11, 'n' ))   # 和d⼀样
    13 # print(format(11))   # 和d⼀样
    14 
    15 # print(format(123456789, 'e' ))   # 科学计数法. 默认保留6位小数
    16 # print(format(123456789, '0.2e' ))   # 科学计数法. 保留2位小数(小写)
    17 # print(format(123456789, '0.2E' ))   # 科学计数法. 保留2位小数(大写)
    18 # print(format(1.23456789, 'f' ))   # 小数点计数法. 保留6位小数
    19 # print(format(1.23456789, '0.2f' ))   # 小数点计数法. 保留2位小数
    20 # print(format(1.23456789, '0.10f'))   # 小数点计数法. 保留10位小数
    21 # print(format(1.23456789e+3, 'F'))   # 小数点计数法. 很大的时候输出 INF
    22 
    23 # lst = ["蛋1", "蛋2", "蛋3", "蛋4"]
    24 # for i in range(len(lst)):
    25 #     print(i)
    26 #     print(lst[i])
    27 
    28 # for index, el in enumerate(lst, 100):    # 把索引和元素一起获取,索引默认从0开始. 可以更改
    29 #     print(index)
    30 #     print(el)
    31 
    32 
    33 # print(any([0, "哈哈", "馒头", True]))
    34 
    35 
    36 lst1 = ["施瓦辛格", "泰达米尔", "阿米尔汗", "威震天"]
    37 lst2 = ["终结者", "英雄联盟", "我的个神啊", "变形金刚"]
    38 lst3 = [10000000, 3, 10, 55,66]
    39 for el in zip(lst1, lst2, lst3):
    40     print(el)

    3、今日练习

     1 '''
     2 #**************今日练习******************
     3 # print
     4 print("123","345","5433",sep="_")
     5 print("123","345","5433",sep="_",end='大秧歌') # end末尾接什么(默认
    )
     6 
     7 # 动态导入模块__import__
     8 name=input("请输入你要导入的模块:")
     9 __import__(name)
    10 
    11 # callable可否被调用
    12 def func():
    13     print("明天,你好")
    14 print(callable(func))  # True
    15 
    16 # enumerate 把索引和元素一起获取
    17 lst=["蛋1","蛋2","蛋3","蛋4"]
    18 # for i in range(len(lst)):
    19 #     print(i)
    20 #     print(lst[i])
    21 # for a in enumerate(lst):
    22 #     print(a)
    23 dic={}
    24 for index,a in enumerate(lst):
    25     dic[index+1]=a
    26 print(dic)
    27 '''
  • 相关阅读:
    Python运算符,基本数据类型
    Python2 错误记录1File "<string>", line 1, in <module> NameError: name 'f' is not defined
    用户登录三次练习
    跟我一起学Python-day1(条件语句以及初识变量)
    vim operation
    步步为营-28-事件本质
    步步为营-27-事件
    步步为营-26-多播委托
    步步为营-25-委托(比大小)
    步步为营-24-委托
  • 原文地址:https://www.cnblogs.com/fengxb1213/p/12290840.html
Copyright © 2011-2022 走看看