PYTHON入门(一)
跨入PYTHON大门 虽然主攻方向是FPGA数字系统设计,但偶尔学点别的语言换换脑子,防止思维僵化,就当是偷懒看本小说吧。把Python翻出来看看,说不定以后真能用上呢? Python是一门既简单又强大的编程语言。它适合初学者,也适合专业人员,用Python写代码是一种享受,而不是负担。记得以前看书
跨入PYTHON大门
虽然主攻方向是FPGA数字系统设计,但偶尔学点别的语言换换脑子,防止思维僵化,就当是偷懒看本小说吧。把Python翻出来看看,说不定以后真能用上呢?

Python是一门既简单又强大的编程语言。它适合初学者,也适合专业人员,用Python写代码是一种享受,而不是负担。记得以前看书时,书上说看Python代码就像读英文文章一样。总结一下Python的特点:简单易学、免费开源、移植性好、面向对象、可嵌入(作为C/C++的脚本)、库丰富。后续将在Linux环境下学习Python,因为Linux通常自带Python。
一、基本操作
1. 打开shell,输入python,写个HelloWorld
在命令行输入python(一般python默认指向python2)或python2,快捷键Ctrl+D退出Python命令行回到shell命令行。
root@hlf-virtual-machine:/home/hlf# python2
Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "helloworld"
helloworld
>>>
注意:Python2和Python3在语法上稍有区别,上面这个HelloWorld在Python3中会报错。
2. 新建文件hello.py,内容如下:
print "HelloWorld"
运行命令查看结果:
root@hlf-virtual-machine:/home/hlf/mnt# python2 hello.py
HelloWorld
3. 指定解释器,自动运行
print "HelloWorld"
这个hello.py没有指定解释器,所以每次运行都要 python2 hello.py。
print ("HelloWorld")
类似地,这个hello.py每次运行都要 python3 hello.py。于是——可以这样:
#!/usr/bin/python2
print "HelloWorld"
以及这样:
#!/usr/bin/python3
print ("HelloWorld")
加个第一行,用于指定使用哪个解释器(可以自己去/usr/bin/里找)。然后在shell中运行:
root@hlf-virtual-machine:/home/hlf/mnt# chmod a+x hello.py # 获取权限
root@hlf-virtual-machine:/home/hlf/mnt# ./hello.py # 执行
HelloWorld
甚至连文件名都可以换成hello.txt,然后 chmod a+x hello.txt 和 ./hello.txt,效果一样(但模块需要.py后缀)。
二、支持的类型
1. 数
支持整数、长整数、浮点数和复数。
| 类型 | 例子 |
|---|---|
| 整数 | 2 |
| 长整数 | 2222222(哈哈) |
| 浮点数 | 3.21 或 3.24E-4 |
| 复数 | -5+4j |
2. 字符串
2.1 单引号
print 'HelloWorld'
2.2 双引号
print "HelloWorld"
2.3 三引号
print '''hello
world'''
输出结果为:
hello
world
2.4 转义符——输出特殊字符
print "Hello\"oo\"World"
输出结果为:Hello"oo"World
2.5 转义符——连接下一行
print "Hello\
World"
输出结果为:HelloWorld
2.6 自然字符串
假如有时候就是想打印出"\n"而不是换行,加个r:print r"hello\nworld",即可打印出:hello\nworld
2.7 unicode字符串
Python允许处理非英文的unicode文本,只需在字符串前加u或U,如 u"你好"。
2.8 Python会自动将临近字符串连接
print "hello" "world",输出结果自己想象。
3. 变量
无需声明变量类型。
i = 5
j = 3.25E-5
s = "sssss"
print i+1
print j
print s
输出结果还是自己想象吧。
4. 物理行与逻辑行
i = 5;j = 5.20E-5;s = "sssss";print i+1;print j;print s
上述代码物理行数为2,逻辑行数为6,自己想象。不过应该尽量避免使用分号。
5. 运算符
| 运算符 | 名称 | 例子 |
|---|---|---|
| + | 加 | 'a'+'b'='ab' |
| - | 减 | |
| * | 乘 | 'ab'*3='ababab' |
| ** | 幂 | 2**3=8 |
| / | 除 | |
| // | 取整除 | 4//3=1 |
| % | 取模 | 8%3=2 |
| << | 左移 | 2<<2=8 |
| >> | 右移 | 2>>1=1 |
| & | ~ ^ | 按位与、或、非、异或 | |
| not, and, or | 布尔与、或、非 |
大于小于等比较运算符,同C语言。
6. 缩进
不像其他编程语言,在Python中空白很重要,错误的缩进会引发报错。比如下面就会报错:
i = 9;
print "value is:", i
报错内容:不可以随意开始一个新的语句块。缩进建议选择2空格或Tab,但一旦选定就要一直用,不要混用。
7. 控制语句if、while、for
7.1 if语句
#!/usr/bin/python2
i = int(raw_input("Enter num: ")) # 输入的字符串转数字
if i <= 5:
print "value so small"
else:
print "value so large"
这里用到了之前说的缩进。程序首先让你输入一个数,然后告诉你结果。
root@hlf-virtual-machine:/home/hlf/mnt# ./hello.py
Enter num:6
value so large
7.2 while语句
#!/usr/bin/python2
running = True
while running:
i = int(raw_input("Enter num: "))
if i == 0:
running = False
print "STOP"
else:
print "RUNNING ", "num is", i
程序运行起来,不输入0就不停止。
7.3 for语句
#!/usr/bin/python2
for i in range(3,7):
print i
else:
print "STOP"
这个for循环与C语言的稍有区别,而且带一个else,当for超出范围后会以else的形式最后再执行一次。执行结果为:
3
4
5
6
STOP
步长默认为1,当然也可以设置,在range中加第三个参数:
#!/usr/bin/python2
for i in range(1,10,3):
print i
else:
print "STOP"
结果就是:1 4 7 STOP。
8. 跳出循环的break
#!/usr/bin/python2
while True:
s = raw_input("Enter String: ")
if s == "stop it":
break
print "Length:", len(s)
print "STOP"
当识别到特定字符串时,停止运行,否则循环一直测量字符串长度。运行结果:
root@hlf-virtual-machine:/home/hlf/mnt# ./hello.py
Enter String:hi
Length: 2
Enter String:hello
Length: 5
Enter String:stop it
STOP
break也可以在for循环中使用。
9. 跳出本次循环的continue
#!/usr/bin/python2
while True:
s = raw_input("Enter String: ")
if s == "stop it":
break
if len(s) <= 2:
continue
print "Length:", len(s)
print "STOP"
本程序,若输入字符数小于等于2,则直接忽略,进行下一轮循环。输出结果:
root@hlf-virtual-machine:/home/hlf/mnt# ./hello.py
Enter String:h
Enter String:hi
Enter String:hello
Length: 5
Enter String:stop it
STOP
到这里应该还不算真正入门,但基本已经摸到Python大门的门把手了。一篇写太多的话,看着太累,分两批写吧,好玩的部分应该还在后面呢。

































