Python开发(基础):初识Python
-
Python可以应用于众多领域,如:数据分析、组件集成、网络服务、图像处理、数值计算和科学计算等众多领域。目前业内几乎所有大中型互联网企业都在使用Python,如:Youtube、Dropbox、BT、Quora(中国知乎)、豆瓣、知乎、Google、Yahoo!、Facebook、NASA、百度、腾讯、汽车之家、美团等。互联网公司广泛使用Python来做的事一般有:自动化运维、自动化测试、大数据分析、爬虫、Web 等。
-
注视:上述重点字体表示该公司主要使用Python语言开发
-
Python的种类
-
Cpython
Python的官方版本,使用C语言实现,使用最为广泛,CPython实现会将源文件(py文件)转换成字节码文件(pyc文件),然后运行在Python虚拟机上。 -
Jyhton
Python的Java实现,Jython会将Python代码动态编译成Java字节码,然后在JVM上运行。 -
IronPython
Python的C#实现,IronPython将Python代码编译成C#字节码,然后在CLR上运行。(与Jython类似) -
PyPy(特殊)
Python实现的Python,将Python的字节码字节码再编译成机器码。 -
RubyPython、Brython ...
Python 入门
一、 hellow word
在E:\worspace_all\python\day01\目录下创建hello.py
1
|
print 'hello world!'
|
在命令行,执行hello.py 文件
即:
1
|
python E:\worspace_all\python\day01\hello.py |
python 内部执行过程如下:
二、 解析器
上一步执行过程中,明确指出使用python 解析器,如果想像执行shell脚本一样执行python,可以在python文件头部先指定解析器,如:
1
|
#!/usr/bin/env python |
这样就可以直接使用
1
|
. / hello.py
|
来执行hello.py文件了
三、 编码
python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill),所以如果直接输出中文件,如下所示:
1
2
3
|
#!/usr/bin/env python print "你好,世界"
|
会出错,改为如下所示:
1
2
3
4
|
#!/usr/bin/env python # -*- coding: utf-8 -*- print "你好,世界"
|
四、 注释
-
单行注释:#注释内容
-
多行注释:"""注释内容"""
五、 pyc文件
执行Python代码时,如果导入了其他的 .py 文件,那么执行过程中会自动生成一个与其同名的 .pyc 文件,该文件就是Python解释器编译之后产生的字节码。
1
2
|
import getpass
pwd = getpass.getpass( 'Please Input Password:' )
|
注意:代码经过编译可以产生字节码;字节码通过反编译也可以得到代码。
六、 变量
变量定义的规则:
-
变量名只能是 字母、数字或下划线的任意组合
-
变量名的第一个字符不能是数字
-
以下关键字不能声明为变量名 ['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']
变量的声明和赋值,通过=号赋值,如:
1
2
3
|
n = 0
name1 = 'alex'
name2 = "zhangsan"
|
七、 输入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import getpass
print 'start...'
n = 1
while n< = 3 :
# 将用户输入的内容赋值给 name 变量
name = raw_input ( 'Please Input Username:' )
# 将用户输入的内容赋值给 pwd 变量
pwd = getpass.getpass( 'Please Input Password:' )
if name = = 'alex' and pwd = = '123' :
# 打印
print 'Welcome alex !'
break
elif name = = 'alex' and pwd! = '123' :
print 'Password is wrong!'
elif name! = 'alex' :
print 'Username is not exists!'
n = n + 1
print 'end ...'
|
输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法
八、 流程控制和缩进
1
2
3
4
5
6
7
8
|
语法: if 条件表达式:
执行语句 1
elif 条件表达式:
执行语句 2
.... else :
执行语句n
|
python 通过缩进来控制流程的执行范围(或区域),而java 和 C 语言都是通过{ }来控制范围
1
2
3
4
|
if 1 = = 1 :
print 'True'
else :
print 'False'
|
九、 while循环
1
2
3
4
5
6
7
8
|
语法: while 条件表达式:
循环体...
#终止循环
break
#跳过当前循环,继续执行下一次循环
continue
|
例1:使用while循环输入 1 2 3 4 5 6 8 9 10
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import time
print ( 'start...' )
n = 0
while True :
n = n + 1
if (n = = 7 ):
continue
print (n)
time.sleep( 1 )
if n = = 10 :
break
print ( 'end...' )
|
例2:求1-2+3-4+5 ... 99的所有数的和
1
2
3
4
5
6
7
8
9
|
sum = 0
n = 1
while n< = 100 :
if (n % 2 = = 0 ):
sum = sum - n
if (n % 2 = = 1 ):
sum = sum + n
n = n + 1
print sum
|
例三:用户登陆(三次机会重试)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import getpass
print 'start...'
n = 1
while n< = 3 :
name = raw_input ( 'Please Input Username:' )
pwd = getpass.getpass( 'Please Input Password:' )
if name = = 'alex' and pwd = = '123' :
print 'Welcome alex !'
break
elif name = = 'alex' and pwd! = '123' :
print 'Password is wrong!'
elif name! = 'alex' :
print 'Username is not exists!'
n = n + 1
print 'end ...'
|