Python之路,Day3,,一: 三元运算三元运


一: 三元运算

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

1 # 书写格式2  3 result = 值1 if 条件 else 值24  5 # 如果条件成立,那么将 “值1” 赋值给result变量,否则,将“值2”赋值给result变量

二: lambda表达式

lambda表达式式。在开发者想要使用一个简单函数作为参数或者返回值时,使用lambda表达式是很方便的。下面是使用lambda表达式作为内置filter函数的一个参数的示相当于函数体为单个return语句的普通函数的匿名函数。请注意,lambda语法并没有使用return关键字。开发者可以在任何可以使用函数引用的位置使用lambda表达例:

1 aList = [1, 2, 3, 4, 5, 6, 7, 8, 9]2 low = 33 high = 74 filter(lambda x, l=low, h=high: h>x>l, aList) # returns: [4, 5, 6]

三:set

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

  1 class set(object):  2     """  3     set() -> new empty set object  4     set(iterable) -> new set object  5        6     Build an unordered collection of unique elements.  7     """  8     def add(self, *args, **kwargs): # real signature unknown  9         """ 10         Add an element to a set,添加元素 11           12         This has no effect if the element is already present. 13         """ 14         pass 15   16     def clear(self, *args, **kwargs): # real signature unknown 17         """ Remove all elements from this set. 清除内容""" 18         pass 19   20     def copy(self, *args, **kwargs): # real signature unknown 21         """ Return a shallow copy of a set. 浅拷贝  """ 22         pass 23   24     def difference(self, *args, **kwargs): # real signature unknown 25         """ 26         Return the difference of two or more sets as a new set. A中存在,B中不存在 27           28         (i.e. all elements that are in this set but not the others.) 29         """ 30         pass 31   32     def difference_update(self, *args, **kwargs): # real signature unknown 33         """ Remove all elements of another set from this set.  从当前集合中删除和B中相同的元素""" 34         pass 35   36     def discard(self, *args, **kwargs): # real signature unknown 37         """ 38         Remove an element from a set if it is a member. 39           40         If the element is not a member, do nothing. 移除指定元素,不存在不保错 41         """ 42         pass 43   44     def intersection(self, *args, **kwargs): # real signature unknown 45         """ 46         Return the intersection of two sets as a new set. 交集 47           48         (i.e. all elements that are in both sets.) 49         """ 50         pass 51   52     def intersection_update(self, *args, **kwargs): # real signature unknown 53         """ Update a set with the intersection of itself and another.  取交集并更更新到A中 """ 54         pass 55   56     def isdisjoint(self, *args, **kwargs): # real signature unknown 57         """ Return True if two sets have a null intersection.  如果没有交集,返回True,否则返回False""" 58         pass 59   60     def issubset(self, *args, **kwargs): # real signature unknown 61         """ Report whether another set contains this set.  是否是子序列""" 62         pass 63   64     def issuperset(self, *args, **kwargs): # real signature unknown 65         """ Report whether this set contains another set. 是否是父序列""" 66         pass 67   68     def pop(self, *args, **kwargs): # real signature unknown 69         """ 70         Remove and return an arbitrary set element. 71         Raises KeyError if the set is empty. 移除元素 72         """ 73         pass 74   75     def remove(self, *args, **kwargs): # real signature unknown 76         """ 77         Remove an element from a set; it must be a member. 78           79         If the element is not a member, raise a KeyError. 移除指定元素,不存在保错 80         """ 81         pass 82   83     def symmetric_difference(self, *args, **kwargs): # real signature unknown 84         """ 85         Return the symmetric difference of two sets as a new set.  对称差集 86           87         (i.e. all elements that are in exactly one of the sets.) 88         """ 89         pass 90   91     def symmetric_difference_update(self, *args, **kwargs): # real signature unknown 92         """ Update a set with the symmetric difference of itself and another. 对称差集,并更新到a中 """ 93         pass 94   95     def union(self, *args, **kwargs): # real signature unknown 96         """ 97         Return the union of sets as a new set.  并集 98           99         (i.e. all elements that are in either set.)100         """101         pass102  103     def update(self, *args, **kwargs): # real signature unknown104         """ Update a set with the union of itself and others. 更新 """105         pass

四:数字和字符串

对于 数字 和 字符串 而言,赋值、浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址。

 1 import copy 2 # ######### 数字、字符串 ######### 3 n1 = 123 4 # n1 = "i am alex age 10" 5 print(id(n1)) 6 # ## 赋值 ## 7 n2 = n1 8 print(id(n2)) 9 # ## 浅拷贝 ##10 n2 = copy.copy(n1)11 print(id(n2))12   13 # ## 深拷贝 ##14 n3 = copy.deepcopy(n1)15 print(id(n3))

技术分享

对于字典、元祖、列表 而言,进行赋值、浅拷贝和深拷贝时,其内存地址的变化是不同的。

1、赋值

赋值,只是创建一个变量,该变量指向原来内存地址,如:

1 n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}2   3 n2 = n1

技术分享

2、浅拷贝

浅拷贝,在内存中只额外创建第一层数据

1 import copy2   3 n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}4   5 n3 = copy.copy(n1)

技术分享

3、深拷贝

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

1 import copy2   3 n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}4   5 n4 = copy.deepcopy(n1)

技术分享

五:函数

定义和使用

1 def 函数名(参数):2        3     ...4     函数体5     ...6     返回值

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

def:表示函数的关键字函数名:函数的名称,日后根据函数名调用函数函数体:函数中进行一系列的逻辑计算,如:发送邮件、计算出 [11,22,38,888,2]中的最大数等...参数:为函数体提供数据返回值:当函数执行完毕后,可以给调用者返回数据。

1.返回值

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

以上要点中,比较重要有参数和返回值:

def 发送短信():           发送短信的代码...       if 发送成功:        return True    else:        return False      while True:           # 每次执行发送短信函数,都会将返回值自动赋值给result    # 之后,可以根据result来写日志,或重发等操作       result = 发送短信()    if result == False:        记录日志,短信发送失败...

2.参数

函数的有三中不同的参数:

普通参数

1 def func(name, age = 18):2     3     print "%s:%s" %(name,age)4 5 # 指定参数6 func(‘wupeiqi‘, 19)7 # 使用默认参数8 func(‘alex‘)

默认参数

 1 复制代码 2 def func(name, age = 18): 3      4     print "%s:%s" %(name,age) 5  6 # 指定参数 7 func(‘wupeiqi‘, 19) 8 # 使用默认参数 9 func(‘alex‘)10 11 注:默认参数需要放在参数列表最后

动态参数

def func(*args):    print args# 执行方式一func(11,33,4,4454,5)# 执行方式二li = [11,2,2,3,3,4,54]func(*li)
----------------------------------------------------def func(**kwargs): print args# 执行方式一func(name=‘wupeiqi‘,age=18)# 执行方式二li = {‘name‘:‘wupeiqi‘, age:18, ‘gender‘:‘male‘}func(**li)
-------------------------------------------------------------def func(*args, **kwargs): print args print kwargs

Python之路,Day3

相关内容

    暂无相关文章

评论关闭