python继续函数-练习(2017-8-3)

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

    def detection(p):
        intcount = 0
        strcount = 0
        othercount = 0
        spacecount=0
        for i in p:
            if i.isalpha():
                strcount+=1
            elif i.isdigit():
                intcount+=1
            elif i.isspace():
                spacecount+=1
            else :
                othercount+=1
        return{"数字":intcount,"字母":strcount,"空格":spacecount,"其它":othercount}
    a=input("Please enter a string")
    zz=detection(a)
    print(zz)


写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。
1、

    def detection(p):
            b=0
            a="q"
            if type(p)==type(a):
                for i in p:
                    b+=1
            else:
                for i in p:
                    for k in i:
                        b+=1
            if b>5:
                a="长度大于5"
                return a
            else :
                a = "长度不大于5"
                return a

        a=["qwe","123"]
        qq=detection(a)
        print(qq)


2、

        def detection(p):
            if len(p)>5:
                return True
            return False



        a=["qwe","123","123","123","123","123"]
        qq=detection(a)
        # print(len(a))
        print(qq)


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

        def detection(p):
            b=0
            a="q"
            if type(p)==type(a):
                for i in p:
                    if i.isspace():
                        return True

            else:
                for i in p:
                    for k in i:
                        if k.isspace():
                            return True
            return False



        a=["qwe","123"]
        qq=detection(a)
        print(qq)


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

        def detection(p):
            if len(p)>2:
                p=p[0:2]
                return p

        a=["qwe","123","123","123","123","123"]
        qq=detection(a)
        # print(len(a))
        print(qq)

2、

        def detection(p):
            if len(p)>2:
                del p[2:] #删除2以后的元素
            return p

        dic=[11,22,33,44]
        qq=detection(dic)
        # print(len(a))
        print(qq)

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

    def detection(p):
        a=[]
        for k,i in enumerate(p):
            if k%2!=0:
                a.append(i)
        return a

    a=("qwe","123","123","000","123","111")
    qq=detection(a)
    print(qq)


---------------一种错误写法-----------------------------------

    def f1(arg)
        for i in range(len(rag)):
            if i%2==1
                pass
            else:
                del arg[i]#这种循环再次遇到len(arg)时他的长度改变所以再次删除时就会
                        #根据新的排序删除,就会出错,所以想要使用len(arg)这个方式
                        #可以新建一个列表,在if后进行添加:arg.append(arg[i])就可以
                        #最后在函数结尾添加一个返回值
    li=[11,22,33,44]
    f1(li)
    print(li)



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

        def detection(p):
            for i in p.keys():
                if len(p[i])>2:
                    p[i]=p[i][0:2]
            return p

        dic = {"k1": "v1v1", "k2": [11,22,33,44]}
        qq=detection(dic)
        # print(len(a))
        print(qq)

2、 #创建新的变量

        def f2(arg):
            ret={}
            for key,values in arg.items():#items类似于 enumerate
                if len(values)>2:
                    ret[key]=values[0:2]
                else:
                    ret[key]=values
            return ret


        a={"qwe":"123","qqq":"000","ppp":"111"}
        r=f2(a)
        print(r)

3、#对原来的变量进行改变

        def f2(arg):
            for key,values in arg.items():
                if len(values)>2:
                    arg[key]=values[0:2]
                else:
                    arg[key]=values



        a={"qwe":"123","qqq":"000","ppp":"111"}
        f2(a)
        print(a)

写函数,利用递归获取斐波那契数列中的第 10 个数,并将该值返回给调用者

这个练习结果在http://www.cnblogs.com/liudi2017/articles/7383998.html


总结

  print()#输出变量
  name="babab"
  age=10
  job="stu"    #多个变量引用
  print("my name is %s,my job is %s,age id %d"%(name,job,age))
  #单个变量引用
  print("you %s zzz"%name)

判断一个数据的类型
temp="aaa"
isinstance(temp,str) #str为数据类型,temp为变量


在python中列表、字典、set类型数据的在传给函数传参数时,传的是引用(地址?,就像在C语言里数组给函数传参数时,传的是地址),
所以在函数中形参的改变会造成实参的改变。如果函数中形参指向另一个变量这这个形参就改指向另一个变量的地址,再对形参进行改变时,不会影响实参,因为它不再指向实参

    def aa(p):
        p=p.pop()

    z={11,22,33}
    aa(z)
    print(z)

结果:{11, 22}

    def aa(p):
        p.append(123)
    z=[11,22,33]
    aa(z)
    print(z)

结果:[11, 22, 33, 123]

    def aa(p):
        p={22,11,22}#这个情况下,在函数中p的地址有重新指向            #{22,11,22},所以在下面输出时没有改变
    z={11,22,33}        #个人认为  是对的
    aa(z)
    print(z)

#结果:{33, 11, 22}

别的语言可能有两种选择,但是python默认就一种,就是传引用的参数

特别添加
range

>>>range(10) # 从 0 开始到 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 11) # 从 1 开始到 11
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(0, 30, 5) # 步长为 5
[0, 5, 10, 15, 20, 25]
>>> range(0, 10, 3) # 步长为 3
[0, 3, 6, 9]
>>> range(0, -10, -1) # 负数
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> range(0)
[]
>>> range(1, 0)
[]

以下是 range 在 for 中的使用,循环出runoob 的每个字母:

>>>x = 'runoob'
>>> for i in range(len(x)) :
... print(x[i])
... 
r
u
n
o
o
b
>>>


循环输出索引

x = 'runoob'
for i in range(len(x)) :
print(i)

0
1
2
3
4
5
a[11,22,33]
b=a[0:2]

#这里的b所指的切片在内存里是重新创建的,所以a指向的单单是新建的地址

原文地址:https://www.cnblogs.com/liudi2017/p/7666903.html