print 额外用法
1.print 的参数可以用逗号隔开,并具有一定效果,相互隔开的数据会用‘ ’(空格)进行连接
示例 输出
print 1,2,3 1 2 3
2.如果在结尾处加上逗号,那么接下来的语句会与之前的语句在同一行打印
示例 输出
print 'Hello,',
print 'World' Hello,World
import 的几种表达形式
1.import somemoudle 导入某个模块
2.from somemoudle import somefunction 从某个模块导入某个函数
3.from somemoudle import somefunction, antherfunction, yetanotherfunction 从某个模块导入多个函数
4.from somemoudle import * 从给定模块导入所有功能
as 与 import 联合使用
as 用法:在语句末尾增加一个as子句,在该子句后给出想要用的别名
1.可以为函数取别名 import math as foobar
2.可以为模块取别名 from math import sqrt as foobar
Python中特殊的赋值技巧
1.序列解包:多个赋值操作同时进行,**********当函数或者方法返回元组(或者其他序列或可迭代对象)时,这个特性尤其有效。************
示例 输出
values = 1,2,3
print values (1,2,3)
x,y,z = values
print x,y,z 1 2 3
被赋值的参数必须与序列参数一一对应
values = [1,2,3,4]
print values
x,y = values
print x,y 报错。。。too many values to unpack
3.0新特性,序列收集器:可以在函数的参数列表中一样使用*运算符
a,b,*rest = [1,2,3,4]
print a,b,rest
rest中存放的将会是[3,4]
2.链式赋值
将同一个值赋给多个变量。
x = y = somefunction()
3.增量赋值------和C/C++并不相同
没有 ++x, x--等形式
只有
x = 2
x +=2
x *=2
x /=2
x %=2
等形式
Python语句的缩排(代码块)
注意Python中语句是不可以随意缩排的,与C/C++中的代码块有一定的区别:
错误代码
X = 6:
x = 6
print x
会报错
Python中,冒号(:)标示语句块的开始,块中的每一个语句都是缩进的(缩进量相同)
伪代码
this is a line
this is anther line:
this is anther block
continuing the same block
the last line of this block
phew, there we escaped the inner block
Python中的真值
真值(布尔值)
True与False
被看做false 的量
False None 0 "" () [] {}
被看做true 的量
非False的量
if ,else,elif 语句
与C/C++的区别 注意 Python中没有else if 语句
name = raw_input('What is your name? ')
if name.endswith('Gumby'):
if name.startswith('Mr.'):
print 'Hello, Mr.Gumby'
elif name.startswith('Mrs.'):
print 'Hello, Mrs.Gumby'
else:
print 'Hello, Gumby'
else:
print 'Hello, stranger'
python中的比较运算符
x == y
x < y
x > y
x<= y
x >=y
x <> y #相当于x!=y
x != y
x is y #相当与判断x与y绑定在相同的内存对象上
x is not y #x与y是不同的内存的对象
x in y #x是y容器(例如,序列)的成员
x not in y #x不是y 容器 (例如, 序列) 的成员
比较运算与赋值运算一样是可以连接的,如0<age<100
2.相等运算符 ==
判断两个事物是否在数值上相等
3.is 同一性运算符
is 运算符,相当与判定x,y是否是绑定在一个对象(事物)上的。
4.in 成员资格运算符
5.字符串和序列比较
6.布尔运算符 and /or/ not
<span style="font-size:14px;">name = raw_input('Please enter your name: ') or' <unknown>'</span>
利用到的特性,短路逻辑
Python中的断言
相当于若没有通过断言,程序会直接报错,并停止运行
利用assert condition [,str #用来解释断言]
示例
age = -1
assert 0<age<100, 'The age must be realistic'
Python中的循环形式:for循环 and while循环
1.while循环
<span style="font-size:10px;">name = ''
while not name:
name = raw_input('Please input your name: ')
print 'Hello, %s' % name</span>
2.for循环
注意:Python中的for循环与C中的for循环并不相同,Python中的for循环为for...in...的形式,并且Python中的for...in...形式常常与range()结合起来一起使用作为范围。
for循环示例 输出
<span style="font-size:10px;">words = ['this','is','an','ex','parrot']
for word in words:
print word this</span>
<span style="font-size:10px;"> is</span>
<span style="font-size:10px;"> an</span>
<span style="font-size:10px;"> ex</span>
<span style="font-size:10px;"> parrot</span>
range()函数------------------ 一次创建整个序列
range(A) #从0~A,不包含A
range(A,B) #从A~B,但不包含B
range (A,B,foot ) #从A~B,不包含B,以foot//的步长
xrange函数()----------------------- 与range类似:区别:一次只创建一个数
for x in xrange(10):
print 1 打印出来10行1
d = {'x':1, 'y':2, 'z':3} y correspond to 2
for key,value in d.items(): x correspond to 1
print key, 'correspond to', value z correspond to 3
names = ['anne','beth','george','damon'] anne is 12 years old
ages = [12,45,32,102] beth is 45 years old
for i in range(len(names)): george is 32 years old
print names[i],'is',ages[i],'years old' damon is 102 years old
zip(A,B)函数,将两个序列压缩在一起,并返回一个元组序列,
可以处理不等长序列,在最短的序列‘用完’的时候停止
示例
print zip(range(5),xrange(1000000)) [(0,0),(1,1),(2,2),(3,3),(4,4)]
enumerate()函数返回索引,值对, 适用于字符串
for index,string in enumerate(strings):
if 'xxx' in strings:
strings[index] = '[censored]' item assignment
sort/reverse 与 sorted/reversed区别。
sort/reverse都是原地操作,即在原位置上修改(翻转/排序),sorted/reversed返回一个新的修改后的序列或者可迭代对象。
Python中的跳出循环的方式:break/continue
示例
from math import sqrt
for n in range(99,0,-1):
root = sqrt(n)
if root == int(root):
print n
break
在循环中增加一个else语句----------仅在没用调用break时执行:
示例
from math import sqrt
for n in range(99,0,-1):
root = sqrt(n)
if root == int(root):
print n
break
else:
print "Didn't find it!"
特殊的循环(轻量级循环);----------列表推导式
利用其他列表创建新列表(类似数学中的集合推导式)
示例
print [x*x for x in range(10)]
print [(x,y) for x in range(3) for y in range(3)]
3种语句pass,del,exec/eval
:pass
pass可以在代码中做占位符使用,相当于没有实现的部分,python中空代码块是违法的
name = 'Enid'
if name == 'Ralph Auldus Melish':
print 'Welcome'
elif name == 'Enid':
#...
print 1
pass
elif name == 'Bill Gates':
print 'Access Denied'
:del
对于Python来说,在某个值不在使用的时候,Python解释器会负责内存的的回收
del 只是相当与删除了一个标号
示例
x = 1
del x
:exec
主要与命名空间结合起来
命名空间可以是一个字典
示例
from math import sqrt
scope = {}
exec 'sqrt =1' in scope
print sqrt(4)
print scope['sqrt']
eval(用于“求值”)类似于exec的内建函数。exec会执行一系列Python语句,而eval会计算
python表达式,并且返回结果值。