跨入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/python3m
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.py换成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”而不是换行
print r"helloworld\n"
加个r,
即可打印出:helloworld\n2.7 unicode字符串
python允许处理,非英文的unicode文本,只需在字符串前加u或U,如u"you 好"
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大门的门把手了,一篇写太多了的话,看得太累,分两批写吧,好玩的部分应该还在后面呢