Linux system Python--4

                                    集合

集合set的定义

  如果花括号里面为空,则是字典类型

Linux system Python--4

  定义集合的第一种方式

Linux system Python--4

  定义集合的第二种方式;定义一个空的集合

Linux system Python--4

  实现列表去重1

Linux system Python--4

  实现列表去重2

Linux system Python--4

集合的特性

  集合是无序的、不重复的数据类型,因此不支持索引、切片、重复和连接

  可支持成员操作符

Linux system Python--4

集合的增删改查

  增

Linux system Python--4

Linux system Python--4

  删

Linux system Python--4

    s.discard删除集合指定元素,如果不存在则什么都不做

Linux system Python--4

  查(关系测试操作)

Linux system Python--4

    交集

Linux system Python--4

    并集

Linux system Python--4

    差集

Linux system Python--4

    对等差分

Linux system Python--4

    交集

Linux system Python--4

    并集

Linux system Python--4

    差集

Linux system Python--4

    对等差分

Linux system Python--4

总结

可变数据类型:列表、字典、集合

不可变数据类型:数值类型、字符串、元组

   可变数据类型实现某个功能,直接改变可变的数据类型;

   不可变数据类型实现某个功能,需要将结果赋值给另外一个变量

是否实现for循环

   可迭代数据类型:str、list、tuple、dict、set

   不可迭代数据类型:数值类型

是否支持索引、切片、重复和连接特性

   支持有序的数据类型:str、list、tuple

   不支持无序的数据类型:dict、set

 

 

                          乐高积木之函数

函数的定义

   def 函数名( ):

         函数体

函数的调用

  函数名( )

  总结:定义函数时,函数不执行;调用函数时,函数才执行

有参数的函数

必选参数

  形式参数

  实参,x=1,2

Linux system Python--4

默认参数

Linux system Python--4

可变参数

  形式参数

  实参,x=1,y=2

Linux system Python--4

关键字参数

  kwargs实质上是一个字典

Linux system Python--4

返回值

  函数中如果没有return时,默认返回None

Linux system Python--4

返回多个值

  实质上python只能返回一个值

  间接通过元组返回多个值

Linux system Python--4

函数的作用域

  global关键字必须要先声明再赋值

  全局变量

  global声明num为全局变量

Linux system Python--4

迭代

  是否可以for循环遍历的对象

  isinstance判断是否可迭代

Linux system Python--4

列表生成式

  生成列表的公式

  需求:生成一个列表,返回1-100中偶数的平方

  方法1

Linux system Python--4

  方法2

Linux system Python--4

变异的列表生成式

  for循环嵌套if语句

Linux system Python--4

  for循环嵌套for循环

Linux system Python--4