Python之路,Day01

Python介绍


Python的创始人为吉多·范罗苏姆(Guido van Rossum)。
Python可以应用于众多领域,如:数据分析、组件集成、网络服务、图像处理、数值计算和科学计算等众多领域。目前业内几乎所有大中型互联网企业都在使用Python,如:Youtube、Dropbox、BT、Quora(中国知乎)、豆瓣、知乎、Google、Yahoo!、Facebook、NASA、百度、腾讯、汽车之家、美团等。

目前Python主要应用领域:

云计算:云计算最火的语言,典型应用OpenStack
WEB开发:众多优秀的WEB框架,众多大型网站均为Python开发,Youtube,Dropbox,豆瓣,典型WEB框架有Django
科学运算、人工智能:典型库NumPy,SciPy,Matplotlib,Enthoughtlibrarys,pandas
系统运维:运维人员必备语言
金融:量化交易,金融分析,在金融工程领域,Python不但在用,且用的最多,而且重要性逐年提高。原因:作为动态语言的Python,语言结构清晰简单,库丰富,成熟稳定,科学计算和统计分析都很牛逼,生产效率远远高于c,c++,java,尤其擅长策略回测
图形GUI:PyQT,WxPython,TkInter

 

编译型vs解释型

编译型
优点:编译器一般会有预编译的过程对代码进行优化。因为编译只做一次,运行时不需要编译,所以编译型语言的程序执行效率高。可以脱离语言环境独立运行。

缺点:编译之后如果需要修改就需要整个模块重新编译。编译的时候根据对应的运行环境生成机器码,不同的操作系统之间移植就会有问题,需要根据运行的操作系统环境编译不同的可执行文件。

解释型
优点:有良好的平台兼容性,在任何环境中都可以运行,前提是安装了解释器(虚拟机)。灵活,修改代码的时候直接修改就可以,可以快速部署,不用停机维护。

缺点:每次运行的时候都要解释一遍,性能上不如编译型语言。

 

Python的解释器很多,但使用最广泛的还是CPython。

 

Python 2 or 3?


In summary : Python 2.x is legacy, Python 3.x is the present and future of the language

Python 3.0 was released in 2008. The final 2.x version 2.7 release came out in mid-2010, with a statement of

extended support for this end-of-life release. The 2.x branch will see no new major releases after that. 3.x is

under active development and has already seen over five years of stable releases, including version 3.3 in 2012,

3.4 in 2014, and 3.5 in 2015. This means that all recent standard library improvements, for example, are only

available by default in Python 3.x.

Guido van Rossum (the original creator of the Python language) decided to clean up Python 2.x properly, with less regard for backwards compatibility than is the case for new releases in the 2.x range. The most drastic improvement is the better Unicode support (with all text strings being Unicode by default) as well as saner bytes/Unicode separation.

Besides, several aspects of the core language (such as print and exec being statements, integers using floor division) have been adjusted to be easier for newcomers to learn and to be more consistent with the rest of the language, and old cruft has been removed (for example, all classes are now new-style, "range()" returns a memory efficient iterable, not a list as in 2.x).

 

py2与3的详细区别


PRINT IS A FUNCTION

The statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old statement (PEP 3105). Examples:

Old: print "The answer is", 2*2 New: print("The answer is", 2*2)
Old: print x, # Trailing comma suppresses newline New: print(x, end=" ") # Appends a space instead of a newline
Old: print # Prints a newline
New: print() # You must call the function!
Old: print >>sys.stderr, "fatal error" New: print("fatal error", file=sys.stderr)
Old: print (x, y) # prints repr((x, y))
New: print((x, y)) # Not the same as print(x, y)!
You can also customize the separator between items, e.g.:

print("There are <", 2**32, "> possibilities!", sep="")
ALL IS UNICODE NOW

从此不再为讨厌的字符编码而烦恼


Python安装

Windows
1、下载安装包
https://www.python.org/downloads/
2、安装
默认安装路径:C:\python27
3、配置环境变量
【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【Python安装目录追加到变值值中,用 ; 分割】
如:原来的值;C:\python27,切记前面有分号

Linux、Mac

无需安装,原装Python环境

ps:如果自带2.6,请更新至2.7

 

 

Hello World程序


在linux 下创建一个文件叫hello.py,并输入

print("Hello World!")
然后执行命令:python hello.py ,输出

localhost:~ jieli$ vim hello.py
localhost:~ jieli$ python hello.py
Hello World!

指定解释器

上一步中执行 python hello.py 时,明确的指出 hello.py 脚本由 python 解释器来执行。

如果想要类似于执行shell脚本一样执行python脚本,例: ./hello.py ,那么就需要在 hello.py 文件的头部指定解释器,如下:

#!/usr/bin/env python

print "hello,world"如此一来,执行: ./hello.py 即可。

ps:执行前需给予 hello.py 执行权限,chmod 755 hello.py

在交互器中执行 

除了把程序写在文件里,还可以直接调用python自带的交互器运行代码, 

localhost:~ jieli$ python
Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World!")
Hello World!


变量、字符编码


Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.

声明变量

#_*_coding:utf-8_*_

name = "Alex Li"
上述代码声明了一个变量,变量名为: name,变量name的值为:"Alex Li" 

变量定义的规则:

变量名只能是 字母、数字或下划线的任意组合
变量名的第一个字符不能是数字
以下关键字不能声明为变量名
['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']

 

变量的赋值

name = "Alex Li"

name2 = name
print(name,name2)

name = "Jack"

print("What is the value of name2 now?")

 

字符编码


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

ASCII码无法将世界上的各种文字和符号全部表示,所以,就需要新出一种可以代表所有字符和符号的编码,即:Unicode

Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,规定虽有的字符和符号最少由 16 位来表示(2个字节),即:2 **16 = 65536,
注:此处说的的是最少2个字节,可能更多

UTF-8,是对Unicode编码的压缩和优化,他不再使用最少使用2个字节,而是将所有的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存...

所以,python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill),如果是如下代码的话:

报错:ascii码无法表示中文

#!/usr/bin/env python

print "你好,世界"
改正:应该显示的告诉python解释器,用什么编码来执行源代码,即:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

print "你好,世界"

 

注释
  当行注视:# 被注释内容

  多行注释:""" 被注释内容 """

 

用户输入 


#!/usr/bin/env python
#_*_coding:utf-8_*_


#name = raw_input("What is your name?") #only on python 2.x
name = input("What is your name?")
print("Hello " + name )
输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import getpass

# 将用户输入的内容赋值给 name 变量
pwd = getpass.getpass("请输入密码:")

# 打印输入的内容
print(pwd)

 

# 仅作记录。

posted @ 2018-03-07 20:55  ueue  阅读(202)  评论(0编辑  收藏  举报