Python 随版本的不同,语法也有一些差异。 具体可以参考最新的Python帮助文档。 以下说明都是基于Python 3.1 版本。
一. Python 变量类型
#整型
integer_number = 90
#浮点
float_number = 90.4
#复数
complex_number = 10 + 10j
#list 序列:列表、元组和字符串都是序列,序列的两个主要特点是索引操作符和切片操作符。
sample_list = [1,2,3,'abc']
#dictionary 字典
sample_dic = {"key":value, 2:3}
#tuple 只读的序列
sample_tuple = (1,3,"ab")
二. Python 程序流程控制
2.1 条件判断结构
flag1 = some_value
flag2 = other_value
if flag1:
do_function_1()
elif flag2:
do_function_2()
else:
do_function_3()
2.2 循环结构
for i in range(0, 10):
print(i)
for i in ['a','b','c','dd','eee'];
print(i)
三. Print 函数及格式化输出
3.1 Print 自动换行
在Python 3.0 以后,Python 在print 函数上做了修改。 在Python 2.x 版本中,示例如下:
for i in range(0,5):
print i
默认情况是自动换行的,如果说是不自动换行,在最后加逗号就可以了:print i,
在Python 3.0 的文档里,对print 说明如下:
print([object, ...], *, sep=' ', end='\n', file=sys.stdout)
Print object(s) to the stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.
All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no object is given, print() will just write end.
The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used.
在Python 3.x 版本中,如果不想自动换行,就需要使用end 参数。该参数默认使用'\n',即回车换行,如果不想使用,换成其他字符,或者为空即可。 示例如下:
>>> for i in range(5):
print(i,end='')
01234
>>> for i in range(5):
print(i)
0
1
2
3
4
>>> for i in range(5):
print(i,end=',')
0,1,2,3,4,
3.2 print 正常输出
使用print输出各型的
(1). 字符串
(2). 整数
(3). 浮点数
(4). 出度及精度控制
>>> str = 'tianlesoftware oracle dba'
>>> print(str);
tianlesoftware oracle dba
>>>
3.3 格式化输出整数
python print也支持参数格式化,与C言的printf似, 示例:
>>> str = "the length of (%s) is %d" %('Hello World',len('Hello World'))
>>> print(str)
the length of (Hello World) is 11
或者直接写道print里:
>>> print( "the length of (%s) is %d" %('Hello World',len('Hello World')))
the length of (Hello World) is 11
>>>
3.4 格式化输出16制整数
nHex = 0x20
#%x --- hex 十六进制
#%d --- dec 十进制
#%o --- oct 八进制
示例:
>>> nHex = 0x20
>>> print("nHex = %x,nDec = %d,nOct = %o" %(nHex,nHex,nHex))
nHex = 20,nDec = 32,nOct = 40
3.5 格式化输出浮点数(float)
#导入math 模块
>>> import math
#default
>>> print("PI = %f" % math.pi)
PI = 3.141593
#width = 10,precise = 3,align = left
>>> print("PI = %10.3f" % math.pi)
PI = 3.142
#width = 10,precise = 3,align = rigth
>>> print("PI = %-10.3f" % math.pi)
PI = 3.142
#前面填充字符
>>> print("PI = %06d" % int(math.pi))
PI = 000003
>>>
3.6 格式化输出字符串(string)
# precise = 3
>>> print("%.3s " % ("jcodeer"))
jco
#precise = 4
>>> print("%.*s" % (4,"jcodeer"))
jcod
# width = 10,precise = 3
>>> print ("%10.3s" % ("jcodeer"))
jco
3.7 输出列表(list)
#list直接打印即可
>>> l = [1,2,3,4,'tianlesoftware']
>>> print(l)
[1, 2, 3, 4, 'tianlesoftware']
>>> print(l[0])
1
>>> print(l[4])
tianlesoftware
3.8 输出字典(dictionary)
>>> dave = {1:'A',2:'B',3:'C',4:'D'}
>>> print(dave)
{1: 'A', 2: 'B', 3: 'C', 4: 'D'}
>>> print(dave[4])
D
>>> print(dave[1])
A
一. Python 变量类型
#整型
integer_number = 90
#浮点
float_number = 90.4
#复数
complex_number = 10 + 10j
#list 序列:列表、元组和字符串都是序列,序列的两个主要特点是索引操作符和切片操作符。
sample_list = [1,2,3,'abc']
#dictionary 字典
sample_dic = {"key":value, 2:3}
#tuple 只读的序列
sample_tuple = (1,3,"ab")
二. Python 程序流程控制
2.1 条件判断结构
flag1 = some_value
flag2 = other_value
if flag1:
do_function_1()
elif flag2:
do_function_2()
else:
do_function_3()
2.2 循环结构
for i in range(0, 10):
print(i)
for i in ['a','b','c','dd','eee'];
print(i)
三. Print 函数及格式化输出
3.1 Print 自动换行
在Python 3.0 以后,Python 在print 函数上做了修改。 在Python 2.x 版本中,示例如下:
for i in range(0,5):
print i
默认情况是自动换行的,如果说是不自动换行,在最后加逗号就可以了:print i,
在Python 3.0 的文档里,对print 说明如下:
print([object, ...], *, sep=' ', end='\n', file=sys.stdout)
Print object(s) to the stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.
All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no object is given, print() will just write end.
The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used.
在Python 3.x 版本中,如果不想自动换行,就需要使用end 参数。该参数默认使用'\n',即回车换行,如果不想使用,换成其他字符,或者为空即可。 示例如下:
>>> for i in range(5):
print(i,end='')
01234
>>> for i in range(5):
print(i)
0
1
2
3
4
>>> for i in range(5):
print(i,end=',')
0,1,2,3,4,
3.2 print 正常输出
使用print输出各型的
(1). 字符串
(2). 整数
(3). 浮点数
(4). 出度及精度控制
>>> str = 'tianlesoftware oracle dba'
>>> print(str);
tianlesoftware oracle dba
>>>
3.3 格式化输出整数
python print也支持参数格式化,与C言的printf似, 示例:
>>> str = "the length of (%s) is %d" %('Hello World',len('Hello World'))
>>> print(str)
the length of (Hello World) is 11
或者直接写道print里:
>>> print( "the length of (%s) is %d" %('Hello World',len('Hello World')))
the length of (Hello World) is 11
>>>
3.4 格式化输出16制整数
nHex = 0x20
#%x --- hex 十六进制
#%d --- dec 十进制
#%o --- oct 八进制
示例:
>>> nHex = 0x20
>>> print("nHex = %x,nDec = %d,nOct = %o" %(nHex,nHex,nHex))
nHex = 20,nDec = 32,nOct = 40
3.5 格式化输出浮点数(float)
#导入math 模块
>>> import math
#default
>>> print("PI = %f" % math.pi)
PI = 3.141593
#width = 10,precise = 3,align = left
>>> print("PI = %10.3f" % math.pi)
PI = 3.142
#width = 10,precise = 3,align = rigth
>>> print("PI = %-10.3f" % math.pi)
PI = 3.142
#前面填充字符
>>> print("PI = %06d" % int(math.pi))
PI = 000003
>>>
3.6 格式化输出字符串(string)
# precise = 3
>>> print("%.3s " % ("jcodeer"))
jco
#precise = 4
>>> print("%.*s" % (4,"jcodeer"))
jcod
# width = 10,precise = 3
>>> print ("%10.3s" % ("jcodeer"))
jco
3.7 输出列表(list)
#list直接打印即可
>>> l = [1,2,3,4,'tianlesoftware']
>>> print(l)
[1, 2, 3, 4, 'tianlesoftware']
>>> print(l[0])
1
>>> print(l[4])
tianlesoftware
3.8 输出字典(dictionary)
>>> dave = {1:'A',2:'B',3:'C',4:'D'}
>>> print(dave)
{1: 'A', 2: 'B', 3: 'C', 4: 'D'}
>>> print(dave[4])
D
>>> print(dave[1])
A