1.使用逗号输出:print输出,打印多个表达式,使用逗号隔开。
2.语句块
a) Python中,冒号(:)用来表示语句块的开始,块中的每一个语句的是缩进的(缩进量相同)。
3.布尔变量:
a)False ,None ,0,””,(),[],{} ,这些值作为布尔表达式的时候,会被解释器看做假(False)。
PS:bool(0) == bool(“”) ==bool(None) == False,但他们本身却并不相等。
4.if,else,elif语句
Example:num = raw_input('Please enter a num(0-9):')
if num ==0:
print'you enter a num 0'
elif num > 5:
print'you enter a num:' + num
elif num <= 5:
print'you enter anum:' + num
else:
print'you enter anum:' +num
5.比较运算符:
x== y, x<y, x>y, x>=y, x<=y, x!=y, x is y, x is not y, x in y, x notin y等等。
6.while循环
Example: x = 1
While x<= 100
x += 1
7.for循环(如果能使用for循环,就尽量不用while循环)
Example: words = ['this','is','a','test','program']
for word in words:
print word
a) range 和xrange函数:Python内建函数,可以迭代某范围的数字。
example:>>>range(0,10)
[0,1,2,3,4,5,6,7,8,9]
PS:range函数一次创建整个序列,而xrange一次只创建一个数。
Example:使用range(迭代,设置步长为2)
words = ['this','is','a','test','program']
for x in range(0,len(words),2):
print words[x]
运行结果:this
a
program
8.跳出循环:break,continue
9.循环中的else字句:
Example::nums = [1,2,3,4,5,6,7,8]
for num in nums:
if num == 9:
print'I find you'
break
else:
print'Did notfind you' 运行结果:Did not find you
10.pass语句:由于Python中空代码是非法,所以在语句块中加上一个pass语句可解决。
Example:name = 'vicken'
if name == 'ken':
print'hello,ken'
elif name == 'vicky':
#还没完成
pass
elif name == 'lisa':
print'hello,lisa'
else:
print'sorry,没有你的信息哦'
11.del语句,可以移除一个对象的引用,也会移除那个名字本身
Example:>>> x = 1
>>> del x
12.exec和eval