Python基础教程(三):运算符、条件语句
2015-12-01 17:37
627 查看
Python运算符
什么是运算符?
本章节主要说明Python的运算符。举个简单的例子4+5=9。例子中,4和5被称为操作数,"+"号为运算符。
Python语言支持以下类型的运算符:
算术运算符
比较(关系)运算符
赋值运算符
逻辑运算符
位运算符
成员运算符
身份运算符
运算符优先级
接下来让我们一个个来学习Python的运算符。
Python算术运算符
以下假设变量a为10,变量b为20:
以下实例演示了Python所有算术运算符的操作:
#!/usr/bin/python
a=21
b=10
c=0
c=a+b
print"Line1-Valueofcis",c
c=a-b
print"Line2-Valueofcis",c
c=a*b
print"Line3-Valueofcis",c
c=a/b
print"Line4-Valueofcis",c
c=a%b
print"Line5-Valueofcis",c
a=2
b=3
c=a**b
print"Line6-Valueofcis",c
a=10
b=5
c=a//b
print"Line7-Valueofcis",c
以上实例输出结果:
Line1-Valueofcis31
Line2-Valueofcis11
Line3-Valueofcis210
Line4-Valueofcis2
Line5-Valueofcis1
Line6-Valueofcis8
Line7-Valueofcis2
Python比较运算符
以下假设变量a为10,变量b为20:
以下实例演示了Python所有比较运算符的操作:
#!/usr/bin/python
a=21
b=10
c=0
if(a==b):
print"Line1-aisequaltob"
else:
print"Line1-aisnotequaltob"
if(a!=b):
print"Line2-aisnotequaltob"
else:
print"Line2-aisequaltob"
if(a<>b):
print"Line3-aisnotequaltob"
else:
print"Line3-aisequaltob"
if(a<b):
print"Line4-aislessthanb"
else:
print"Line4-aisnotlessthanb"
if(a>b):
print"Line5-aisgreaterthanb"
else:
print"Line5-aisnotgreaterthanb"
a=5;
b=20;
if(a<=b):
print"Line6-aiseitherlessthanorequalto b"
else:
print"Line6-aisneitherlessthannorequalto b"
if(b>=a):
print"Line7-biseithergreaterthan orequaltob"
else:
print"Line7-bisneithergreaterthan norequaltob"
以上实例输出结果:
Line1-aisnotequaltob
Line2-aisnotequaltob
Line3-aisnotequaltob
Line4-aisnotlessthanb
Line5-aisgreaterthanb
Line6-aiseitherlessthanorequaltob
Line7-biseithergreaterthanorequaltob
Python赋值运算符
以下假设变量a为10,变量b为20:
以下实例演示了Python所有赋值运算符的操作:
#!/usr/bin/python
a=21
b=10
c=0
c=a+b
print"Line1-Valueofcis",c
c+=a
print"Line2-Valueofcis",c
c*=a
print"Line3-Valueofcis",c
c/=a
print"Line4-Valueofcis",c
c =2
c%=a
print"Line5-Valueofcis",c
c**=a
print"Line6-Valueofcis",c
c//=a
print"Line7-Valueofcis",c
以上实例输出结果:
Line1-Valueofcis31
Line2-Valueofcis52
Line3-Valueofcis1092
Line4-Valueofcis52
Line5-Valueofcis2
Line6-Valueofcis2097152
Line7-Valueofcis99864
Python位运算符
按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下:
以下实例演示了Python所有位运算符的操作:
#!/usr/bin/python
a=60 #60=00111100
b=13 #13=00001101
c=0
c=a&b; #12=00001100
print"Line1-Valueofcis",c
c=a|b; #61=00111101
print"Line2-Valueofcis",c
c=a^b; #49=00110001
print"Line3-Valueofcis",c
c=~a; #-61=11000011
print"Line4-Valueofcis",c
c=a<<2; #240=11110000
print"Line5-Valueofcis",c
c=a>>2; #15=00001111
print"Line6-Valueofcis",c
以上实例输出结果:
Line1-Valueofcis12
Line2-Valueofcis61
Line3-Valueofcis49
Line4-Valueofcis-61
Line5-Valueofcis240
Line6-Valueofcis15
Python逻辑运算符
Python语言支持逻辑运算符,以下假设变量a为10,变量b为20:
以下实例演示了Python所有逻辑运算符的操作:
#!/usr/bin/python
a=10
b=20
c=0
if(aandb):
print"Line1-aandbaretrue"
else:
print"Line1-Eitheraisnottrueorbisnottrue"
if(aorb):
print"Line2-Eitheraistrueorbistrueorbotharetrue"
else:
print"Line2-Neitheraistruenorbistrue"
a=0
if(aandb):
print"Line3-aandbaretrue"
else:
print"Line3-Eitheraisnottrueorbisnottrue"
if(aorb):
print"Line4-Eitheraistrueorbistrueorbotharetrue"
else:
print"Line4-Neitheraistruenorbistrue"
ifnot(aandb):
print"Line5-Eitheraisnottrueorbis nottrueorbotharenottrue"
else:
print"Line5-aandbaretrue"
以上实例输出结果:
Line1-aandbaretrue
Line2-Eitheraistrueorbistrueorbotharetrue
Line3-Eitheraisnottrueorbisnottrue
Line4-Eitheraistrueorbistrueorbotharetrue
Line5-Eitheraisnottrueorbis nottrueorbotharenottrue
Python成员运算符
除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。
以下实例演示了Python所有成员运算符的操作:
#!/usr/bin/python
a=10
b=20
list=[1,2,3,4,5];
if(ainlist):
print"Line1-aisavailableinthegivenlist"
else:
print"Line1-aisnotavailableinthegivenlist"
if(bnotinlist):
print"Line2-bisnotavailableinthegivenlist"
else:
print"Line2-bisavailableinthegivenlist"
a=2
if(ainlist):
print"Line3-aisavailableinthegivenlist"
else:
print"Line3-aisnotavailableinthegivenlist"
以上实例输出结果:
Line1-aisnotavailableinthegivenlist
Line2-bisnotavailableinthegivenlist
Line3-aisavailableinthegivenlist
Python身份运算符
身份运算符用于比较两个对象的存储单元
以下实例演示了Python所有身份运算符的操作:
#!/usr/bin/python
a=20
b=20
if(aisb):
print"Line1-aandbhavesameidentity"
else:
print"Line1-aandbdonothavesameidentity"
if(id(a)==id(b)):
print"Line2-aandbhavesameidentity"
else:
print"Line2-aandbdonothavesameidentity"
b=30
if(aisb):
print"Line3-aandbhavesameidentity"
else:
print"Line3-aandbdonothavesameidentity"
if(aisnotb):
print"Line4-aandbdonothavesameidentity"
else:
print"Line4-aandbhavesameidentity"
以上实例输出结果:
Line1-aandbhavesameidentity
Line2-aandbhavesameidentity
Line3-aandbdonothavesameidentity
Line4-aandbdonothavesameidentity
Python运算符优先级
以下表格列出了从最高到最低优先级的所有运算符:
以下实例演示了Python所有运算符优先级的操作:
#!/usr/bin/python
a=20
b=10
c=15
d=5
e=0
e=(a+b)*c/d #(30*15)/5
print"Valueof(a+b)*c/dis", e
e=((a+b)*c)/d #(30*15)/5
print"Valueof((a+b)*c)/dis", e
e=(a+b)*(c/d); #(30)*(15/5)
print"Valueof(a+b)*(c/d)is", e
e=a+(b*c)/d; # 20+(150/5)
print"Valueofa+(b*c)/dis", e
以上实例输出结果:
Valueof(a+b)*c/dis90
Valueof((a+b)*c)/dis90
Valueof(a+b)*(c/d)is90
Valueofa+(b*c)/dis50
可以通过下图来简单了解条件语句的执行过程:
Python程序语言指定任何非0和非空(null)值为true,0或者null为false。
Python编程中if语句用于控制程序的执行,基本形式为:
其中"判断条件"成立时(非零),则执行后面的语句,而执行内容可以多行,以缩进来区分表示同一范围。
else为可选语句,当需要在条件不成立时执行内容则可以执行相关语句,具体例子如下:
输出结果为:
if语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。
当判断条件为多个值是,可以使用以下形式:
实例如下:
输出结果为:
由于python并不支持switch语句,所以多个条件判断,只能用elif来实现,如果判断需要多个条件需同时判断时,可以使用or(或),表示两个条件有一个成立时判断条件成功;使用and(与)时,表示只有两个条件同时成立的情况下,判断条件才成功。
当if有多个条件时可使用括号来区分判断的先后顺序,括号中的判断优先执行,此外and和or的优先级低于>(大于)、<(小于)等判断符号,即大于和小于在没有括号的情况下会比与或要优先判断。
以上代码执行输出结果如下:
什么是运算符?
本章节主要说明Python的运算符。举个简单的例子4+5=9。例子中,4和5被称为操作数,"+"号为运算符。
Python语言支持以下类型的运算符:
接下来让我们一个个来学习Python的运算符。
Python算术运算符
以下假设变量a为10,变量b为20:
运算符 | 描述 | 实例 |
+ | 加-两个对象相加 | a+b输出结果30 |
- | 减-得到负数或是一个数减去另一个数 | a-b输出结果-10 |
* | 乘-两个数相乘或是返回一个被重复若干次的字符串 | a*b输出结果200 |
/ | 除-x除以y | b/a输出结果2 |
% | 取模-返回除法的余数 | b%a输出结果0 |
** | 幂-返回x的y次幂 | a**b为10的20次方,输出结果100000000000000000000 |
// | 取整除-返回商的整数部分 | 9//2输出结果4,9.0//2.0输出结果4.0 |
#!/usr/bin/python
a=21
b=10
c=0
c=a+b
print"Line1-Valueofcis",c
c=a-b
print"Line2-Valueofcis",c
c=a*b
print"Line3-Valueofcis",c
c=a/b
print"Line4-Valueofcis",c
c=a%b
print"Line5-Valueofcis",c
a=2
b=3
c=a**b
print"Line6-Valueofcis",c
a=10
b=5
c=a//b
print"Line7-Valueofcis",c
以上实例输出结果:
Line1-Valueofcis31
Line2-Valueofcis11
Line3-Valueofcis210
Line4-Valueofcis2
Line5-Valueofcis1
Line6-Valueofcis8
Line7-Valueofcis2
Python比较运算符
以下假设变量a为10,变量b为20:
运算符 | 描述 | 实例 |
== | 等于-比较对象是否相等 | (a==b)返回False。 |
!= | 不等于-比较两个对象是否不相等 | (a!=b)返回true. |
<> | 不等于-比较两个对象是否不相等 | (a<>b)返回true。这个运算符类似!=。 |
> | 大于-返回x是否大于y | (a>b)返回False。 |
< | 小于-返回x是否小于y。所有比较运算符返回1表示真,返回0表示假。这分别与特殊的变量True和False等价。注意,这些变量名的大写。 | (a<b)返回true。 |
>= | 大于等于-返回x是否大于等于y。 | (a>=b)返回False。 |
<= | 小于等于-返回x是否小于等于y。 | (a<=b)返回true。 |
#!/usr/bin/python
a=21
b=10
c=0
if(a==b):
print"Line1-aisequaltob"
else:
print"Line1-aisnotequaltob"
if(a!=b):
print"Line2-aisnotequaltob"
else:
print"Line2-aisequaltob"
if(a<>b):
print"Line3-aisnotequaltob"
else:
print"Line3-aisequaltob"
if(a<b):
print"Line4-aislessthanb"
else:
print"Line4-aisnotlessthanb"
if(a>b):
print"Line5-aisgreaterthanb"
else:
print"Line5-aisnotgreaterthanb"
a=5;
b=20;
if(a<=b):
print"Line6-aiseitherlessthanorequalto b"
else:
print"Line6-aisneitherlessthannorequalto b"
if(b>=a):
print"Line7-biseithergreaterthan orequaltob"
else:
print"Line7-bisneithergreaterthan norequaltob"
以上实例输出结果:
Line1-aisnotequaltob
Line2-aisnotequaltob
Line3-aisnotequaltob
Line4-aisnotlessthanb
Line5-aisgreaterthanb
Line6-aiseitherlessthanorequaltob
Line7-biseithergreaterthanorequaltob
Python赋值运算符
以下假设变量a为10,变量b为20:
运算符 | 描述 | 实例 |
= | 简单的赋值运算符 | c=a+b将a+b的运算结果赋值为c |
+= | 加法赋值运算符 | c+=a等效于c=c+a |
-= | 减法赋值运算符 | c-=a等效于c=c-a |
*= | 乘法赋值运算符 | c*=a等效于c=c*a |
/= | 除法赋值运算符 | c/=a等效于c=c/a |
%= | 取模赋值运算符 | c%=a等效于c=c%a |
**= | 幂赋值运算符 | c**=a等效于c=c**a |
//= | 取整除赋值运算符 | c//=a等效于c=c//a |
#!/usr/bin/python
a=21
b=10
c=0
c=a+b
print"Line1-Valueofcis",c
c+=a
print"Line2-Valueofcis",c
c*=a
print"Line3-Valueofcis",c
c/=a
print"Line4-Valueofcis",c
c =2
c%=a
print"Line5-Valueofcis",c
c**=a
print"Line6-Valueofcis",c
c//=a
print"Line7-Valueofcis",c
以上实例输出结果:
Line1-Valueofcis31
Line2-Valueofcis52
Line3-Valueofcis1092
Line4-Valueofcis52
Line5-Valueofcis2
Line6-Valueofcis2097152
Line7-Valueofcis99864
Python位运算符
按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下:
运算符 | 描述 | 实例 |
& | 按位与运算符 | (a&b)输出结果12,二进制解释:00001100 |
| | 按位或运算符 | (a|b)输出结果61,二进制解释:00111101 |
^ | 按位异或运算符 | (a^b)输出结果49,二进制解释:00110001 |
~ | 按位取反运算符 | (~a)输出结果-61,二进制解释:11000011,在一个有符号二进制数的补码形式。 |
<< | 左移动运算符 | a<<2输出结果240,二进制解释:11110000 |
>> | 右移动运算符 | a>>2输出结果15,二进制解释:00001111 |
#!/usr/bin/python
a=60 #60=00111100
b=13 #13=00001101
c=0
c=a&b; #12=00001100
print"Line1-Valueofcis",c
c=a|b; #61=00111101
print"Line2-Valueofcis",c
c=a^b; #49=00110001
print"Line3-Valueofcis",c
c=~a; #-61=11000011
print"Line4-Valueofcis",c
c=a<<2; #240=11110000
print"Line5-Valueofcis",c
c=a>>2; #15=00001111
print"Line6-Valueofcis",c
以上实例输出结果:
Line1-Valueofcis12
Line2-Valueofcis61
Line3-Valueofcis49
Line4-Valueofcis-61
Line5-Valueofcis240
Line6-Valueofcis15
Python逻辑运算符
Python语言支持逻辑运算符,以下假设变量a为10,变量b为20:
运算符 | 描述 | 实例 |
and | 布尔"与"-如果x为False,xandy返回False,否则它返回y的计算值。 | (aandb)返回true。 |
or | 布尔"或"-如果x是True,它返回True,否则它返回y的计算值。 | (aorb)返回true。 |
not | 布尔"非"-如果x为True,返回False。如果x为False,它返回True。 | not(aandb)返回false。 |
#!/usr/bin/python
a=10
b=20
c=0
if(aandb):
print"Line1-aandbaretrue"
else:
print"Line1-Eitheraisnottrueorbisnottrue"
if(aorb):
print"Line2-Eitheraistrueorbistrueorbotharetrue"
else:
print"Line2-Neitheraistruenorbistrue"
a=0
if(aandb):
print"Line3-aandbaretrue"
else:
print"Line3-Eitheraisnottrueorbisnottrue"
if(aorb):
print"Line4-Eitheraistrueorbistrueorbotharetrue"
else:
print"Line4-Neitheraistruenorbistrue"
ifnot(aandb):
print"Line5-Eitheraisnottrueorbis nottrueorbotharenottrue"
else:
print"Line5-aandbaretrue"
以上实例输出结果:
Line1-aandbaretrue
Line2-Eitheraistrueorbistrueorbotharetrue
Line3-Eitheraisnottrueorbisnottrue
Line4-Eitheraistrueorbistrueorbotharetrue
Line5-Eitheraisnottrueorbis nottrueorbotharenottrue
Python成员运算符
除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。
运算符 | 描述 | 实例 |
in | 如果在指定的序列中找到值返回True,否则返回False。 | x在y序列中,如果x在y序列中返回True。 |
notin | 如果在指定的序列中没有找到值返回True,否则返回False。 | x不在y序列中,如果x不在y序列中返回True。 |
#!/usr/bin/python
a=10
b=20
list=[1,2,3,4,5];
if(ainlist):
print"Line1-aisavailableinthegivenlist"
else:
print"Line1-aisnotavailableinthegivenlist"
if(bnotinlist):
print"Line2-bisnotavailableinthegivenlist"
else:
print"Line2-bisavailableinthegivenlist"
a=2
if(ainlist):
print"Line3-aisavailableinthegivenlist"
else:
print"Line3-aisnotavailableinthegivenlist"
以上实例输出结果:
Line1-aisnotavailableinthegivenlist
Line2-bisnotavailableinthegivenlist
Line3-aisavailableinthegivenlist
Python身份运算符
身份运算符用于比较两个对象的存储单元
运算符 | 描述 | 实例 |
is | is是判断两个标识符是不是引用自一个对象 | xisy,如果id(x)等于id(y),is返回结果1 |
isnot | isnot是判断两个标识符是不是引用自不同对象 | xisnoty,如果id(x)不等于id(y).isnot返回结果1 |
#!/usr/bin/python
a=20
b=20
if(aisb):
print"Line1-aandbhavesameidentity"
else:
print"Line1-aandbdonothavesameidentity"
if(id(a)==id(b)):
print"Line2-aandbhavesameidentity"
else:
print"Line2-aandbdonothavesameidentity"
b=30
if(aisb):
print"Line3-aandbhavesameidentity"
else:
print"Line3-aandbdonothavesameidentity"
if(aisnotb):
print"Line4-aandbdonothavesameidentity"
else:
print"Line4-aandbhavesameidentity"
以上实例输出结果:
Line1-aandbhavesameidentity
Line2-aandbhavesameidentity
Line3-aandbdonothavesameidentity
Line4-aandbdonothavesameidentity
Python运算符优先级
以下表格列出了从最高到最低优先级的所有运算符:
运算符 | 描述 |
** | 指数(最高优先级) |
~+- | 按位翻转,一元加号和减号(最后两个的方法名为+@和-@) |
*/%// | 乘,除,取模和取整除 |
+- | 加法减法 |
>><< | 右移,左移运算符 |
& | 位'AND' |
^| | 位运算符 |
<=<>>= | 比较运算符 |
<>==!= | 等于运算符 |
=%=/=//=-=+=*=**= | 赋值运算符 |
isisnot | 身份运算符 |
innotin | 成员运算符 |
notorand | 逻辑运算符 |
#!/usr/bin/python
a=20
b=10
c=15
d=5
e=0
e=(a+b)*c/d #(30*15)/5
print"Valueof(a+b)*c/dis", e
e=((a+b)*c)/d #(30*15)/5
print"Valueof((a+b)*c)/dis", e
e=(a+b)*(c/d); #(30)*(15/5)
print"Valueof(a+b)*(c/d)is", e
e=a+(b*c)/d; # 20+(150/5)
print"Valueofa+(b*c)/dis", e
以上实例输出结果:
Valueof(a+b)*c/dis90
Valueof((a+b)*c)/dis90
Valueof(a+b)*(c/d)is90
Valueofa+(b*c)/dis50
Python条件语句
Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。可以通过下图来简单了解条件语句的执行过程:
Python程序语言指定任何非0和非空(null)值为true,0或者null为false。
Python编程中if语句用于控制程序的执行,基本形式为:
if判断条件:
执行语句……
else:
执行语句……
其中"判断条件"成立时(非零),则执行后面的语句,而执行内容可以多行,以缩进来区分表示同一范围。
else为可选语句,当需要在条件不成立时执行内容则可以执行相关语句,具体例子如下:
#!/usr/bin/python
#-*-coding:UTF-8-*-
#例1:if基本用法
flag=False
name='luren'
ifname=='python': #判断变量否为'python'
flag=True #条件成立时设置标志为真
print'welcomeboss' #并输出欢迎信息
else:
printname #条件不成立时输出变量名称
输出结果为:
>>>luren #输出结果
if语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。
当判断条件为多个值是,可以使用以下形式:
if判断条件1:
执行语句1……
elif判断条件2:
执行语句2……
elif判断条件3:
执行语句3……
else:
执行语句4……
实例如下:
#!/usr/bin/python
#-*-coding:UTF-8-*-
#例2:elif用法
num=5
ifnum==3: #判断num的值
print'boss'
elifnum==2:
print'user'
elifnum==1:
print'worker'
elifnum<0: #值小于零时输出
print'error'
else:
print'roadman' #条件均不成立时输出
输出结果为:
>>>roadman #输出结果
由于python并不支持switch语句,所以多个条件判断,只能用elif来实现,如果判断需要多个条件需同时判断时,可以使用or(或),表示两个条件有一个成立时判断条件成功;使用and(与)时,表示只有两个条件同时成立的情况下,判断条件才成功。
#!/usr/bin/python
#-*-coding:UTF-8-*-
#例3:if语句多个条件
num=9
ifnum>=0andnum<=10: #判断值是否在0~10之间
print'hello'
>>>hello #输出结果
num=10
ifnum<0ornum>10: #判断值是否在小于0或大于10
print'hello'
else:
print'undefine'
>>>undefine #输出结果
num=8
#判断值是否在0~5或者10~15之间
if(num>=0andnum<=5)or(num>=10andnum<=15):
print'hello'
else:
print'undefine'
>>>undefine #输出结果
当if有多个条件时可使用括号来区分判断的先后顺序,括号中的判断优先执行,此外and和or的优先级低于>(大于)、<(小于)等判断符号,即大于和小于在没有括号的情况下会比与或要优先判断。
简单的语句组
你也可以在同一行的位置上使用if条件判断语句,如下实例:#!/usr/bin/python
#-*-coding:UTF-8-*-
var=100
if(var ==100):print"变量var的值为100"
print"Goodbye!"
以上代码执行输出结果如下:
变量var的值为100
Goodbye!
出处:http://www.runoob.com/python/python-tutorial.html
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- Extjs4.0 最新最全视频教程
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- OpenERP 的XML-RPC的实例+many2many,one2many,many2one...
- CSS3属性教程与案例分享
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- jquery教程靠边站,一分钱不花让你免费学会jquery
- Python在CAM软件Genesis2000中的应用