码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础01-20160508

时间:2016-05-09 07:17:40      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:users   历史   

一、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>python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (
Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Hello World"
Hello World
>>>

C:\Users\Administrator>python3
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:16:59) [MSC v.1900 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Hello World"
  File "<stdin>", line 1
    print "Hello World"
                      ^
SyntaxError: Missing parentheses in call to ‘print‘
>>> print("Hello World")
Hello World

3)某些库改名了

4)用户输入

Python 2.x:raw_input
Python 3.x:input

四、Python安装

4.1 安装介绍

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


4.2 使用建议

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

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


五、第一个Python程序

mads@mads-virtual-machine:~/python$ cat hello.py 
#!/usr/bin/env python3         # 明确的指出hello.py脚本由python解释器来执行
#_*_coding:utf-8_*_

print("Hello World!")

mads@mads-virtual-machine:~/python$ chmod +x hello.py # 执行前需给予hello.py执行权限    
mads@mads-virtual-machine:~/python$ ./hello.py 
Hello World!

对比其他语言hello world

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


六、变量

6.1 变量声明

#!/usr/bin/env python
# encoding: utf-8
 
name = "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 = name1
print(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$ cat hello.py 
#!/usr/bin/env python
print(‘你好‘)
mads@mads-virtual-machine:~/python$ python hello.py 
  File "hello2.py", line 3
SyntaxError: Non-ASCII character ‘\xe4‘ in file hello2.py on line 3, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

解决:

mads@mads-virtual-machine:~/python$ cat hello.py
#!/usr/bin/env python
#_*_coding:utf-8_*_
print(u‘你好‘)
mads@mads-virtual-machine:~/python$ python hello.py
你好


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

mads@mads-virtual-machine:~/python$ cat hello.py
#!/usr/bin/env python3
print(‘你好‘)
mads@mads-virtual-machine:~/python$ python3 hello.py
你好


八、注释

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

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


九、用户输入

9.1 常规使用

#!/usr/bin/env python3
# encoding: utf-8

name = input("Please input your name:")      # Python 3.x
name = raw_input("Please input your name:")  # Python 2.x
print(name)


9.2 隐藏输入内容实现

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

mads@mads-virtual-machine:~/python$ cat test_getpass.py
#!/usr/bin/python
#-*-coding:utf-8-*-
 
import getpass
 
username = input("username:")
password = getpass.getpass("password:")
print (username,password)
mads@mads-virtual-machine:~/python$ python3 test_getpass.py 
username:root
password:
root sss


十、模块

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

sys:

mads@mads-virtual-machine:~/python$ cat input.py 
#!/usr/bin/env python
#_*_coding:utf-8_*_
import sys
if len(sys.argv)<2:
   print "\033[32;1m"+"Usages: Host Port"+"\033[0m"
   sys.exit(2)
   
host = sys.argv[1]
port = sys.argv[2]

print "host:%s port:%s" % (host,port)
mads@mads-virtual-machine:~/python$ python input.py 192.168.1.1 22
host:192.168.1.1 port:22


os:

>>> import os
>>> os.mkdir(‘student‘)                  # 新建目录
>>> os.system(‘ls -l‘)                    # 查看当前目录内容总用量 20
-rwxrwxr-x 1 mads mads   83 5月   8 15:39 hello.py
drwxrwxr-x 2 mads mads 4096 5月   8 18:02 __pycache__
drwxrwxr-x 2 mads mads 4096 5月   8 18:16 student
-rwxrwxr-x 1 mads mads  151 5月   8 18:00 test_getpass.py
-rwxrwxr-x 1 mads mads   73 5月   5 15:53 test.py0
>>> cmd_res = os.system(‘ls -l‘)          # 仅保存命令执行的返回值总用量 20
-rwxrwxr-x 1 mads mads   83 5月   8 15:39 hello.py
drwxrwxr-x 2 mads mads 4096 5月   8 18:02 __pycache__
drwxrwxr-x 2 mads mads 4096 5月   8 18:16 student
-rwxrwxr-x 1 mads mads  151 5月   8 18:00 test_getpass.py
-rwxrwxr-x 1 mads mads   73 5月   5 15:53 test.py
>>> print cmd_res0    
>>> cmd_res = os.popen(‘ls -l‘).read()    # 保存命令的执行结果
>>> print cmd_res                     
总用量 20
-rwxrwxr-x 1 mads mads   83 5月   8 15:39 hello.py
drwxrwxr-x 2 mads mads 4096 5月   8 18:02 __pycache__
drwxrwxr-x 2 mads mads 4096 5月   8 18:16 student
-rwxrwxr-x 1 mads mads  151 5月   8 18:00 test_getpass.py
-rwxrwxr-x 1 mads mads   73 5月   5 15:53 test.py


sys和os结合

import sys,os
os.system(‘‘.join(sys.argv[1:]))


编写tab补全模块

windows环境


linux环境

#!/usr/bin/env python 
# python startup file 
import sys
import readline
import rlcompleter
import atexit
import os
# tab completion 
readline.parse_and_bind(‘tab: complete‘)
# history file 
histfile = os.path.join(os.environ[‘HOME‘], ‘.pythonhistory‘)
try:
    readline.read_history_file(histfile)
except IOError:
    pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter


mac环境

import sys
import readline
import rlcompleter
if sys.platform == ‘darwin‘ and sys.version_info[0] == 2:
    readline.parse_and_bind("bind ^I rl_complete")
else:
    readline.parse_and_bind("tab: complete")  # linux and python3 on mac

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

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



Python基础01-20160508

标签:users   历史   

原文地址:http://madsstudy.blog.51cto.com/6249249/1771271

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!