心之所有
想学习的可以互相沟通,互相学习,刚开始学。有什么问题可以沟通

导航

 

一、全局变量和局部变量

局部变量:在函数里面定义的变量是局部变量

全局变量:整个python文件

全局变量的作用域的是整个函数,局部变量的作用域仅仅是函数

当全部变量和局部变量名字一样的时候,在函数内部,局部变量是第一优先级,在函数外,全局变量是第一优先级

注:如果想在函数内部调用全部变量,需要使用到global关键字进行申明变量

name=''

def func():
  name='陈学习'
  print(name)
def func2():
  global name
  name='陈学习测试'
  print(name)

func()
func2()

解:

 

 

 结果:

 

 

 二、匿名函数(使用到关键字lambda)

tada=lambda a,b:a+b
print(tada(9,10))

解:

 

 

 结果:

 

 

 三、过滤函数

(1)filter是过滤的功能,也就是说我们在一个列表的对象里面过滤出我们自己想要的数据

list1=[2,3,5,6,8]
list2=[]
for item in list1:
    if item>3:
        list2.append(item)
print(list2)

#filter形式
def funFilter(a):
    if a>2:
        return True

print(list(filter(funFilter,list1)))

解:

 

 

 结果 :

(2)随机函数


def fun():
    import random
    list3=[]
    for p in range(1,10):
     i=random.randrange(2,88)
     list3.append(chr(i))
    print(''.join(list3))
fun()

结果:

 

 (3) 字母转数字

#chr() 字母转数字
u=109
chr(u)
print(chr(u))

(4)数字转字母

#ord()字母转数字
str1='p'
ord(str1)
print(ord(str1))

 

 

 四、包和模块

模块:每一个python文件就是一个模块

包和模块的区别就是,包里面有__init__.py的模块

###在一个包里面,查找模块里面的内容:

现在一个包里面创建两个不同的模块

如:

 

在第一个模块‘login’填写函数的内容,如:

def index1():
    print('我的世界')
name='chen'
age='100'

在第二个模块‘logout’查找,要用到(from.‘包的名称’. 模块的名称 import 查找的内容 (*)代表所有数据)如:

from 包和函数.login import *
index1()
print(name) print(age)

解:

 

 

 结果:

 

 

 

 

 

 

 五、os实战

1、查找os方法:

import os    (导入os模块)
#查找os路径的方法
print(dir(os))
#查看os所有的方法(模块使用)
print(os.__all__)
['DirEntry', 'F_OK', 'GenericAlias', 'Mapping', 'MutableMapping', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'PathLike', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_AddedDllDirectory', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_check_methods', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_walk', '_wrap_close', 'abc', 'abort', 'access', 'add_dll_directory', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 'stat', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 'umask', 'uname_result', 'unlink', 'unsetenv', 'urandom', 'utime', 'waitpid', 'waitstatus_to_exitcode', 'walk', 'write']
['altsep', 'curdir', 'pardir', 'sep', 'pathsep', 'linesep', 'defpath', 'name', 'path', 'devnull', 'SEEK_SET', 'SEEK_CUR', 'SEEK_END', 'fsencode', 'fsdecode', 'get_exec_path', 'fdopen', 'popen', 'extsep', '_exit', 'DirEntry', 'F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'R_OK', 'TMP_MAX', 'W_OK', 'X_OK', 'abort', 'access', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'device_encoding', 'dup', 'dup2', 'environ', 'error', 'execv', 'execve', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'link', 'listdir', 'lseek', 'lstat', 'mkdir', 'open', 'pipe', 'putenv', 'read', 'readlink', 'remove', 'rename', 'replace', 'rmdir', 'scandir', 'set_handle_inheritable', 'set_inheritable', 'spawnv', 'spawnve', 'startfile', 'stat', 'stat_result', 'statvfs_result', 'strerror', 'symlink', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 'umask', 'uname_result', 'unlink', 'unsetenv', 'urandom', 'utime', 'waitpid', 'waitstatus_to_exitcode', 'write', 'makedirs', 'removedirs', 'renames', 'walk', 'execl', 'execle', 'execlp', 'execlpe', 'execvp', 'execvpe', 'getenv', 'supports_bytes_environ', 'spawnl', 'spawnle']

2、返回当前目录,使用到(getcwd())

如:

print('返回当前目录:',os.getcwd())

结果:

 

3、获取绝对路径,使用到(abspath(在哪个盘就写哪个路径))

如:

print('获取绝对路径:',os.path.abspath('c/Users'))

结果:

 

4、针对 路径进行分割,使用到(spilt)

 

 如:

print('针对目录进行分割:',os.path.split('c/Users'))

结果:

 file

5、判断是不是目录,使用到(isfile)

如:

print('判断是不是文件:',os.path.isFile('c/Users'))

 结果:

 

 

 6、获取当前路径,使用到(dirname(__file__))

 如:

print('获取当前路径:',os.path.dirname(__file__))

结果:

 

 7、获取当前路径的上一级路径,使用到(使用两次:os.path.dirname+(__file__))

如:

print('获取当前路径的上一级路径:',os.path.dirname(os.path.dirname(__file__)))

结果:

 

 8、获取当前路径的上上级路径,使用到(使用3次:os.path.dirname+(__file__))

如:

print('获取当前路径的上上级路径:',os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

 

结果:

 

 9,查看当前路径下所有的文件和文件夹,,用到(listdir('文件名称'))

print('查看当前路径下的所有文件和文件夹:\n')
for item in os.listdir('/users/'):
    print(item)
结果:
Administrator.lwp-PC All Users Default Default User Default.migrated desktop.ini Public Process finished with exit code 0

10、例如:

best_dir=os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
print('获取login.by路径:',os.path.join(best_dir,'函数','login..by'))

结果:

 

posted on 2021-06-28 19:11  橙橙的橙  阅读(37)  评论(0编辑  收藏  举报