函数递归 二分法 匿名函数 内置函数 模块的使用 模块的搜索路径

函数递归

函数递归调用:在调用一个函数的过程中直接或间接地调用该函数本身,称之为函数的递归调用
# import sys
# # print(sys.getrecursionlimit())
# sys.setrecursionlimit(2000)
# n=1
# def func1():
#     global n
#     print('from func1',n)
#     n+=1
#     func1()
#
# #
# func1()


# def func():
#     print('from func')
#     bar()
#
# def bar():
#     func()
#
# func()


#递归分为两个重要的阶段:递推+回溯

# age(5)=age(4)+2
# age(4)=age(3)+2
# age(3)=age(2)+2
# age(2)=age(1)+2
# age(1)=18
#
# n!=1 #age(n)=age(n-1)+2
# n=1 #age(n)=18

 

二分法

l=[1,2,10,30,33,99,101,200,301,402] #从小到大排列的数字列表
def get(num,l):
    print(l)
    if len(l) > 0: #列表不为空,则证明还有值是可以执行二分法逻辑的
        mid=len(l)//2
        if num > l[mid]:
            #in the right
            l=l[mid+1:]
        elif num < l[mid]:
            #in the left
            l=l[:mid]
        else:
            print('find it')
            return
        get(num,l)
    else: #列表为空,则证明根本不存在要查找的值
        print('not exists')
        return
get(403,l)


# num=200
# for item in l:
#     if num == item:
#         print('find it')
#         break

 

匿名函数

# def f1(n):
#     return n**2
#
#
# # f1(3)
# print(f1)
#
# f2=lambda n:n**2
# print(f2)
#
# print(f2(3))
#
# lambda n:n**2


#匿名函数即没有绑定名字的函数,没有绑定名字,意味着只能用一次就会被回收
#所以说匿名函数的应用场景就是:某个功能只用一次就结束了


salaries={
    'egon':3000,
    'alex':100000000,
    'wupeiqi':10000,
    'yuanhao':2000
}
# print(max(salaries)) #默认比较的是key
# print(max(salaries.values()))



#拉链函数
# l1=[1,2,3]
# s1='hello'
#
# res=zip(l1,s1)
# print(list(res))

 

内置函数

#优先掌握
max
min
sorted
map
from _functools import reduce
filter
sum
bool
chr
divmod
enumerate
id
input
print
isinstance
iter
len
open
pow
type
zip


#面向对象
object

classmethod
staticmethod
property

getattr
hasattr
setattr
delattr

super

isinstance
issubclass

object.__dict__

int,str,bytes,list,tuple,set,float,dict


#其他内置函数
# print(abs(-1))

# print(all([1,'a',[]]))
# print(all([]))

# print(any([0,None,'',1]))
# print(any([])) #False

# print(bin(10))
# print(oct(10))
# print(hex(10))

#布尔值为假:0,None,空
# bool()

# print('hello'.encode('utf-8'))
# print(bytes('hello',encoding='utf-8'))

# print(callable(max))

# print(chr(65))
# print(ord('A'))


#complex复数
# x=1-2j #x=complex(1-2j)
# print(type(x))
# print(x.real)
# print(x.imag)

#dict,int,list,tuple,str,float,set,frozenset
# s=set({1,2,3}) #可变集合
# s=frozenset({1,2,3}) #不可变集合

import time
# print(dir(time))

# print(divmod(1001,25))

# l=['a','b','c','d']
# for x in enumerate(l):
#     print(x)


# print(hash('asdfasdfasdfasdfasdf'))
# print(hash(' asdfasdfasdfasdfasdf'))

# def func():
#     '''
#     xxxxxx
#     :return:
#     '''
#     pass
#
# print(help(func))


# print(isinstance(1,int))
# print(type(1) is int)



# print(pow(10,2,3)) #10**2%3

# print(str({'a':1}))

# l=[1,4,2,9]
# print(list(reversed(l)))

# print(round(10.55545,3))

l1=['a','b','c','d','e']
l2=['a','b','c','d','e']
# print(l1[1:5:2]) #'b','d'
# print(l2[1:5:2]) #'b','d'

# obj=slice(1,5,2)
# print(l1[obj])
# print(l2[obj])






# print(sum([1,2,3,4]))
# print(sum(range(10)))
# print(vars() is locals())
# vars(obj) 等同于obj.__dict__

# x=111111111111111111111111111111111111111
# print(locals())



# m=input('>>: ')
# print(type(m))
# obj=__import__(m)
# obj.sleep(3)

# import "time" #import 不能导入字符串


#了解:compile,exec,eval

#eval:提取字符串内的表达式执行,然后返回执行结果
# s1="1+2+3"
# s1="['a','b','c']"
# l=eval(s1)
# print(type(l))

# s2="for i in range(10):print(i)"
# eval(s2)

#exec:仅仅只是执行字符串内的表达式或语句,没有返回值
# s1="1+2+3"
# print(exec(s1))
s2="for i in range(10):print(i)"
exec(s2)

 

模块的使用

#第一部分:import

#导入模块,只会在第一次导入时执行源文件的代码
#如果模块已经加载到内存了,下一次导入直接引用内存中导入的结果
# import spam #m1=111111
# import spam #m2=m1
# import spam
# import spam
# import spam
# import spam

# import sys
# print('spam' in sys.modules)


#import 导入文件都做了哪些事?
#1 以源文件为准产生一个名称空间
#2 以刚刚产生的名称空间为准,执行源文件的代码
#3 会在当前文件中定义一个名字,这个名字就是模块名,用来引用模块中的名字
# import spam
# # money=0
# # def read1():
# #     print('from ------>')
# # read1()
# # spam.read1()
# # spam.read2()
#
#
# money=1000000000
# spam.change()
# # print(money)
# spam.read1()


#为模块起别名
# import spam as sm
#
# print(sm.money)



# engine_type='mysql'
# if engine_type == 'mysql':
#     import mysql as engine
# elif engine_type == 'oracle':
#     import oracle as engine
#
# engine.parse()



#在一行导入多个模块
import spam,time




##第一部分:from...import...

# from spam import money,read1,read2,change
# money=1
# print(money)
# read1()
# def read1():
#     print('===>?')
# read1()

# read2()

# change()
# print(money)


# from spam import *
# print(money)
# print(read1)
# print(read2)
# print(change)

# from spam import * #* 对应模块spam内的__all__属性
# # print(money)
# # print(read1)
# print(read2)

# from spam import money,read1,read2,change
#
# import importlib
#
# importlib.reload()


import spam

 

模块的搜索路径

#模块的查找顺序是:内存中已经加载的模块->内置模块->sys.path路径中包含的模块
# import time,sys
# print(sys)

# import time
# time.sleep(3)

# import sys
# print(sys.path)

# import xxx
# import sys
# sys.path.append(r'C:\Users\Administrator\PycharmProjects\python19期\day5\a')


# import m
from a import m

 

posted @ 2017-10-20 19:02  荒天帝001  阅读(140)  评论(0编辑  收藏  举报