美文网首页
Python基础

Python基础

作者: YoungHu | 来源:发表于2020-05-20 15:30 被阅读0次

Python基础

https://www.runoob.com/python3/python3-tutorial.html

1 数据类型

数字

  • 整型int和浮点型float

    • round四舍五入
    • int浮点数转整数,float整数转浮点数
    • math.floor向下取整;math.ceil向上取整
    • 返回小数和整数部分

空值

  • None,特殊值,不能理解为0因为0是有意义的

复数

  • a + bj

布尔型

  • True、False

字符串

  • str,

    • 支持索引切片操作,但字符串不支持索引更改 len

    • 支持 + *运算

    • 大小写转换 str.capitalize , str.upper , str.lower , str.swapcase, str.title

    • 字符类型 str.isalpha, str.isnumeric, str.isalnum, isdigit, islower, isupper, isspace

    • 切割 str.split str.strip str.lstrip str.rstrip str.splitlines按行分割

    • 查找 str.startwith str.endwith str.find str.index

    • 替换与统计次数 str.replace('th', 'TH') str.maketrans str.translate str.count

    • 编码与解码 str.encode str.decode

    • 拼接 str.join 将列表或元组通过str字符拼接成字符串

    • 填充

      • str.center str.rjust str.ljust str.zfill
    • 字符串比较大小与ASCII码值 ord() char()

列表

  • list []

      • 支持索引切片 in判断 list.index len max in list.count
      • 支持索引更改列表元素
      • list.append list.insert list.extend 支持 + *运算
      • del list[index]可批量 list.pop list.remove list.clear
    • 排序反转

      • list.sort list.reverse
    • 浅拷贝与深拷贝

      • list2=list1 list3=list1.copy()
    • 遍历与枚举

      • for...in enumerate

元组

  • tuple ()

    • 访问元素

      • 支持索引切片操作
    • 修改元素

      • 元组内的元素不可更改
    • 元组操作

      • 支持+ *运算 len max min

字典

  • dict {} 键值对 key唯一,无索引

    • 访问元素

      • dict[key]
    • 添加元素

      • dict[key]=value
    • 删除元素

      • dict.pop(key)
    • 遍历

      • dict.values dict.items enumerate

集合

  • set {}

    • 本质

      • 无索引,类似dict,是一组key集合,不存储value。无序和无重复元素的集合
    • 添加元素

      • set.add 不能是可变对象 set.update 打碎插入
    • 删除元素

      • set.remove set.discard set.pop随机移除
    • 遍历

      • for i in set:
    • 计算

      • len 交集 & set.intersection 并集 | set.union

2 变量

标识符

  • 给变量、函数等命名 ; 字符串 ; 字母数字下划线组成,不能数字开头 ; 不能用关键字

存储位置

  • id(varname()

3 运算符

算术运算符

        • / // % **

比较运算符

  • < >= <= == !=

赋值运算符

  • = += -= *= /= //= %= **=

逻辑运算符

  • and or not

成员运算符

  • in not in

身份运算符

  • is is not

位运算符

  • & | ^ ~ >> <<

4 流程控制

分支结构

  • if...elif...elif....else

循环结构

  • for循环

    • for...in...:
  • while循环

    • while:...else:... 在循环条件为False时执行else语句块
  • continue

    • 跳过当前循环进入下一循环
  • break

    • 退出该层循环

5 函数

最简单函数

  • 无参无返回值

函数的参数

  • 不可变类型和可变类型

函数的返回值

  • return

传递参数

  • 值传递
  • 引用传递

关键字参数

  • 函数调用使用关键字参数来确定传入的参数值,传参顺序可变

默认参数

  • 默认参数的值如果没有传入,则被认为是默认值,一般放最后

不定长参数

  • def funname(*args): 可传递任意数量参数

匿名函数

  • lamda arg1,..,argn: 表达式

相关文章

网友评论

      本文标题:Python基础

      本文链接:https://www.haomeiwen.com/subject/jphnohtx.html