python的创始人为吉多·范罗苏姆(Guido van Rossum),从2005年开始就任于Google公司。php
Python是线上和线下的一种重要的编程语言。Python社区的人赋予他“仁慈大君” 的称号,这一称号直接来自英国肥皂剧《Monty Python飞行马戏团》。Guido当初之因此选中Python做为程序的名字,是由于他太喜欢这部肥皂剧了。html
Guido原居荷兰,1995移居到美国,并遇到了他如今的妻子。在2003年初,Guido和他的家人,包括他2001年出生的儿子Orlijn一直居住在华盛顿洲北弗吉尼亚的郊区。随后他们搬迁到硅谷,如今Guido在为Google工做(其中有一半时间是花在Python上)。前端
Guido在1982年得到阿姆斯特丹大学的数学和计算机科学的硕士学位,并于同年加入一个多媒体组织CWI,作调研员。1989年,他创立了Python语言。java
最新的TIOBE排行榜,Python赶超PHP占据第五, Python崇尚优美、清晰、简单,是一个优秀并普遍使用的语言。python
由上图可见,Python总体呈上升趋势,反映出Python应用愈来愈普遍而且也逐渐获得业内的承认!!!linux
Python能够应用于众多领域,如:数据分析、组件集成、网络服务、图像处理、数值计算和科学计算等众多领域。目前业内几乎全部大中型互联网企业都在使用Python,如:Youtube、Dropbox、BT、Quora(中国知乎)、豆瓣、知乎、Google、Yahoo!、Facebook、NASA、百度、腾讯、汽车之家、美团等c++
目前Python主要应用领域:程序员
Python在一些公司的应用: web
Python的设计哲学是“优雅”、“明确”、“简单”。Python开发者的哲学是“用一种方法,最好是只有一种方法来作一件事”。在设计Python语言时,若是面临多种选择,Python开发者通常会拒绝花俏的语法,而选择明确没有或者不多有歧义的语法。这些准则被称为“ Python格言”。在Python解释器内运行import this能够得到完整的列表算法
1.优美胜于丑陋(Python 以编写优美的代码为目标)
2.明了胜于晦涩(优美的代码应当是明了的,命名规范,风格类似)
3.简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现)
4.复杂胜于凌乱(若是复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁,多加注释!)
5.扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套)
6.间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题,一行代码不能超过80个字符,你能够换行或起一个新的逻辑来写)
7.可读性很重要(优美的代码是可读的)
8.即使假借特例的实用性之名,也不可违背这些规则(这些规则至高无上),觉对不容许特列必须按照这个规则
9.不要包容全部错误,除非你肯定须要这样作(精准地捕获异常,不写 except:pass 风格的代码)
10.当存在多种可能,不要尝试去猜想!
11.而是尽可能找一种,最好是惟一一种明显的解决方案(若是不肯定,就用穷举法)
12.虽然这并不容易,由于你不是 Python 之父(这里的 Dutch 是指 Guido )
13.作也许好过不作,但不假思索就动手还不如不作(动手以前要细思量)
14.若是你没法向人描述你的方案,那确定不是一个好方案;反之亦然(方案测评标准)
15.命名空间是一种绝妙的理念,咱们应当多加利用(倡导与号召)
先看优势
再看缺点:
固然,Python还有一些其它的小缺点,在这就不一一列举了,我想说的是,任何一门语言都不是完美的,都有擅长和不擅长作的事情,建议各位不要拿一个语言的劣势去跟另外一个语言的优点来去比较,语言只是一个工具,是实现程序设计师思想的工具,就像咱们以前中学学几什么时候,有的时候须要要圆规,有的时候须要用三角尺同样,拿相应的工具去作它最擅长的事才是正确的选择。以前不少人问我Shell和Python到底哪一个好?我回答说Shell是个脚本语言,但Python不仅是个脚本语言,能作的事情更多,而后又有钻牛角尖的人说彻底不必学Python, Python能作的事情Shell均可以作,只要你足够牛B,而后又举了用Shell能够写俄罗斯方块这样的游戏,对此我能说表达只能是,不要跟SB理论,SB会把你拉到跟他同样的高度,而后用充分的经验把你打倒
Python和其余语言的比较
Python 和 php
python是综合性的语言,他不光能够作web开发。他俩的对比应该仅限于web开发方向。不见其phpweb开发别python好,php是先入为主,python的web框架在2000才有的比较晚了
django起来的比较晚,他们的web开发都是很不错了。你用php作的python均可以作。python给你的是一个完整的解决,前端和后端均可以用python。
Python 和 c
C语言: 代码编译获得 机器码 ,机器码在处理器上直接执行,每一条指令控制CPU工做
其余语言: 代码编译获得 字节码 ,虚拟机执行字节码并转换成机器码再后在处理器上执行
Python 和 C Python这门语言是由C开发而来
对于使用:Python的类库齐全而且使用简洁,若是要实现一样的功能,Python 10行代码能够解决,C可能就须要100行甚至更多.
对于速度:Python的运行速度相较与C,相同的程序Python比C慢几百倍很正常
程序的快慢很大程度上程序员逻辑、SQL、算法,比起O(n)和O(n^2)的区别, 语言间的差别就显得微乎其微
Python 和 Java、C#等
对于使用:Linux原装Python,其余语言没有;以上几门语言都有很是丰富的类库支持
对于速度:Python在速度上可能稍显逊色
因此,Python和其余语言没有什么本质区别,其余区别在于:擅长某领域、人才丰富、先入为主。
以上除PyPy以外,其余的Python的对应关系和执行流程以下:
PyPy,在Python的基础上对Python的字节码进一步处理,从而提高执行速度!
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).
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:
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
2
3
4
|
<strong>>>> a,
*
rest,b
=
range
(
5
)
>>> a,rest,b
(
0
, [
1
,
2
,
3
],
4
)
<
/
strong>
|
某些库更名了
Old Name |
New Name |
_winreg |
winreg |
ConfigParser |
configparser |
copy_reg |
copyreg |
Queue |
queue |
SocketServer |
socketserver |
markupbase |
_markupbase |
repr |
reprlib |
test.test_support |
test.support |
还有谁不支持PYTHON3?
One popular module that don't yet support Python 3 is Twisted (for networking and other applications). Most
actively maintained libraries have people working on 3.x support. For some libraries, it's more of a priority than
others: Twisted, for example, is mostly focused on production servers, where supporting older versions of
Python is important, let alone supporting a new version that includes major changes to the language. (Twisted is
a prime example of a major package where porting to 3.x is far from tri
windows:
12345671
、下载安装包
https:
/
/
www.python.org
/
downloads
/
2
、安装
默认安装路径:C:\python35
3
、配置环境变量
【右键计算机】
-
-
》【属性】
-
-
》【高级系统设置】
-
-
》【高级】
-
-
》【环境变量】
-
-
》【在第二个内容框中找到 变量名为Path 的一行,双击】
-
-
> 【Python安装目录追加到变值值中,用 ; 分割】
如:原来的值;C:\python35,切记前面有分号
linux:
123无需安装,原装Python环境
ps:若是自带
2.6
,请更新至
2.7
更新Python
windows:
1卸载重装便可
linux:
Linux的yum依赖自带Python,为防止错误,此处更新其实就是再安装一个Python
12345678910111213141516171819查看默认Python版本
python
-
V
1
、安装gcc,用于编译Python源码
yum install gcc
2
、下载源码包,https:
/
/
www.python.org
/
ftp
/
python
/
3
、解压并进入源码文件
4
、编译安装
.
/
configure
make
all
make install
5
、查看版本
/
usr
/
local
/
bin
/
python2.
7
-
V
6
、修改默认Python版本
mv
/
usr
/
bin
/
python
/
usr
/
bin
/
python2.
6
ln
-
s
/
usr
/
local
/
bin
/
python2.
7
/
usr
/
bin
/
python
7
、防止yum执行异常,修改yum使用的Python版本
vi
/
usr
/
bin
/
yum
将头部
#!/usr/bin/python 修改成 #!/usr/bin/python2.6
在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
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.
声明变量
1
2
3
|
#_*_coding:utf-8_*_
name
=
"Alex Li"
|
上述代码声明了一个变量,变量名为: name,变量name的值为:"Alex Li"
变量定义的规则:
变量的赋值
1
2
3
4
5
6
7
8
|
name
=
"Alex Li"
name2
=
name
print
(name,name2)
name
=
"Jack"
print
(
"What is the value of name2 now?"
)
|
python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill)
ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其余西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256-1,因此,ASCII码最多只能表示 255 个符号。
关于中文
为了处理汉字,程序员设计了用于简体中文的GB2312和用于繁体中文的big5。
GB2312(1980年)一共收录了7445个字符,包括6763个汉字和682个其它符号。汉字区的内码范围高字节从B0-F7,低字节从A1-FE,占用的码位是72*94=6768。其中有5个空位是D7FA-D7FE。
GB2312 支持的汉字太少。1995年的汉字扩展规范GBK1.0收录了21886个符号,它分为汉字区和图形符号区。汉字区包括21003个字符。2000年的 GB18030是取代GBK1.0的正式国家标准。该标准收录了27484个汉字,同时还收录了藏文、蒙文、维吾尔文等主要的少数民族文字。如今的PC平台必须支持GB18030,对嵌入式产品暂不做要求。因此手机、MP3通常只支持GB2312。
从ASCII、GB23十二、GBK 到GB18030,这些编码方法是向下兼容的,即同一个字符在这些方案中老是有相同的编码,后面的标准支持更多的字符。在这些编码中,英文和中文能够统一地处理。区分中文编码的方法是高字节的最高位不为0。按照程序员的称呼,GB23十二、GBK到GB18030都属于双字节字符集 (DBCS)。
有的中文Windows的缺省内码仍是GBK,能够经过GB18030升级包升级到GB18030。不过GB18030相对GBK增长的字符,普通人是很难用到的,一般咱们仍是用GBK指代中文Windows内码。
显然ASCII码没法将世界上的各类文字和符号所有表示,因此,就须要新出一种能够表明全部字符和符号的编码,即:Unicode
Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每一个字符设定了统一而且惟一的二进制编码,规定虽有的字符和符号最少由 16 位来表示(2个字节),即:2 **16 = 65536,
注:此处说的的是最少2个字节,可能更多
UTF-8,是对Unicode编码的压缩和优化,他再也不使用最少使用2个字节,而是将全部的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存...
因此,python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill),若是是以下代码的话:
报错:ascii码没法表示中文
1
2
3
|
#!/usr/bin/env python
print
"你好,世界"
|
改正:应该显示的告诉python解释器,用什么编码来执行源代码,即:
1
2
3
4
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
print
"你好,世界"
|
1字节=8bit 又叫8位,最先的ASCII码就是占一个字节(8位) 8位也就是二进制0000 0000,二进制8位最大表示数字位255,因为老外并不知道地球的另外一边有其余世界的存在,因此他就在0-255之间选择0-127做为英文的存放,还预留了一半的空间做为扩展。但后后来中国人发现预留的这些空间远远不够存放中国博大精深的全部汉字,可是中国人有办法,中国人在128-255中间取了一部分用于存放汉字的索引,也能够说只要查找的位置落到了这一部分就表明着要存放汉字,在1980年中国的人们本身又建立了一个表存放中国汉字就产生了GB2312其中共收录了7445个字符,包括6763个汉字和682个其它符号;可是中国实在是博大精深在1995年又诞生了GBK编码共收录21886个符号,它分为汉字区和图形符号区,汉字区包括21003个字符。2000年的 GB18030是取代GBK1.0的正式国家标准。该标准收录了27484个汉字,同时还收录了藏文、蒙文、维吾尔文等主要的少数民族文字。如今的PC平台必须支持GB18030,对嵌入式产品暂不做要求。因此手机、MP3通常只支持GB2312。
因为Unicode浪费了存储空间,不管存放中文仍是英文都要是用16位来存放,平时50G的数据,使用Unicode就要使用100G,这显然不合适,这样就有了UTF-8的编码,这个编码存放英文将占用1个字节,存放中文占用3个字节。
当行注视:# 被注释内容
多行注释:""" 被注释内容 """
Python算术运算符:
操做符 | 描述符 | 例子 |
---|---|---|
+ | 加法 - 对操做符的两侧增长值 | a + b = 30 |
- | 减法 - 减去从左侧操做数右侧操做数 | a - b = -10 |
* | 乘法 - 相乘的运算符两侧的值 | a * b = 200 |
/ | 除 - 由右侧操做数除以左侧操做数 | b / a = 2 |
% | 模 - 由右侧操做数和余返回除以左侧操做数 | b % a = 0 |
** | 指数- 执行对操做指数(幂)的计算 | a**b = 10 的幂 20 |
// | 地板除 - 操做数的除法,其中结果是将小数点后的位数被除去的商。 | 9//2 = 4 而 9.0//2.0 = 4.0 |
1
2
3
4
5
6
7
8
|
>>> a
=
21
>>> b
=
10
>>> c
=
0
>>> c
=
a
+
b ;
print
c
31
>>> c
=
a
-
b ;
print
c11>>> c
=
a
*
b ;
print
c210
>>> c
=
c
/
b ;
print
c
21
>>> c
=
c
%
b ;
print
c
1
|
Python的比较操做符:
运算符 | 描述 | 示例 |
---|---|---|
== | 检查,两个操做数的值是否相等,若是是则条件变为真。 | (a == b) 不为 true. |
!= | 检查两个操做数的值是否相等,若是值不相等,则条件变为真。 | (a != b) 为 true. |
<> | 检查两个操做数的值是否相等,若是值不相等,则条件变为真。 | (a <> b) 为 true。这个相似于 != 运算符 |
> | 检查左操做数的值是否大于右操做数的值,若是是,则条件成立。 | (a > b) 不为 true. |
< | 检查左操做数的值是否小于右操做数的值,若是是,则条件成立。 | (a < b) 为 true. |
>= | 检查左操做数的值是否大于或等于右操做数的值,若是是,则条件成立。 | (a >= b) 不为 true. |
<= | 检查左操做数的值是否小于或等于右操做数的值,若是是,则条件成立。 | (a <= b) 为 true. |
1
2
3
4
5
6
7
8
|
>>> a
=
21
>>> b
=
10
>>> c
=
0
>>> c
=
a
+
b ;
print
c
31
>>> c
=
a
-
b ;
print
c11>>> c
=
a
*
b ;
print
c210
>>> c
=
c
/
b ;
print
c
21
>>> c
=
c
%
b ;
print
c
1
|
Python赋值运算符:
运算符 | 描述 | 示例 |
---|---|---|
= | 简单的赋值运算符,赋值从右侧操做数左侧操做数 | c = a + b将指定的值 a + b 到 c |
+= | 加法AND赋值操做符,它增长了右操做数左操做数和结果赋给左操做数 | c += a 至关于 c = c + a |
-= | 减AND赋值操做符,它减去右边的操做数从左边操做数,并将结果赋给左操做数 | c -= a 至关于 c = c - a |
*= | 乘法AND赋值操做符,它乘以右边的操做数与左操做数,并将结果赋给左操做数 | c *= a 至关于 c = c * a |
/= | 除法AND赋值操做符,它把左操做数与正确的操做数,并将结果赋给左操做数 | c /= a 至关于= c / a |
%= | 模量AND赋值操做符,它须要使用两个操做数的模量和分配结果左操做数 | c %= a is equivalent to c = c % a |
**= | 指数AND赋值运算符,执行指数(功率)计算操做符和赋值给左操做数 | c **= a 至关于 c = c ** a |
//= | 地板除,并分配一个值,执行地板除对操做和赋值给左操做数 | c //= a 至关于 c = c // a |
1
2
3
4
5
6
7
8
|
>>> a
=
21
>>> b
=
10
>>> c
=
0
>>>
>>> c
=
a
+
b
>>> c
+
=
a ;
print
c
52
>>> c
+
=
a
is
c
=
c
+
a
|
Python位运算符:
操做符 | 描述 | 示例 |
---|---|---|
& | 二进制和复制操做了一下,结果,若是它存在于两个操做数。 | (a & b) = 12 即 0000 1100 |
| | 二进制或复制操做了一个比特,若是它存在一个操做数中。 | (a | b) = 61 即 0011 1101 |
^ | 二进制异或运算符的副本,若是它被设置在一个操做数而不是两个比特。 | (a ^ b) = 49 即 0011 0001 |
~ | 二进制的补运算符是一元的,并有“翻转”位的效果。 | (~a ) = -61 即 1100 0011以2的补码形式因为带符号二进制数。 |
<< | 二进位向左移位运算符。左操做数的值左移由右操做数指定的位数。 | a << 2 = 240 即 1111 0000 |
>> | 二进位向右移位运算符。左操做数的值是由右操做数指定的位数向右移动。 | a >> 2 = 15 即 0000 1111 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
二进制“与”计算:(
1
和
1
=
1
0
和任意都为
0
)
a
=
10
b
=
49
a&b
10
0
0
0
0
1
0
1
0
49
0
0
1
1
0
0
0
1
=
0
0
0
0
0
0
0
0
0
二进制“或”计算:(遇
1
得
1
)
a|b
a
=
60
# 60 = 0 0 1 1 1 1 0 0
b
=
13
#13 = 0 0 0 0 1 1 0 1
=
0
0
1
1
1
1
0
1
=
0
+
0
+
32
+
16
+
8
+
4
+
0
+
1
=
61
二进制“异或”运算:(相同为
0
,不一样为
1
)
a^b
a
=
60
# 60 = 0011 1100
b
=
13
# 13 = 0000 1101
0011
0001
=
0
+
0
+
32
+
16
+
0
+
0
+
0
+
1
=
49
二进制取反运算:(
0
置换为
1
,
1
置换为
0
)
~a
a
=
60
# 60 = 0011 1100
那么
-
a
=
1100
0011
128
+
64
+
2
+
1
=
二进位向左、右移位运算符:
a
=
2
那么 往左移
1
位就是
4
以下:
a<<
1
=
4
; a>>
1
=
1
|
Python逻辑运算符:
运算符 | 描述 | 示例 |
---|---|---|
and | 所谓逻辑与运算符。若是两个操做数都是真的,那么则条件成立。 | (a and b) 为 true. |
or | 所谓逻辑OR运算符。若是有两个操做数都是非零而后再条件变为真。 | (a or b) 为 true. |
not | 所谓逻辑非运算符。用于反转操做数的逻辑状态。若是一个条件为真,则逻辑非运算符将返回false。 | not(a and b) 为 false. |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
例子:
and
运算,都为真时为真
>>>
9
>
8
and
9
<
10
True
>>>
9
>
8
and
9
>
10
False
or
运算,有一个为真的时候便可为真
>>>
9
>
8
or
9
>
10
True
>>>
9
<
8
or
9
>
10
False
>>>
not
运算,假为真真为假
>>>
not
6.2
<
=
6
True
>>>
not
6.2
>
=
6
False
>>>
|
2 是一个整数的例子。
长整数 不过是大一些的整数。
3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。
(-5+4j)和(2.3-4.6j)是复数的例子,其中-5,4为实数,j为虚数,数学中表示复数是什么?。
int(整型)
"hello world"
1
2
3
4
|
name
=
"alex"
print
"i am %s "
%
name
#输出: i am alex
|
PS: 字符串是 %s;整数 %d;浮点数%f
1
2
3
|
name_list
=
[
'alex'
,
'seven'
,
'eric'
]
或
name_list =
list
([
'alex'
,
'seven'
,
'eric'
])
|
基本操做:
1
2
3
|
ages
=
(
11
,
22
,
33
,
44
,
55
)
或
ages
=
tuple
((
11
,
22
,
33
,
44
,
55
))
|
1
2
3
|
person
=
{
"name"
:
"mr.wu"
,
'age'
:
18
}
或
person
=
dict
({
"name"
:
"mr.wu"
,
'age'
:
18
})
|
经常使用操做:
1
2
3
4
5
6
7
8
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 将用户输入的内容赋值给 name 变量
name
=
raw_input
(
"请输入用户名:"
)
# 打印输入的内容
print
name
|
输入密码时,若是想要不可见,须要利用getpass 模块中的 getpass方法,即:
1
2
3
4
5
6
7
8
9
10
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
getpass
# 将用户输入的内容赋值给 name 变量
pwd
=
getpass.getpass(
"请输入密码:"
)
# 打印输入的内容
print
pwd
|
需求1、用户登录验证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#!/usr/bin/env python
# -*- coding: encoding -*-
# 提示输入用户名和密码
# 验证用户名和密码
# 若是错误,则输出用户名或密码错误
# 若是成功,则输出 欢迎,XXX!
import
getpass
name
=
raw_input
(
'请输入用户名:'
)
pwd
=
getpass.getpass(
'请输入密码:'
)
if
name
=
=
"alex"
and
pwd
=
=
"cmd"
:
print
"欢迎,alex!"
else
:
print
"用户名和密码错误"
|
需求2、根据用户输入内容输出其权限
1
2
3
4
5
6
|
# 根据用户输入内容打印其权限
# alex --> 超级管理员
# eric --> 普通管理员
# tony,rain --> 业务主管
# 其余 --> 普通用户
|
1
2
3
4
5
6
7
8
9
10
11
|
name
=
raw_input
(
'请输入用户名:'
)
if
name
=
=
"alex"
:
print
"超级管理员"
elif
name
=
=
"eric"
:
print
"普通管理员"
elif
name
=
=
"tony"
or
name
=
=
"rain"
:
print
"业务主管"
else
:
print
"普通用户"
|
一、基本循环
1
2
3
4
5
6
|
while
条件:
# 循环体
# 若是条件为真,那么循环体则执行
# 若是条件为假,那么循环体不执行
|
二、break
break用于退出全部循环
1
2
3
4
|
while
True
:
print
"123"
break
print
"456"
|
三、continue
continue用于退出当前循环,继续下一次循环
1
2
3
4
|
while
True
:
print
"123"
continue
print
"456"
|
场景1、用户登录验证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# 提示输入用户名和密码
# 验证用户名和密码
# 若是错误,则输出用户名或密码错误
# 若是成功,则输出 欢迎,XXX!
#!/usr/bin/env python
# -*- coding: encoding -*-
import
getpass
name
=
raw_input
(
'请输入用户名:'
)
pwd
=
getpass.getpass(
'请输入密码:'
)
if
name
=
=
"alex"
and
pwd
=
=
"cmd"
:
print
(
"欢迎,alex!"
)
else
:
print
(
"用户名和密码错误"
)
|
场景2、猜年龄游戏
在程序里设定好你的年龄,而后启动程序让用户猜想,用户输入后,根据他的输入提示用户输入的是否正确,若是错误,提示是猜大了仍是小了
最简单的循环10次
1
2
3
4
5
6
|
#_*_coding:utf-8_*_
__author__
=
'Alex Li'
for
i
in
range
(
10
):
print
(
"loop:"
, i )
|
输出:
1
2
3
4
5
6
7
8
9
10
|
loop:
0
loop:
1
loop:
2
loop:
3
loop:
4
loop:
5
loop:
6
loop:
7
loop:
8
loop:
9
|
需求一:仍是上面的程序,可是遇到小于5的循环次数就不走了,直接跳入下一次循环
1
2
3
4
|
for
i
in
range
(
10
):
if
i<
5
:
continue
#不往下走了,直接进入下一次loop
print
(
"loop:"
, i )
|
需求二:仍是上面的程序,可是遇到大于5的循环次数就不走了,直接退出
1
2
3
4
|
for
i
in
range
(
10
):
if
i>
5
:
break
#不往下走了,直接跳出整个loop
print
(
"loop:"
, i )
|
有一种循环叫死循环,一经触发,就运行个天荒地老、海枯石烂。
海枯石烂代码
1
2
3
4
5
|
count
=
0
while
True
:
print
(
"海枯石烂..."
,count)
count
+
=
1
|
其实除了时间,没有什么是永恒的,死loop仍是少写为好
上面的代码循环100次就退出吧
1
2
3
4
5
6
7
8
|
count
=
0
while
True
:
print
(
"你是风儿我是沙,..."
,count)
count
+
=
1
if
count
=
=
100
:
print
(
"缠缠绵绵到天涯.."
)
break
|
回到上面for 循环的例子,如何实现让用户不断的猜年龄,但只给最多3次机会,再猜不对就退出程序。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
my_age
=
28
count
=
0
while
count <
3
:
user_input
=
int
(
input
(
"input your guess num:"
))
if
user_input
=
=
my_age:
print
(
"Congratulations, you got it !"
)
break
elif
user_input < my_age:
print
(
"Oops,think bigger!"
)
else
:
print
(
"think smaller!"
)
count
+
=
1
#每次loop 计数器+1
else
:
print
(
"猜这么屡次都不对,你个笨蛋."
)
|
加号的做用是用在拼接一个较长的字符串,见下面例子:
sean@sean-Parallels-Virtual-Platform:~/python/s14$ cat hello.py #!/usr/bin/env python name = input('please input your name:') age = input('please input your age:') job = input('please input your job:') salary = input('please input your salary:') msg = ''' -------------info of '''+name+'''----------------- Name:'''+name+''' Age:''' +age+''' Job:'''+job+''' Salary:'''+salary print (msg) sean@sean-Parallels-Virtual-Platform:~/python/s14$ python hello.py please input your name:Sean please input your age:28 please input your job:it please input your salary:2999 -------------info of Sean----------------- Name:Sean Age:28 Job:it Salary:2999
字符串凭借有不少种方法,我目前只知道有4种上面所说的万恶的加号是第一种也是最难用的一种,下面来介绍其他的三种
sean@sean-Parallels-Virtual-Platform:~/python/s14$ python hello.py please input your name:XinZhiYu please input your age:28 please input your job:It please input your salary:90000 ------------info of XinZhiYu----------- Name: XinZhiYu Age: 28 Job: It Salary: 90000 sean@sean-Parallels-Virtual-Platform:~/python/s14$ cat hello.py #!/usr/bin/env python name = input('please input your name:') age = input('please input your age:') job = input('please input your job:') salary = input('please input your salary:') msg = ''' ------------info of %s----------- Name: %s Age: %s Job: %s Salary: %s '''%(name,name,age,job,salary) print (msg)
听说有的时候只能使用这种格式,可是目前我还不知道那种状况下必须使用这种格式的字符串拼接
sean@sean-Parallels-Virtual-Platform:~/python/s14$ cat hello.py #!/usr/bin/env python name = input('please input your name:') age = input('please input your age:') job = input('please input your job:') salary = input('please input your salary:') msg = ''' ------------info of {_name}----------- Name: {_name} Age: {_age} Job: {_job} Salary: {_salary} '''.format(_name=name,_age=age,_job=job,_salary=salary) print (msg) sean@sean-Parallels-Virtual-Platform:~/python/s14$ python hello.py please input your name:Xinsir please input your age:28 please input your job:It please input your salary:29090 ------------info of Xinsir----------- Name: Xinsir Age: 28 Job: It Salary: 29090
依然是format函数可是这里使用{0}{1}。。。,不在使用变量
sean@sean-Parallels-Virtual-Platform:~/python/s14$ cat hello.py #!/usr/bin/env python name = input('please input your name:') age = input('please input your age:') job = input('please input your job:') salary = input('please input your salary:') msg = ''' ------------info of {0}----------- Name: {0} Age: {1} Job: {2} Salary: {3} '''.format(name,age,job,salary) print (msg) sean@sean-Parallels-Virtual-Platform:~/python/s14$ python hello.py please input your name:XiaoXin please input your age:28 please input your job:IT please input your salary:28765 ------------info of XiaoXin----------- Name: XiaoXin Age: 28 Job: IT Salary: 28765
for循环就是在指定的状况下循环指定次,往下看:
sean@sean-Parallels-Virtual-Platform:~/python/s14$ cat for.py #!/usr/bin/env python for i in range(5): print ('loop:',i) sean@sean-Parallels-Virtual-Platform:~/python/s14$ python for.py loop: 0 loop: 1 loop: 2 loop: 3 loop: 4
for循环能够挨个循环,也能够跳着循环,上面是每一个循环,若是我想打印1-10之间的全部基数呢?继续看:
sean@sean-Parallels-Virtual-Platform:~/python/s14$ cat for.py #!/usr/bin/env python for i in range(1,10,2): print ('loop:',i) sean@sean-Parallels-Virtual-Platform:~/python/s14$ python for.py loop: 1 loop: 3 loop: 5 loop: 7 loop: 9
for...else...循环就是当前面的循环正常执行完,没有跳出,后面的else将被执行
sean@sean-Parallels-Virtual-Platform:~/python/s14$ cat for.py #!/usr/bin/env python for i in range(5): print ('loop:',i) else: print ('OK!') sean@sean-Parallels-Virtual-Platform:~/python/s14$ python for.py loop: 0 loop: 1 loop: 2 loop: 3 loop: 4 OK!
sean@sean-Parallels-Virtual-Platform:~/python/s14$ cat for.py #!/usr/bin/env python for i in range(5): if i > 3: break print ('loop:',i) else: print ('OK!') sean@sean-Parallels-Virtual-Platform:~/python/s14$ python for.py loop: 0 loop: 1 loop: 2 loop: 3
功能和for...else...循环相差很少
sean@sean-Parallels-Virtual-Platform:~/python/s14$ python while.py 0 1 2 3 4 5 sean@sean-Parallels-Virtual-Platform:~/python/s14$ vim while.py sean@sean-Parallels-Virtual-Platform:~/python/s14$ cat while.py #!/usr/bin/env python i = 0 while i < 10: if i > 5: break print (i) i += 1 else: print ('ok')
sean@sean-Parallels-Virtual-Platform:~/python/s14$ cat while.py #!/usr/bin/env python i = 0 while i < 10: if i > 11: break print (i) i += 1 else: print ('ok') sean@sean-Parallels-Virtual-Platform:~/python/s14$ python while.py 0 1 2 3 4 5 6 7 8 9 ok sean@sean-Parallels-Virtual-Platform:~/python/s14$
在循环里面若是遇到break函数那么就跳出本次循环,若是遇到continue函数那么就跳出本次循环
sean@sean-Parallels-Virtual-Platform:~/python/s14$ cat for.py #!/usr/bin/env python i = 0 while i < 3: print ('while:',i) i += 1 for x in range(0,3): print ('for:',x) sean@sean-Parallels-Virtual-Platform:~/python/s14$ python for.py while: 0 for: 0 for: 1 for: 2 while: 1 for: 0 for: 1 for: 2 while: 2 for: 0 for: 1 for: 2
break智能跳出一次循环,可是若是循环嵌套循环,想一次性跳出2曾循环,那改怎么办呢?往下看:
sean@sean-Parallels-Virtual-Platform:~/python/s14$ cat for.py #!/usr/bin/env python i = 0 #定义一个跳出指示变量,当break_key等于true的时候,就告诉父循环跳出循环 break_key = 'false' while i < 3: print ('while:',i) i += 1 for x in range(0,3): #当内部循环等于2的时候,把外面的跳出指示变量设置为真 if x == 2: break_key = 'true' #跳出子循环 break print ('for:',x) if break_key == 'true': #跳出父循环 break sean@sean-Parallels-Virtual-Platform:~/python/s14$ python for.py while: 0 for: 0 for: 1
一、python 把代码读到内存 二、词法语法分析 三、放到编译器 ---》 生成字节码 四、执行字节码 ---》生成机器码 CPU执行
Python有大量的模块,从而使得开发Python程序很是简洁。类库有包括三中:
Python内部提供的模块
业内开源的模块
程序员本身开发的模块
Python内部提供一个 sys 的模块,其中的 sys.argv 用来捕获执行执行python脚本时传入的参数
1
2
3
4
5
6
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
sys
print
sys.argv
|
执行Python代码时,若是导入了其余的 .py 文件,那么,执行过程当中会自动生成一个与其同名的 .pyc 文件,该文件就是Python解释器编译以后产生的字节码。
ps:代码通过编译能够产生字节码;字节码经过反编译也能够获得代码。(任何字节码经过反编译均可以获得代码)
做业一:博客
https://i.cnblogs.com/EditPosts.aspx?postid=5712122
做业二:编写登录接口
#!/usr/bin/env/python import sys,os,getpass #os.system('clear') #清屏 i=0 while i < 3: #只要用户异常登陆不超过3次,能够不断循环 name = input('\33[32m请输入用户名:\33[37m') lock_file = open('user_lock_info.txt','a+') #当用户输入用户名后,打开锁定文件 检查此用户是否已经lock掉了 lock_list = lock_file.readlines() #按行的形式打开 for lock_line in lock_list: #循环lock文件 lock_line =lock_line.strip('\n') #去掉换行符 if name == lock_line: #若是lock,直接退出 sys.exit('用户 %s 已经被锁定,退出!' % name) user_file = open('user_info.txt') #打开帐号文件 user_list = user_file.readlines() for user_line in user_list: #对帐号文件进行循环匹配 (user,password) = user_line.strip('\n').split() #分别获取帐号、密码 if name == user: #若是用户名正常匹配 a = 0 while a < 3: #只要用户密码异常输入不超过3次就不断循环 passwd = input('\33[32m请输入密码:\33[37m') #输入密码,也能够作隐藏密码 if passwd == password: #密码正确匹配,提示欢迎登陆 print('欢迎登录管理平台,用户 %s !' % name) sys.exit(0) #正常退出,调用系统退出命令 else : if a != 2: #a=2时,是最后一次机会,不会提示0次机会了 print('用户 %s 密码错误,请从新输入,还有 %d 次机会' % (name,2 - a)) a += 1 #密码输错后,循环增长1 else: lock_file.write(name + '\n') #密码输错三次后,将该用户写入lock文件 sys.exit('用户 %s 达到最大登录次数将被锁定退出' % name) else: if i != 2: #i=2时,是最后一次机会,再也不提示0次机会了 print ('用户 %s 不存在,请从新输入,还有 %d 次机会' % (name,2 -i)) i += 1 #当用户输错时,循环增长1 else: print('用户 %s 不存在,退出!' % name) #用户输入3次错误后,异常退出 lock_file.close() #关闭lock文件 user_file.close() #关闭帐号、密码文件
流程图
http://www.tuicool.com/articles/ZV3YJfM
http://www.jb51.net/article/78358.htm