三元运算

1 name = "张三" if 1 == 2 else "李四"
2 print(name)
3 name1 = "张三" if 1 == 1 else "李四"
4 print(name1)

     运行结果
      李四

      张三

三元运算就是如果为真取前面的值1,如果为假为后面的值2  值1 if条件 else 值2

set集合主要有交集,并集,(无序不重复)

 1 old_dict ={
 2     "#1":11,
 3     "#2":22,
 4     "#3":100,
 5 }
 6 
 7 new_dict = {
 8     "#1":33,
 9     "#4":22,
10     "#7":100,
11 }
12 old_keys= old_dict.keys()
13 new_keys = new_dict.keys()
14 old_set_keys=set(old_keys)
15 new_set_keys=set(new_keys)
16 remove_set = old_set_keys.difference(new_set_keys)
17 add_set = new_set_keys.difference(old_set_keys)#求new_set_keys去掉与old_set_keys相同的项,余下的
18 update_set = old_set_keys.intersection(new_set_keys) #求交集
19 print(remove_set) 20 print(add_set) 21 print(update_set) 22 for i in remove_set: 23 old_dict.pop(i) 24 for j in add_set: 25 old_dict.update({j:new_dict[j]}) 26 for m in update_set: 27 old_dict[m]=new_dict[m] 28 print(old_dict)

集合主要用于更新,如上面将old_dict字典与新的new_dict相同的key更新为新的,而与new_dict不同的#2 #3则移除,并将new_dict中没有的#4 #7更新到old_dict中结果如下
{'#1': 33, '#4': 22, '#7': 100}

 浅拷贝与深拷贝

对于数字与字符串深浅一样

1 import copy
2 n1="123n"
3 n2=copy.copy(n1)#浅拷贝
4 n3=copy.deepcopy(n1) #深拷贝
5 print(id(n1))
6 print(id(n2))
7 print(id(n3))

运行结果
18477328
18477328
18477328

内存地址一样的,所以深浅一样

list tuple dict深浅有区别,浅拷最外一层,深拷底层的数据不拷其它的都拷

1 import copy
2 n1=[11,22,33,{"k1":"v1"}]
3 n2=copy.copy(n1)#浅拷贝
4 n3=copy.deepcopy(n1) #深拷贝
5 print(id(n1[3]))
6 print(id(n2[3]))
7 print(id(n3[3]))

运行结果

16762888
16762888
18553096

1 import copy
2 n1=[11,22,33,{"k1":"v1"}]
3 n2=copy.copy(n1)#浅拷贝
4 n3=copy.deepcopy(n1) #深拷贝
5 print(id(n1[3]["k1"]))
6 print(id(n2[3]["k1"]))
7 print(id(n3[3]["k1"]))

运行结果
18740648
18740648
18740648

说明最底层的数据元素深浅一样,没有被拷贝

=======================================================

函数

实参与形参

 1 import smtplib
 2 from email.mime.text import MIMEText
 3 from email.utils import formataddr
 4 def mail(n):#形参n为目的邮箱地址
 5     msg = MIMEText('邮件内容如服务器挂了', 'plain', 'utf-8')
 6     msg['From'] = formataddr(["发件人姓名", '13632610XXXX@139.com'])
 7     msg['To'] = formataddr(["自已", '43125471@qq.com'])
 8     msg['Subject'] = "邮件标题 "
 9 
10     server = smtplib.SMTP("smtp.139.com", 25)
11     server.login("1363261XXXX@139.com", "你登陆邮箱的密码")
12     server.sendmail('1363261XXX@139.com', [n, ], msg.as_string())
13     server.quit()

mail("43125471@qq.com")将实参传入到形参中,一般情况一一对应的

如果改变顺序则要

1 def f(a,b):
2     print(a,b)
3 f(b="456",a="123")

运行结果是123 456
传多个参数(包括0个参数)

1 def f(*a):
2     print(a,type(a))
3 f(11,22,33)

运行结果 (11, 22, 33) <class 'tuple'>加一个*为一个元组,如果加两个*则是一个字典

def f(**kwargs):
    print(kwargs,type(kwargs))
f(k1=123,k2=456)

运行结果{'k1': 123, 'k2': 456} <class 'dict'> 为一个字典

1 def f(*args,**kwargs):
2     print(args,type(args))
3     print(kwargs,type(kwargs))
4 f(11,22,33,k1=123,k2=456)

运行结果(11, 22, 33) <class 'tuple'>
              {'k2': 456, 'k1': 123} <class 'dict'>
加一个*传入的变成元组,**为字典,

1 def f(*args,**kwargs):
2     print(args,type(args))
3     print(kwargs,type(kwargs))
4     return kwargs
5     print(kwargs, type(kwargs))
6 
7 dic=f(k1=123,k2=456)
8 print(dic)


运行结结果() <class 'tuple'>
                  {'k2': 456, 'k1': 123} <class 'dict'>
                 {'k2': 456, 'k1': 123}

说明return语句返回值之后下面的语句不在执行,如果没有返回值则返回none

 如果没有指定可以设置默认参数

def f(b,a=3):
    print(a,b)
f(1)

运行结果 3 1 其中1传给了b,a 没有传实参,默认参数都要写在后面不能写成def f(a=3,b):

===================================================

局部变量(只在函数中用),全局变量(所有都能用的)

 1 P="lisi"
 2 def func1():
 3     a=123
 4    
 5     global P
 6     P="0000"
 7     print(a)
 8 
 9 def func2():
10     a=456
11     print(a)
12     print(P)
13 func1()
14 func2()

运行结果123
               456
               0000

全局变量用大写,局部变量用小写,如果想在函数里面改变全局变量,则要加一个global P安装

========================================================================

函数

    a定义

    b函数名

    c 返回值

        1 return  返回值 ,没有则为None

        2 return语句之后则不在执行

    d 参数

     1,形参,实参

      2 普通参数与实参一般一一对应,如果不对应则要能通过形参=实参

      3 默认形参放在所有形参数的尾部

       4 动态参数 一个*args为tuple **kargs为dict类型

全局与局部变量

       全局大写,哪在局部中要修改global

        局部变量小写

====================================================

斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368

 

 1 C=3
 2 def func(arg1,arg2):
 3     global C
 4     C=C+1
 5     if arg1 == 0:
 6         print(arg1,arg2)
 7     arg3 = arg1 + arg2
 8     print(arg3)
 9 
10     if C > T:
11        print(arg3)
12        return arg3
13     func(arg2,arg3)
14 t=input("please input  第几位数:")
15 T=int(t)
16 func(0,1)

引用了全局变量,并通过C计数器控制

运行结果please input  第几位数:13
0 1
1
2
3
5
8
13
21
34
55
89
144
144

======================================

写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数

 

 1 def func(s):
 2     isdigit_sum = 0
 3     isalpha_sum = 0
 4     isspace_sum = 0
 5     other_sum = 0
 6     for i in s:
 7         # print(i)
 8         if i.isdigit():
 9             isdigit_sum +=1
10         elif i.isalpha():
11             isalpha_sum +=1
12         elif i.isspace():
13             isspace_sum +=1
14         else:
15             other_sum +=1
16     return {"数字":isdigit_sum,"字母":isalpha_sum,"空格":isspace_sum,"其它":other_sum}
17 m="ssss234s123 345"
18 t=func(m)
19 print(t)

写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容

 

 1 def func(s):
 2     for i in s:
 3 
 4         if  i==" ":
 5             return True
 6 
 7 def fun_flag(flag,s):
 8     if flag:
 9         print(s,"有空内容")
10     else:
11         print(s,"输入的内容没有空格")
12 m="mn     nnl"
13 lis=[11,22,33]
14 ret_m=func(m)
15 fun_flag(ret_m,m)
16 ret_lis=func(lis)
17 fun_flag(ret_lis,lis)

 

写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者

dic = {"k1": "v1v1", "k2": [11,22,33,44]}
 
PS:字典中的value只能是字符串或列表
1 def func(s):
2     dic1={}
3     for i in s.keys():
4         if len(s[i]) > 2:
5             dic1.update({i:s[i][0:2]}) #新增一个key和value并且value值切片,取前两位
6     return dic1
7 dic = {"k1": "v1v1", "k2": [11,22,33,44],"k3":"12"}
8 t=func(dic)
9 print(t)

写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。

1 def func(s):
2     new_list=[]
3     for i in range(0,len(s)):
4         if i % 2 == 1:
5            new_list.append(s[i])
6     return new_list
7 lis=[11,23,33,44,55,66]
8 print(func(lis))

写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。

 

1 def func(s):
2     if len(s) > 2:
3        new_list=s[0:2]#进行切片
4     return new_list
5 s1=[11,22,33,44]
6 lis1=func(s1)
7 print(lis1)

 

posted on 2017-09-28 11:15  43125471  阅读(141)  评论(0编辑  收藏  举报