python基础语句以及条件语句大全
经历两周的时间,python的数据类型算是告一段落了,如有不明白的地方可以翻阅之前的文章,还有一些经典案列,帮助你快速的理解。
今天我们告别python的基础中的基础,走进语句的介绍,快拿出小本本来记录下来。
其实仔细想想,计算机语言和咱们正常沟通交流的语言,母语是一样子的学习经历,孩子能学会母语,为什么学不会计算机语言呢,母语的学习也是字母,生字,词语,语句,段落,文章这么一个流程的,python也是一样子的。
好了,废话不多说,进入语句的学习。
print: 输出,默认输出是换行的,如果实现不换行,需要在变量末尾加上逗号。
>>> a="hello world"
>>> print a
hello world
import: 导入,把某件事物作为另一件事物导入。
>>> import sys
>>> import os
>>> from __future__ import division
赋值魔法(告诉你孩子这叫魔法,有没有勾引起他学习的兴趣呢?)
序列解包:
>>> values=1,2,3
>>> values
(1, 2, 3)
>>> x,y,z=values
>>> x
1
>>> y
2
>>> z
3
链式赋值:
增量赋值:(+ - x / %等标准运算符都可以用)
x=x+1=====>x+=1
例子:
>>> a="hello"
>>> a+="world"
>>> a*=2
>>> a
"helloworldhelloworld"
条件语句if ,else, elif子句
elif是else if的简写,可以检查多个条件。
number=int(raw_input("plsease enter a number:"))
if number>0:
print "The number is positive"
elif number<0:
print "The number is negative"
else:
print "The number is zero!"
plsease enter a number:10
The number is positive
plsease enter a number:-10
The number is negative
plsease enter a number:0
The number is zero!
重点提示:python语句和别的语言有点差别,它对缩进的要求特别严格,所以小伙伴们在写python脚本的时候要养成良好的习惯,按Tab也可以实现缩进。