python 成员运算符

Python has two membership operators – “in” and “not in”. They are used to check if an element is present in a sequence or not.

Python有两个成员运算符-“ in”和“ not in”。 它们用于检查元素是否按顺序存在。

运算符中的Python (Python in Operator)

This operator returns True if the specified element is present in the sequence. The syntax of “in” operator is:

如果序列中存在指定的元素,则此运算符返回True。 “ in”运算符的语法为:


x in y

Here “x” is the element and “y” is the sequence where membership is being checked.

“ x”是元素,“ y”是检查成员资格的顺序。

Here is a simple program to show the usage of Python in operator.

这是一个简单的程序,用于显示运算符中Python的用法。


vowels = ['A', 'E', 'I', 'O', 'U']ch = input('Please Enter a Capital Letter:\n')if ch in vowels:print('You entered a vowel character')
else:print('You entered a consonants character')
Python in Operator Example
运算符示例中的Python

Recommended Readings: Python input(), Python List

推荐读物 : Python input() , Python列表

We can use the “in” operator with Strings and Tuples too because they are sequences.

我们也可以对字符串和元组使用“ in”运算符,因为它们是序列。


>>> name='JournalDev'
>>> 'D' in name
True
>>> 'x' in name
False
>>> primes=(2,3,5,7,11)
>>> 3 in primes
True
>>> 6 in primes
False

我们可以在字典中使用Python“ in”运算符吗? (Can we use Python “in” Operator with Dictionary?)

Let’s see what happens when we use “in” operator with a dictionary.

让我们看看将“ in”运算符与字典一起使用会发生什么情况。


dict1 = {"name": "Pankaj", "id": 1}print("name" in dict1)  # True
print("Pankaj" in dict1)  # False

It looks like the Python “in” operator looks for the element in the dictionary keys.

看起来像Python的“ in”运算符在字典键中寻找元素。

Python“不在”运算符 (Python “not in” Operator)

It’s opposite of the “in” operator. We can use it with sequence and iterables.

它与“ in”运算符相反。 我们可以将其与sequ​​ence和iterables一起使用。


>>> primes=(2,3,5,7,11)
>>> 6 not in primes
True
>>> 3 not in primes
False

翻译自: https://www.journaldev.com/38028/python-in-and-not-in-membership-operators

python 成员运算符

python 成员运算符_Python的“ in”和“ not in”成员资格运算符相关推荐

  1. python数值运算符也叫内置运算符_Python全栈工程师(数值类型、运算符)

    1.十进制表示方式 10 200 -100 999 八进制: 0o177(127) 0o11(9) 2.十六进制: 0x11(17) 0xff(255) 0x1a2b 3.二进制: 0b111 0b1 ...

  2. python语言的取余运算符_Python 中用于整数除法取余的运算符是()_学小易找答案...

    [填空题]隐球菌病多由()传播. [单选题]下列选项中,哪一个符号是管道符号. ( ) [多选题]可采用分批法计算产品成本的企业有( ) [填空题]现有字符串 s="1234567" ...

  3. python类的成员函数_Python为类对象动态添加成员函数

    Python: 为对象动态添加函数 , 且函数定义 来自一个 str 在 Python 中 , 通常情况下 , 你只能为对象添加一个已经写好的方法 需求 : 传入一个 str 类型的变量 , 其值是一 ...

  4. python变量运算符_Python基础 — 变量和运算符

    序言:这一章我们将学习变量以及常见的类型,我们将以案例和代码相结合的方式进行梳理,但是其中所有的案例和知识点 都是Python3 版本. 变量和运算符 1.1 初步介绍 ​ 在程序设计中,变量是一种存 ...

  5. python 对象的异或运算符_python的运算符

    算数运算符 算数运算符主要用作于计算机的算数运算 种类符号作用+加法.字符串的拼接 -减法 *乘法.字符串的重复 /除法 //地板除(除法) %取余(除法) **幂运算 +# 数字类型的加法运算 pr ...

  6. python中基本运算符_Python中的基本运算符及示例

    python中基本运算符 Operators are symbols which tells the interpreter to do a specific operation such as ar ...

  7. python快速运算符_Python基本运算符

    运算符是可以操纵操作数值的结构.如下一个表达式:10 + 20 = 30.这里,10和20称为操作数,+则被称为运算符. 运算符类型 Python语言支持以下类型的运算符 - 1.算术运算符 2.比较 ...

  8. python根号运算符_Python 运算符

    在本文中,您将学习有关Python中不同类型的运算符,它们的语法以及如何在示例中使用它们的所有知识. python中的运算符是什么? 运算符是Python中执行算术或逻辑计算的特殊符号.运算符所操作的 ...

  9. python中算术运算符_python中的基本算术运算符有哪些

    python中的基本算术运算符有哪些 发布时间:2020-11-16 14:54:33 来源:亿速云 阅读:74 今天就跟大家聊聊有关python中的基本算术运算符有哪些,可能很多人都不太了解,为了让 ...

最新文章

  1. lambda^k/k! 积分
  2. 【图灵】iOS技能书单——入门+进阶+精通
  3. SqlServer示例数据库Northwind(一)——实体关系
  4. vs studio 2017/2015 连接mysql报错 You have a usable connection already.
  5. linux mysql库看不见,linux下Mysql找不到mysqlclient库!谢谢大家
  6. A different twist on pre-compiling JSPs--reference
  7. gitlab mysql 配置_gitlab的安装与修改端口配置
  8. 【三万字!】Dubbo、Zookeeper学习笔记!秒杀面试官!——双非上岸阿里巴巴系列
  9. 解决outlook2013设置错误无法启动
  10. java 创建线程_java多线程|创建线程的各种方式
  11. Django模板:过滤器
  12. ArcGIS模型操作
  13. 鸿蒙HMS大势渐成,P40系列开启华为终端新征程
  14. Delphi教程推荐
  15. java中的math pow,Java Math.pow(a,b)时间复杂度
  16. js JavaScript 求100以内的质数(素数)和合数 以及奇数和偶数
  17. 多域名一个公网ip多内网应用服务同时使用80端口
  18. MAYA安装未完成,某些产品无法安装的解决方法
  19. 免费在线汉信码识别(Online Hanxin Decoder)
  20. ps 2019直装版 for Mac

热门文章

  1. 在window.onload中使用setTimeout
  2. 杭电1232(图—并查集)
  3. oracle 锁表查询及解决、表字段查询
  4. python解释器的使用
  5. [转帖]SQL SERVER 2005 安全设置
  6. 关于C语言中文件的基本操作
  7. [转载] lstm时间序列预测_pytorch入门使用PyTorch进行LSTM时间序列预测
  8. [转载] Python中三种类型的引号(单引号、双引号、三引号)
  9. CF1110G Tree-Tac-Toe 博弈论、构造
  10. Java接口之间的继承