Python介绍

Python介绍
发展史

  • 1989年,为了打发圣诞节假期,Guido开始写Python语言的编译器。Python这个名字,来自Guido所挚爱的电视剧Monty Python’s Flying Circus。他希望这个新的叫做Python的语言,能符合他的理想:创造一种C和shell之间,功能全面,易学易用,可拓展的语言。
  • 1991年,第一个Python编译器诞生。它是用C语言实现的,并能够调用C语言的库文件。从一出生,Python已经具有了:类,函数,异常处理,包含表和词典在内的核心数据类型,以及模块为基础的拓展系统。
  • Granddaddy of Python web frameworks, Zope 1 was released in 1999
  • Python 1.0 - January 1994 增加了 lambdamapfilter and reduce.
  • Python 2.0 - October 16, 2000,加入了内存回收机制,构成了现在Python语言框架的基础
  • Python 2.4 - November 30, 2004, 同年目前最流行的WEB框架Django 诞生
  • Python 2.5 - September 19, 2006
  • Python 2.6 - October 1, 2008
  • Python 2.7 - July 3, 2010
  • In November 2014, it was announced that Python 2.7 would be supported until 2020, and reaffirmed that there would be no 2.8 release as users were expected to move to Python 3.4+ as soon as possible
  • Python 3.0 - December 3, 2008
  • Python 3.1 - June 27, 2009
  • Python 3.2 - February 20, 2011
  • Python 3.3 - September 29, 2012
  • Python 3.4 - March 16, 2014
  • Python 3.5 - September 13, 2015

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.: 

 1 print("There are <", 2**32, "> possibilities!", sep="") 

ALL IS UNICODE NOW

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

 

还可以这样玩: (A,*REST,B)=RANGE(5)

1 <strong>>>> a,*rest,b = range(5)
2 >>> a,rest,b
3 (0, [1, 2, 3], 4)
4 </strong>

 

 


被解救的姜戈
2.4 50万行
Python 2.6 - October 1, 2008
Python 2.6.1 - October 1, 2008
Python 2.6.6 - October 1, 2008
Python 3.0 - December 3, 2008
Python 2.7 - July 3, 2010 #目前业内主流使用的工业版本依然是2.7

print "hello world" #in 2.x
print("hello world") #in 3.x


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

总结: python2.x 是遗产, python3.x是现在和未来的语言

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.

2.x = 默认编码 =ASSIC =不支持
3.x = 默认编码 =UNICODE =默认支持中文

 

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).

 

python 2 vs 3
1。 默认支持中文
2. 不兼容2.x
3. 核心语法调整,更易学
4. 新特性默认只在3.x上有



系统位数

32bit =内存的最大寻址空间是2**32, 4GB
64bit, =2**64 但实际上支持不到这莫大的内存,2**4x, 目前主板支持的最大的内存是100多GB


4gb ==64位系统 会不会比32位系统快? = 一样的

硬盘:
5400转 = 每分钟 =骑自行车
7200转 = 每分钟 =骑电动车
10000转 = 每分钟 =骑快速电动车
15000转 = 每分钟 =骑摩托车 机械硬盘最快的

SSD = Tesla




Python 2 or 3?
安装
交互器模式

开始--》cmd --> cd c:\ -->dir
cd = change directory
dir = 查看当前目录文件列表


cd .. 返回上一层目录
cd ../.. 返回上上一层目录
cd ../../.. 返回上上上一层目录
cd ../../../.. 返回上上上上一层目录


D:\>"d:\Program Files\Python35\python.exe" c:\hello.txt
Hello World!


#c:\hello.txt .txt 代表文件的扩展名(后缀名),用于区分文件类型
.txt 记事本文本文件
.doc word文件
.xls excel文件
.ppt PPT文件
.exe 可执行文件
.jpg .png .jpeg 图片
.gif 动态图片
.pdf PDF文件
.mp4 .avi 视频
.py python文件
.java java 文件
.c .h c源码
.php php文件
.js javascript

环境变量

D:\Program Files\Python35\Scripts\;
D:\ProgramFiles\Python35\;
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;
D:\Program Files\Python27


执行py程序方式为:
1. 交互器,缺点程序不能永久保存,主要用与简单的语法测试相关
2. 文件执行


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.

变量的命名规则
1. 要具有描述性
2. 变量名只能_,数字,字母组成,不可以是空格或特殊字符(#?<.,¥$*!~)
3. 不能以中文为变量名
4. 不能以数字开头
5. 保留字符是不能被使用



常量 :不变的量 pie = 3.141592653....
在py里面所有的变量都是可变的 ,所以用全部大写的变量名来代表次变量为常量


内存何时释放?


字符编码
支持中文的第一张表就叫 GB2312

1980 gb2312 6700+
1995 gbk1.0 20000
2000 gb18030 27000
big5 台湾

unicode 万国码 支持所有国家和地区的编码
2**16 = 65535 = 存一个字符 统一占用2个字节


UTF-8 = unicode 的扩展集,可变长的字符编码集

Assic -->Gb2312 ->gbk1.0-->gb18030
Assic -->unicode -->utf-8 /utf-16


Python2.x == Assic 默认编码
#!-*- coding:utf-8 -*-
#coding:utf-8

python3.x == unicode默认编码


unicode 是向下兼容gb2312 , gbk


注释
单行注释 用#
多行注释用三个单引号或三个双引号 '''被注释的内容'''












用户输入

 

模块初识
.pyc是个什么鬼?
数据类型初识
数据运算
表达式if ...else语句
缩进 IndentationError: expected an indented block
^
IndentationError: unindent does not match any outer indentation level
SyntaxError: invalid syntax 语法错误
tab != 4个空格

缩进级别必须保持一致

表达式for 循环
break and continue
表达式while 循环

四、Python安装

windows

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

linux、Mac

1
2
3
无需安装,原装Python环境
  
ps:如果自带2.6,请更新至2.7

  

五、Hello World程序

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

1
print("Hello World!")

然后执行命令:python hello.py ,输出

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

指定解释器

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

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

1
2
3
#!/usr/bin/env python
  
print "hello,world"

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

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

在交互器中执行 

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

1
2
3
4
5
6
localhost:~ jieli$ python
Python 2.7.10 (default, Oct 23 201518: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!
posted @ 2018-07-03 18:19  花落灬终会开  阅读(121)  评论(0编辑  收藏  举报