原创

Python 使用成员运算符案例

什么是操作符?

简单的回答可以使用表达式4 + 5等于9,在这里4和5被称为操作数,+被称为操符。 Python语言支持操作者有以下几种类型。

算术运算符

比较(即关系)运算符

赋值运算符

逻辑运算符

位运算符

会员操作符

标识操作符

下面简单介绍一下,Python的成员运算符,下表列出了所有Python语言支持的成员运算符。

操作符描述示例
in计算结果为true,如果它在指定找到变量的顺序,否则false。x在y中,在这里产生一个1,如果x是序列y的成员。
not in计算结果为true,如果它不找到在指定的变量顺序,否则为false。x不在y中,这里产生结果不为1,如果x不是序列y的成员。

Python编程语言提供会员运算符,代码实现如下:

#!/usr/bin/python
 
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
 
if ( a in list ):
  print "Line 1 - a is available in the given list"
else:
  print "Line 1 - a is not available in the given list"
 
if ( b not in list ):
  print "Line 2 - b is not available in the given list"
else:
  print "Line 2 - b is available in the given list"
 
a = 2
if ( a in list ):
  print "Line 3 - a is available in the given list"
else:
  print "Line 3 - 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
~阅读全文-人机检测~

微信公众号“Java精选”(w_z90110),专注Java技术干货分享!让你从此路人变大神!回复关键词领取资料:如Mysql、Hadoop、Dubbo、Spring Boot等,免费领取视频教程、资料文档和项目源码。微信搜索小程序“Java精选面试题”,内涵3000+道Java面试题!

涵盖:互联网那些事、算法与数据结构、SpringMVC、Spring boot、Spring Cloud、ElasticSearch、Linux、Mysql、Oracle等

评论

分享:

支付宝

微信