Python基础01-20160508,,一、Python简介


一、Python简介

1.1 Python介绍

1.2 Python应用

二、Python发展历史

三、Python 2.X vs Python 3.X

3.1 概述

3.2 详细差异

1)编码问题 Python3.x无需再特意指定UTF-8,默认支持UTF-8编码

2)print函数区别

C:\Users\Administrator>pythonPython2.7.11(v2.7.11:6d1b6a68f775,Dec52015,20:32:19)[MSCv.150032bit(Intel)]onwin32Type"help","copyright","credits"or"license"formoreinformation.>>>print"HelloWorld"HelloWorld>>>C:\Users\Administrator>python3Python3.5.0(v3.5.0:374f501f4567,Sep132015,02:16:59)[MSCv.190032bit(Intel)]onwin32Type"help","copyright","credits"or"license"formoreinformation.>>>print"HelloWorld"File"<stdin>",line1print"HelloWorld"^SyntaxError:Missingparenthesesincallto‘print‘>>>print("HelloWorld")HelloWorld

3)某些库改名了

4)用户输入

Python2.x:raw_inputPython3.x:input

四、Python安装

4.1 安装介绍

详见:http://madsstudy.blog.51cto.com/6249249/1770483


4.2 使用建议

Window 下开发工具较多,优先在Windows环境开发

Linux环境适用于代码调试和简单的运维脚本编写


五、第一个Python程序

mads@mads-virtual-machine:~/python$cathello.py#!/usr/bin/envpython3#明确的指出hello.py脚本由python解释器来执行#_*_coding:utf-8_*_print("HelloWorld!")mads@mads-virtual-machine:~/python$chmod+xhello.py#执行前需给予hello.py执行权限mads@mads-virtual-machine:~/python$./hello.pyHelloWorld!

对比其他语言hello world

http://madsstudy.blog.51cto.com/6249249/1771215


六、变量

6.1 变量声明

#!/usr/bin/envpython#encoding:utf-8name="zhangsan"

注:声明了一个变量,变量名为: name,变量name的值为:"zhangsan" 


6.2 变量定义规则

1)变量名只能是 字母、数字或下划线的任意组合

2)变量名的第一个字符不能是数字,可以是下划线及字母

3)不能与Python关键字重复

[‘and‘,‘as‘,‘assert‘,‘break‘,‘class‘,‘continue‘,‘def‘,‘del‘,‘elif‘,‘else‘,‘except‘,‘exec‘,‘finally‘,‘for‘,‘from‘,‘global‘,‘if‘,‘import‘,‘in‘,‘is‘,‘lambda‘,‘not‘,‘or‘,‘pass‘,‘print‘,‘raise‘,‘return‘,‘try‘,‘while‘,‘with‘,‘yield‘]


6.3 变量赋值

name1="zhangsan"name2=name1print(name1,name2)print(id(name1),id(name2))name1="lisi"print(name1,name2)print(id(name1),id(name2))

解释为何最后name1 与 name2的值不同?
1)给name1变量分配一个内存地址存放name的值zhangsan
2)把name2变量的值等于nam1e内存地址中存放的值zhangsan

3)将name1的变量值修改为lisi,是重新分配一个内存地址存放name1的新值,此时 name1=lisi 而name2不变等于zhangsan


七、字符编码

字符编码出现的时间:ASCII>Unicode>UTF-8

三种编码的特点

ASCII:

Unicode:

UTF-8:

Python解释器在加载.py文件中的代码时,会对内容进行编码(默认ASCII),如下报错:

mads@mads-virtual-machine:~/python$cathello.py#!/usr/bin/envpythonprint(‘你好‘)mads@mads-virtual-machine:~/python$pythonhello.pyFile"hello2.py",line3SyntaxError:Non-ASCIIcharacter‘\xe4‘infilehello2.pyonline3,butnoencodingdeclared;seehttp://python.org/dev/peps/pep-0263/fordetails

解决:

mads@mads-virtual-machine:~/python$cathello.py#!/usr/bin/envpython#_*_coding:utf-8_*_print(u‘你好‘)mads@mads-virtual-machine:~/python$pythonhello.py你好


Python 3.X已经不存在这个问题

mads@mads-virtual-machine:~/python$cathello.py#!/usr/bin/envpython3print(‘你好‘)mads@mads-virtual-machine:~/python$python3hello.py你好


八、注释

单行注释:# 我被注释掉了,仅到行尾为止

多行注释:‘‘‘ 在这个范围内的所有内容都经被注释‘‘‘


九、用户输入

9.1 常规使用

#!/usr/bin/envpython3#encoding:utf-8name=input("Pleaseinputyourname:")#Python3.xname=raw_input("Pleaseinputyourname:")#Python2.xprint(name)


9.2 隐藏输入内容实现

getpass模块(适用范围 linux可以, window不行; Python 2.x不行,Python 3.x可以)

mads@mads-virtual-machine:~/python$cattest_getpass.py#!/usr/bin/python#-*-coding:utf-8-*-importgetpassusername=input("username:")password=getpass.getpass("password:")print(username,password)mads@mads-virtual-machine:~/python$python3test_getpass.pyusername:rootpassword:rootsss


十、模块

Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持

sys:

mads@mads-virtual-machine:~/python$catinput.py#!/usr/bin/envpython#_*_coding:utf-8_*_importsysiflen(sys.argv)<2:print"\033[32;1m"+"Usages:HostPort"+"\033[0m"sys.exit(2)host=sys.argv[1]port=sys.argv[2]print"host:%sport:%s"%(host,port)mads@mads-virtual-machine:~/python$pythoninput.py192.168.1.122host:192.168.1.1port:22


os:

>>>importos>>>os.mkdir(‘student‘)#新建目录>>>os.system(‘ls-l‘)#查看当前目录内容总用量20-rwxrwxr-x1madsmads835月815:39hello.pydrwxrwxr-x2madsmads40965月818:02__pycache__drwxrwxr-x2madsmads40965月818:16student-rwxrwxr-x1madsmads1515月818:00test_getpass.py-rwxrwxr-x1madsmads735月515:53test.py0>>>cmd_res=os.system(‘ls-l‘)#仅保存命令执行的返回值总用量20-rwxrwxr-x1madsmads835月815:39hello.pydrwxrwxr-x2madsmads40965月818:02__pycache__drwxrwxr-x2madsmads40965月818:16student-rwxrwxr-x1madsmads1515月818:00test_getpass.py-rwxrwxr-x1madsmads735月515:53test.py>>>printcmd_res0>>>cmd_res=os.popen(‘ls-l‘).read()#保存命令的执行结果>>>printcmd_res总用量20-rwxrwxr-x1madsmads835月815:39hello.pydrwxrwxr-x2madsmads40965月818:02__pycache__drwxrwxr-x2madsmads40965月818:16student-rwxrwxr-x1madsmads1515月818:00test_getpass.py-rwxrwxr-x1madsmads735月515:53test.py


sys和os结合

importsys,osos.system(‘‘.join(sys.argv[1:]))


编写tab补全模块

windows环境


linux环境

#!/usr/bin/envpython#pythonstartupfileimportsysimportreadlineimportrlcompleterimportatexitimportos#tabcompletionreadline.parse_and_bind(‘tab:complete‘)#historyfilehistfile=os.path.join(os.environ[‘HOME‘],‘.pythonhistory‘)try:readline.read_history_file(histfile)exceptIOError:passatexit.register(readline.write_history_file,histfile)delos,histfile,readline,rlcompleter


mac环境

importsysimportreadlineimportrlcompleterifsys.platform==‘darwin‘andsys.version_info[0]==2:readline.parse_and_bind("bind^Irl_complete")else:readline.parse_and_bind("tab:complete")#linuxandpython3onmac

注:保存内容为tab.py,使用import tab命令引用该模块。如果想在系统的何何一个地方都使用,要把这个tab.py放到python全局环境变量目录里。print(sys.path) 可以查看python环境变量列表,基本一般都放在一个叫 Python/2.7/site-packages 目录下,这个目录在不同的OS里放的位置不一样。

注意:环境列表的第一个‘ ‘表示当前目录,即环境查找的顺序问题



Python基础01-20160508

评论关闭