Python基础教程(三):运算符、条件语句

Python 运算符


什么是运算符?

本章节主要说明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

以下实例演示了Python所有算术运算符的操作:

#!/usr/bin/python

 

a = 21

b = 10

c = 0

 

c = a + b

print "Line 1 - Value of c is ", c

 

c = a - b

print "Line 2 - Value of c is ", c

 

c = a * b

print "Line 3 - Value of c is ", c

 

c = a / b

print "Line 4 - Value of c is ", c

 

c = a % b

print "Line 5 - Value of c is ", c

 

a = 2

b = 3

c = a**b

print "Line 6 - Value of c is ", c

 

a = 10

b = 5

c = a//b

print "Line 7 - Value of c is ", c

以上实例输出结果:

Line 1 - Value of c is 31

Line 2 - Value of c is 11

Line 3 - Value of c is 210

Line 4 - Value of c is 2

Line 5 - Value of c is 1

Line 6 - Value of c is 8

Line 7 - Value of c is 2


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。

以下实例演示了Python所有比较运算符的操作:

#!/usr/bin/python

 

a = 21

b = 10

c = 0

 

if ( a == b ):

   print "Line1 - a is equal to b"

else:

   print "Line1 - a is not equal to b"

 

if ( a != b ):

   print "Line2 - a is not equal to b"

else:

   print "Line2 - a is equal to b"

 

if ( a <> b ):

   print "Line3 - a is not equal to b"

else:

   print "Line3 - a is equal to b"

 

if ( a < b ):

   print "Line4 - a is less than b"

else:

   print "Line4 - a is not less than b"

 

if ( a > b ):

   print "Line5 - a is greater than b"

else:

   print "Line5 - a is not greater than b"

 

a = 5;

b = 20;

if ( a <= b ):

   print "Line6 - a is either less than or equal to b"

else:

   print "Line6 - a is neither less than nor equal to b"

 

if ( b >= a ):

   print "Line7 - b is either greater than  or equal tob"

else:

   print "Line7 - b is neither greater than  nor equalto b"

以上实例输出结果:

Line 1 - a is not equal to b

Line 2 - a is not equal to b

Line 3 - a is not equal to b

Line 4 - a is not less than b

Line 5 - a is greater than b

Line 6 - a is either less than or equal to b

Line 7 - b is either greater than or equal to b


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

以下实例演示了Python所有赋值运算符的操作:

#!/usr/bin/python

 

a = 21

b = 10

c = 0

 

c = a + b

print "Line 1 - Value of c is ", c

 

c += a

print "Line 2 - Value of c is ", c

 

c *= a

print "Line 3 - Value of c is ", c

 

c /= a

print "Line 4 - Value of c is ", c

 

c  = 2

c %= a

print "Line 5 - Value of c is ", c

 

c **= a

print "Line 6 - Value of c is ", c

 

c //= a

print "Line 7 - Value of c is ", c

以上实例输出结果:

Line 1 - Value of c is 31

Line 2 - Value of c is 52

Line 3 - Value of c is 1092

Line 4 - Value of c is 52

Line 5 - Value of c is 2

Line 6 - Value of c is 2097152

Line 7 - Value of c is 99864


Python位运算符

按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下:

运算符

描述

实例

&

按位与运算符

(a & b) 输出结果 12 ,二进制解释: 0000 1100

|

按位或运算符

(a | b) 输出结果 61 ,二进制解释: 0011 1101

^

按位异或运算符

(a ^ b) 输出结果 49 ,二进制解释: 0011 0001

~

按位取反运算符

(~a ) 输出结果 -61 ,二进制解释: 1100 0011, 在一个有符号二进制数的补码形式。

<< 

左移动运算符

a << 2 输出结果 240 ,二进制解释: 1111 0000

>> 

右移动运算符

a >> 2 输出结果 15 ,二进制解释: 0000 1111

以下实例演示了Python所有位运算符的操作:

#!/usr/bin/python

 

a = 60            #60 = 0011 1100

b = 13            #13 = 0000 1101

c = 0

 

c = a & b;       # 12 = 0000 1100

print "Line 1 - Value of c is ", c

 

c = a | b;        #61 = 0011 1101

print "Line 2 - Value of c is ", c

 

c = a ^ b;        #49 = 0011 0001

print "Line 3 - Value of c is ", c

 

c = ~a;           # -61 = 1100 0011

print "Line 4 - Value of c is ", c

 

c = a << 2;      # 240 = 1111 0000

print "Line 5 - Value of c is ", c

 

c = a >> 2;      # 15 = 0000 1111

print "Line 6 - Value of c is ", c

以上实例输出结果:

Line 1 - Value of c is 12

Line 2 - Value of c is 61

Line 3 - Value of c is 49

Line 4 - Value of c is -61

Line 5 - Value of c is 240

Line 6 - Value of c is 15


Python逻辑运算符

Python语言支持逻辑运算符,以下假设变量a为10,变量b为20:

运算符

描述

实例

and

布尔"与" - 如果x为False,x and y返回False,否则它返回y的计算值。

(a and b) 返回 true。

or

布尔"或" - 如果x是True,它返回True,否则它返回y的计算值。

(a or b) 返回 true。

not

布尔"非" - 如果x为True,返回False。如果x为False,它返回True。

not(a and b) 返回 false。

以下实例演示了Python所有逻辑运算符的操作:

#!/usr/bin/python

 

a = 10

b = 20

c = 0

 

if ( a and b ):

   print "Line1 - a and b are true"

else:

   print "Line1 - Either a is not true or b is not true"

 

if ( a or b ):

   print "Line2 - Either a is true or b is true or both are true"

else:

   print "Line2 - Neither a is true nor b is true"

 

 

a = 0

if ( a and b ):

   print "Line3 - a and b are true"

else:

   print "Line3 - Either a is not true or b is not true"

 

if ( a or b ):

   print "Line4 - Either a is true or b is true or both are true"

else:

   print "Line4 - Neither a is true nor b is true"

 

if not( a and b ):

   print "Line5 - Either a is not true or b is  nottrue or both are not true"

else:

   print "Line5 - a and b are true"

以上实例输出结果:

Line 1 - a and b are true

Line 2 - Either a is true or b is true or both are true

Line 3 - Either a is not true or b is not true

Line 4 - Either a is true or b is true or both are true

Line 5 - Either a is not true or b is  not true or both are not true


Python成员运算符

除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。

运算符

描述

实例

in

如果在指定的序列中找到值返回True,否则返回False。

x 在 y序列中 , 如果x在y序列中返回True。

not in

如果在指定的序列中没有找到值返回True,否则返回False。

x 不在 y序列中 , 如果x不在y序列中返回True。

以下实例演示了Python所有成员运算符的操作:

#!/usr/bin/python

 

a = 10

b = 20

list = [1, 2, 3, 4, 5 ];

 

if ( a in list ):

   print "Line1 - a is available in the given list"

else:

   print "Line1 - a is not available in the given list"

 

if ( b not in list ):

   print "Line2 - b is not available in the given list"

else:

   print "Line2 - b is available in the given list"

 

a = 2

if ( a in list ):

   print "Line3 - a is available in the given list"

else:

   print "Line3 - a is not available in the given list"

以上实例输出结果:

Line 1 - a is not available in the given list

Line 2 - b is not available in the given list

Line 3 - a is available in the given list


Python身份运算符

身份运算符用于比较两个对象的存储单元

运算符

描述

实例

is

is是判断两个标识符是不是引用自一个对象

x is y, 如果 id(x) 等于 id(y) , is 返回结果 1

is not

is not是判断两个标识符是不是引用自不同对象

x is not y, 如果 id(x) 不等于 id(y). is not 返回结果 1

以下实例演示了Python所有身份运算符的操作:

#!/usr/bin/python

 

a = 20

b = 20

 

if ( a is b ):

   print "Line1 - a and b have same identity"

else:

   print "Line1 - a and b do not have same identity"

 

if ( id(a) == id(b) ):

   print "Line2 - a and b have same identity"

else:

   print "Line2 - a and b do not have same identity"

 

b = 30

if ( a is b ):

   print "Line3 - a and b have same identity"

else:

   print "Line3 - a and b do not have same identity"

 

if ( a is not b ):

   print "Line4 - a and b do not have same identity"

else:

   print "Line4 - a and b have same identity"

以上实例输出结果:

Line 1 - a and b have same identity

Line 2 - a and b have same identity

Line 3 - a and b do not have same identity

Line 4 - a and b do not have same identity


Python运算符优先级

以下表格列出了从最高到最低优先级的所有运算符:

运算符

描述

**

指数 (最高优先级)

~ + -

按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)

* / % //

乘,除,取模和取整除

+ -

加法减法

>> <<

右移,左移运算符

&

位 'AND'

^ |

位运算符

<= < > >=

比较运算符

<> == !=

等于运算符

= %= /= //= -= += *= **=

赋值运算符

is is not

身份运算符

in not in

成员运算符

not or and

逻辑运算符

以下实例演示了Python所有运算符优先级的操作:

#!/usr/bin/python

 

a = 20

b = 10

c = 15

d = 5

e = 0

 

e = (a + b) * c / d      #( 30 * 15 ) / 5

print "Value of (a + b) * c / d is ",  e

 

e = ((a + b) * c) / d    # (30 * 15 ) / 5

print "Value of ((a + b) * c) / d is ",  e

 

e = (a + b) * (c / d);   # (30) * (15/5)

print "Value of (a + b) * (c / d) is ",  e

 

e = a + (b * c) / d;     #  20 + (150/5)

print "Value of a + (b * c) / d is ",  e

以上实例输出结果:

Value of (a + b) * c / d is 90

Value of ((a + b) * c) / d is 90

Value of (a + b) * (c / d) is 90

Value of a + (b * c) / d is 50

 

 

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'
if name == 'python':         # 判断变量否为'python'
    flag = True          # 条件成立时设置标志为真
    print 'welcome boss'    # 并输出欢迎信息
else:
    print name              # 条件不成立时输出变量名称

输出结果为:

>>> luren                      # 输出结果

if 语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。

当判断条件为多个值是,可以使用以下形式:

if 判断条件1:
    执行语句1……
elif 判断条件2:
    执行语句2……
elif 判断条件3:
    执行语句3……
else:
    执行语句4……

实例如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 例2:elif用法
 
num = 5     
if num == 3:            # 判断num的值
    print 'boss'        
elif num == 2:
    print 'user'
elif num == 1:
    print 'worker'
elif num < 0:           # 值小于零时输出
    print 'error'
else:
    print 'roadman'     # 条件均不成立时输出

输出结果为:

>>> roadman            # 输出结果

由于 python 并不支持 switch 语句,所以多个条件判断,只能用 elif 来实现,如果判断需要多个条件需同时判断时,可以使用 or (或),表示两个条件有一个成立时判断条件成功;使用 and (与)时,表示只有两个条件同时成立的情况下,判断条件才成功。

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
# 例3:if语句多个条件
 
num = 9
if num >= 0 and num <= 10:    # 判断值是否在0~10之间
    print 'hello'
>>> hello              # 输出结果
 
num = 10
if num < 0 or num > 10:    # 判断值是否在小于0或大于10
    print 'hello'
else:
        print 'undefine'
>>> undefine           # 输出结果
 
num = 8
# 判断值是否在0~5或者10~15之间
if (num >= 0 and num <= 5) or (num >= 10 and num <= 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 "Good bye!" 

以上代码执行输出结果如下:

变量 var 的值为100
Good bye!

出处:http://www.runoob.com/python/python-tutorial.html
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值