zoukankan      html  css  js  c++  java
  • Python基础『一』

    内置数据类型

    数据名称 例子
    数字: Bool,Complex,Float,Integer True/False; z=a+bj; 1.23; 123
    字符串: String '123456'
    元组: Tuple (123456)
    集合: Set
    字典: Dict {1:'123456'}
    文件:File
    数组:Array
    字节数组:Bytearray
    graph TD; 存储方式划分-->可变对象; 存储方式划分-->不可变对象; 可变对象-->字典; 可变对象-->列表; 可变对象-->可变集合; 不可变对象-->数字; 不可变对象-->字符串; 不可变对象-->元组; 不可变对象-->不可变集合;
    graph TD; 操作划分-->数字; 操作划分-->序列; 操作划分-->映射; 数字-->整数; 数字-->浮点数; 数字-->二进制数; 数字-->分数; 数字-->...; 序列-->字符串; 序列-->列表; 序列-->元组; 映射-->字典;

    数字数据

    常用内建函数 例子
    abs(x):取得传递的值的绝对值 abs(-2) = 2
    pow(x,y):进行平方计算 pow(2,2) = 4
    max(x[,x1,x2]):求多个传递的值的最大值 max(1,22,5,3) = 22
    min(x[,x1,x2]):求多个传递的值的最小值 min(1,2,3,4,6) = 1
    sum([x1,x2[,x3],y]):计算传递的多个值的和 sum([1,2,3],2) = 8 ; sum([1,2,3]) = 6
    常用math模块 例子
    math.sqrt():进行开方运算 math.sqrt(4) = 2
    math.pi:数字π 3.1415926...
    math.sin():得到数据参数内的正弦函数 math.sin(1) = 0.8414709848078965

    Bool函数:

    在python2中是内建变量,而在python中增加了保留字之后,但True = 1 ,False = 0保留下来,所以判断真或假,False有三种情况一种是值为空,数字为0,等于None

    None表示空值,它是一个特殊Python对象, None的类型是NoneType.None在Python解释器启动时自动创建, 解释器退出时销毁.

    所以一个解释器进程中只有一个None存在

    所以 输入: None is None and None == None; 输出: True

    ​ 输入: False == None; 输出: False

    • None不支持任何运算也没有任何内建方法

    • None和任何其他的数据类型比较永远返回False

    • None有自己的数据类型NoneType,不能创建其他NoneType对象(它只有一个值None)

    • None与0,空列表,空字符串不一样

    import sys
    a = sys.getsizeof(None)
    b = sys.getsizeof(False)
    c = sys.getsizeof(0)
    d = sys.getsizeof(1)
    print(a,b,c,d)
    
    # 16 24 24 28
    # int为长整型,sys.getsizeof(0)数组元素为0.此时占用24字节(PyObject_VAR_HEAD的大小),sys.getsizeof(1)需使用一个元素所以为28
    
    import operator as op
    op.eq(0,False)
    op.eq('',False)
    op.eq(None,False)
    
    # True
    # False
    # False
    

    内置数据运算

    操作 操作符
    算术运算 **+, -, *, /, //, %, ****
    逻辑运算 and, or, not
    比较运算 ==, !=, >, <, >=, <=,
    位运算 in, not in
    成员运算 is, is not
    赋值运算
    操作符 例子(s=1) 结果
    = s = 4 4
    += s += 4 5
    -= s -= 4 -3
    *= s *= 4 4
    /= s /= 4 0.25
    %= s %= 4, 1
    //= s //= 4 0
    **= s **=4 1

    类型转换

    操作 例子 结果
    int(x[,base]) int('3'); int('1101',2) 3; 13
    float(x) float(3) 3.0
    chr(x) chr(3) 'x03'
    hex(x) hex(3) '0x3'
    oct(x) oct(3) '0o3'

    字符串数据

    转义字符

    转义字符 描述 转义字符 描述
    续航符 反斜杠
    ' 单引号 " 双引号
    a 响铃  退格
    e 转义 00
    执行 v 纵向制表符
    横向制表符 回车
    f 换页 yy 八进制数
    xyy 十六进制数 other 其他字符以普通格式输出

    字符串方法

    内建函数
    内建函数 例子(s = 'sos') 结果
    find() s.find('s') 0
    join() s.join('abc'); s.join('a') 'asosbsosc'; 'a'
    len() len(s) 3
    count() s.count('s') 2
    split() s.split('o') ['s', 's']
    replace() s.replace('o','a') 'sas'
    dir() dir(s) ["__ add __",< br/>...]
    字符串转换
    转换 例子(today = datetime.date.today()) 结果
    str() str(today) '2019-11-23'
    repr() repr(today) 'datetime.date(2019, 11, 23)'

    (str是转换成适合成人类阅读的,repr是转换成编译器使用的,列表和字典总是调用repr)

    索引分片
    索引,分片 例子 str='hello_world' 结果
    str[i] str[2] 'l'
    str[i:j:k] str[1:7:2] 'el_'

    i为起始位置,j为终止位置,k为步长,第一位始终会被读取,截取长度为前闭后开,i默认值为0,j不限制则默认全部,空格也会占一位

    格式化

    %[(name)] [flags] [ width ] [.pression]typecode

    格式化表达式 例子 demo={'test':'1.2242'} 结果 value%demo
    name:指定参数名,用于字典参数格式化 value=' this is %(test)s' ' this is 1.2242'
    flags:+/-表示左右对齐 value=' this is %(test)-10s ' 'this is 1.2242 '
    表示字符串宽度 value=' this is %(test)10s ' 'this is 1.2242 '
    precision:表示浮点数后的有效位数 value=' this is %(test).3f ' 'this is 1.224 '
    typecode:确定转换的数字类型

    {fieldname!confersionflag:formatspec}

    格式化方法 例子demo={'s':12.222}
    fieldname: 指定的数字或关键字,位置参数,关键字参数,下标/属性 value='this is {test} '.format(test = demo['s'])
    confersionflag: r/a/s, repr,ascii.str value='this is {test!r} '.format(test = demo['s'])
    formatspec:

    [[fill] align [sign] [width] [.precision] [typecode]]

    格式化方法参数'formatspec' 作用 例子
    fill 填充 {test:0^10}: this is 0012.22200
    align 填充对齐方式与fill一同出现</>/^,右/左/中 {test!r:0>10}: this is 000012.222
    sign 数字符号表示正负,参数为字符串不可用,相同显示正确结果 {test:+}: this is +12.222
    width 表示字符串宽度 {test:10}: this is 12.222
    .precision 显示浮点数保留几位 {test:10.2f}: this is 12.22
    typecode
    typecode 描述
    s 获取传入对象的 __ str __
    r 获取传入对象的 __ repr __
    c 整数,转换为Unicode值
    o 整数,转换为八进制
    x 整数,转换为十六进制
    d 整数,转换为十进制
    f 浮点数,十进制显示

    列表数据

    • 可以存储任何对象
    • 支持所有序列操作
    • 支持原处修改,可变对象
    • 长度可变,异构,可任意嵌套

    本质是对象的引用组成的数组

    列表操作

    操作 例子 s=[1,2,3,4,6] 结果
    len(): 读取长度 len(s) 5
    sum(): 求和 sum(s) 16
    max()/min(): 最大最小值 min(s) 1
    append(): 追加元素 s.append(7) [1, 2, 3, 4, 6, 7]
    extend(): 追加元素(在原列表上追加新列表) s.extend([1,2,3]) [1, 2, 3, 4, 6, 7, 1, 2, 3]
    insert(x,y): 插入元素(在x位之后插入y元素) s.insert(4,5) [1, 2, 3, 4, 5, 6, 7]
    count(): 元素统计,返回列表中出现的次数 s.count(4) 1
    index(): 返回元素的索引值 s.index(6) 5
    pop(): 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 s.pop(-2) 6
    remove(): 移除列表中某个值的第一个匹配项 s.remove(2) [1, 3, 4, 5, 7]
    del(): 删除一个或多个元素(根据索引删除) del s[1] [1, 4, 5, 7]
    sort(cmp=None,key=None,reverse=False): 用于对原列表进行排序(reverse默认false为升序) s.sort(reverse=True) [7, 5, 4, 1]

    列表推导式

    结构是在一个中括号里包含一个表达式,然后是一个for语句,然后是 0 个或多个 for 或者 if 语句

    t = [x for x in range(1,51)]
    [1,2,3...,49,50]
    
    import random
    s = [random.randint(1,100) for i in range(10)]
    [25, 83, 28, 68, 28, 84, 16, 45, 87, 73]
    

    元组数据

    • 不可变序列(不可在原处修改)
      • 内容不可变
      • 数据之间的相对位置不可变
    • 任意对象的有序集合
    • 固定长度,异构,任意嵌套
    • 支持所有的序列操作,索引,切片

    字典数据

    • 通过'key'来访问数据
    • 任意对象的无序集合
    • 可变长,异构,任意嵌套
    • 可变对象,任意修改

    本质是对象引用的散列表

    • 序列操作对字典无效
    • 对不存在的索引赋值会增加新项
    • 'key'必须可被哈希化,数字,字符串
    • 字典无序存储
    • 通过哈希算法存储'key'查找速度快
    • 查询速度不会因元素增多而变慢
    • 额外储存'key'值,空间换取时间
  • 相关阅读:
    Spring Cloud Consul Config 知识点
    RabbitMQ 的 docker 镜像使用
    Spring Cloud Config 知识点
    漫画:什么是服务熔断?
    Flux 和 Mono 的区别
    同时引入依赖:spring-cloud-starter-gateway 和 spring-boot-starter-web,报错
    Feign 报错:The bean 'service-producer.FeignClientSpecification', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.
    Feign和OpenFeign的区别
    Maven 报错:Compilation of Maven projects is supported only if external build is started from an IDE.
    使用 application.properties 中配置的属性,举例:@Value("${server.port}")
  • 原文地址:https://www.cnblogs.com/boxness/p/11926677.html
Copyright © 2011-2022 走看看