http://www.runoob.com/python/python-variable-types.html
python中的变量不需要声明,变量的赋值操作即是变量的声明和定义的过程,所以,每个变量在使用前必须被赋值,
赋值之后变量才能被创建,等号用来给变量赋值,
多个变量赋值,
a=b=c=1;
或者
a,b,c=1,2,"john"
python支持四种不同的数值类型:int(有符号的整型),long(长整形),float(浮点型),complex(复数)
字符串:s='ilovepython',则:s[1:5]=love
实例:
str='Hello World!'
print str;#输出完成字符串
print str[0];#输出字符串的第一个字符
print str[2:7];#输出第二个到第七个之间的字符
print str[2:];#输出第二个之后的字符
print str*2;#输出字符串两次
print str+"TEST";#输出连接后的字符串
则输出结果为:
加号(+)是列表连接运算符,星号(*)是重复操作。
list (列表)是python中使用最频繁的数据类型。
list=[ 'abcd',768,2.23,'john',70.2];
tinylist=[123,'john'];
print list;
print list[0];
print list[1:3];#没有第三个元素
print list[2:];
print tinylist*2;
print list+tinylist;
输出结果:
元组:元组是另一种数据类型,类似于List(列表)
用()标识,内部元素用逗号隔开,但是元素不能二次赋值,相当于只读列表;
tuple=('abcd',786,2.23,'john',70.2);
tinytuple=(123,'hhhhh');
print tuple;
print tuple[0];
print tuple[1:3];
print tuple[2:];
print tuple*2;
print tuple+tinytuple;
输出:
对于更改元素的事:
tuple=('abcd',786,2.23,'john',70.2);
list=['abcd',786,2.23,'john',70.2];
print tuple,list;
list[2]=1000;
print list;
tuple[2]=1000;
print tuple;
则输出结果:
例如:
a=10;
b=20;
list=[1,2,3,4,5];
if(a in list):
print "a is available in the given list";
else:
print "a is not available in the givec list"
a=2;
if(a in list):
print "a is available in the given list";
else:
print "a is not available in the given list"
输出结果:
对于循环语句:
if……else……:
if 判断条件:
执行语句……
else:
执行语句……
多个判断条件时:
if 判断条件1:
执行语句1……
elif判断条件2:
执行语句2……
elif 判断条件3:
执行语句3……
else:
执行语句4……
Python不支持switch,所以如果是多个条件判断,只能用elif来实现,如果判断需要多个条件需同时判断时,可以使用or(或),
表示两个条件有一个成立时判断条件成功