python第四周小测验答案_Python 学习日记(第四周)

set数据类型

先用一行代码来说明一下

#!/usr/bin/env python

s2={}

s= {33,12,33,32121}for i ins:print(i)print(type(s))print(type(s2))

s1=set()

s1.add(11)

s1.add(22)

s1.add(33)print(s1)

下面的代码的运行结果

32121

12

33

{33, 11, 22}

通过代码的结果可以看出

set是一个是一个无序且不重复的元素集合

创建set 集合和字典相同{} 只有通过内部的元素才能体现出区别

创建空set集合,最好使用set()的方法创建,然后通过add方法添加元素

以下是set集合的一些常用方法

classset(object):"""set() -> new empty set object

set(iterable) -> new set object

Build an unordered collection of unique elements."""

def add(self, *args, **kwargs): #real signature unknown

"""Add an element to a set,添加元素

This has no effect if the element is already present."""

pass

def clear(self, *args, **kwargs): #real signature unknown

"""Remove all elements from this set. 清除内容"""

pass

def copy(self, *args, **kwargs): #real signature unknown

"""Return a shallow copy of a set. 浅拷贝"""

pass

def difference(self, *args, **kwargs): #real signature unknown

"""Return the difference of two or more sets as a new set. A中存在,B中不存在

(i.e. all elements that are in this set but not the others.)"""

pass

def difference_update(self, *args, **kwargs): #real signature unknown

"""Remove all elements of another set from this set. 从当前集合中删除和B中相同的元素"""

pass

def discard(self, *args, **kwargs): #real signature unknown

"""Remove an element from a set if it is a member.

If the element is not a member, do nothing. 移除指定元素,不存在不保错"""

pass

def intersection(self, *args, **kwargs): #real signature unknown

"""Return the intersection of two sets as a new set. 交集

(i.e. all elements that are in both sets.)"""

pass

def intersection_update(self, *args, **kwargs): #real signature unknown

"""Update a set with the intersection of itself and another. 取交集并更更新到A中"""

pass

def isdisjoint(self, *args, **kwargs): #real signature unknown

"""Return True if two sets have a null intersection. 如果没有交集,返回True,否则返回False"""

pass

def issubset(self, *args, **kwargs): #real signature unknown

"""Report whether another set contains this set. 是否是子序列"""

pass

def issuperset(self, *args, **kwargs): #real signature unknown

"""Report whether this set contains another set. 是否是父序列"""

pass

def pop(self, *args, **kwargs): #real signature unknown

"""Remove and return an arbitrary set element.

Raises KeyError if the set is empty. 移除元素"""

pass

def remove(self, *args, **kwargs): #real signature unknown

"""Remove an element from a set; it must be a member.

If the element is not a member, raise a KeyError. 移除指定元素,不存在保错"""

pass

def symmetric_difference(self, *args, **kwargs): #real signature unknown

"""Return the symmetric difference of two sets as a new set. 对称差集

(i.e. all elements that are in exactly one of the sets.)"""

pass

def symmetric_difference_update(self, *args, **kwargs): #real signature unknown

"""Update a set with the symmetric difference of itself and another. 对称差集,并更新到a中"""

pass

def union(self, *args, **kwargs): #real signature unknown

"""Return the union of sets as a new set. 并集

(i.e. all elements that are in either set.)"""

pass

def update(self, *args, **kwargs): #real signature unknown

"""Update a set with the union of itself and others. 更新"""

pass

三元运算

三元运算(三目运算),是对简单的条件语句的缩写。

#书写格式

result= 值1 if 条件 else值2#如果条件成立,那么将 “值1” 赋值给result变量,否则,将“值2”赋值给result变量

对于条件判断的补充

当if的判断语句中有 条件1 or 条件2 and 条件3的时候

按顺序执行当条件一满足的时候后面就不找了

因此需要改成( 条件1 or 条件2)and 条件3 才可以

深浅拷贝

首先说明

在下述环境中

当找到内存地址,就认为找到了数据内容

对于 数字 和 字符串

变量========房子名

内存地址(实际数据)=====房子地址

赋值======== 房子名—房子地址

内存========中介(有许多房源)

浅拷贝

看房子地址单

浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址。(房子地址)

对于字典、元祖、列表

字典、元祖、列表相当于房子在房子内部有许多房间

房子=[ 房间号,房间号,房间号]

因此字典、元祖、列表存储的房子的房间号的集合===房屋房间号对照表

对于浅拷贝

相当于复制一份房屋房间号对照表

对于深拷贝

按照房屋房间号对照表盖房

注意!!由于python内部对字符串和数字的优化 所以在最后一层(按照房屋房间号对照表盖房)也指向同样的地址

即可理解为

深拷贝,在内存中将所有的数据重新创建一份(排除最后一层,即:python内部对字符串和数字的优化)

函数

函数或者叫方法的目的是将程序中大量的重复代码整合,使程序更加简洁易懂。

函数式编程最重要的是增强代码的重用性和可读性

函数的定义

def函数名(参数):

...

函数体

...

返回值

函数的定义通过def 关键字 +函数名定义

函数的定义主要有如下要点:

def:表示函数的关键字

函数名:函数的名称,日后根据函数名调用函数

函数体:函数中进行一系列的逻辑计算,如:发送邮件、计算出 [11,22,38,888,2]中的最大数等...

参数:为函数体提供数据

返回值:当函数执行完毕后,可以给调用者返回数据。

系统内置函数

949531-20160529000714241-757416682.png

函数的返回值

函数是一个功能块,该功能到底执行成功与否,需要通过返回值来告知调用者。

例如:

#!/usr/bin/env python

deffuncl():return "程序执行了"r=funcl()print(r)

return的返回值可以是字符串也可以是其它数据类型

默认情况下 return返回 None:

注意: 一旦遇到return以下的代码将不再执行

函数的参数

定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了。对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复杂逻辑被封装起来,调用者无需了解。

函数的参数就是给函数内部一个数据,使内部代码既能重复执行又能产生不同结果 实现复用

例如 计算x的平方数

defpower(x):return x * x

>>> power(5)25

>>> power(15)225

通过改变x的值就可以得到任意数的平方数

函数的参数有3种类型

普通参数 例如刚才的例子中的x

默认参数

动态参数

默认参数

默认参数就给参数一个默认值。

例如

#!/usr/bin/env python

defpower(x):return x *x

power()

当给x值的时候程序便会报错

Traceback (most recent call last):

File"C:/Users/zhang/PycharmProjects/untitled/blog.py", line 4, in power()

TypeError: power() missing1 required positional argument: 'x'

修改一下程序

#!/usr/bin/env python

def power(x=0):return x *x

r=print(power())

这样即便没有给x值,但程序有一个默认值。因此程序正常运行

动态参数

函数的参数不单单可以是一个变量,也可以是列表,字典等

def func(*args):printargs#执行方式一

func(11,33,4,4454,5)#执行方式二

li = [11,2,2,3,3,4,54]

func(*li)

动态参数

def func(**kwargs):printargs#执行方式一

func(name='wupeiqi',age=18)#执行方式二

li = {'name':'wupeiqi', age:18, 'gender':'male'}

func(**li)

动态参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值