1. print( 坑的信息 )

  • 挖坑时间:2019/01/10
  • 明细
坑的编码 内容
Py006-2 索引君的朋友 in

2. 开始填坑

(1) 前情提要

  • 上回说到,index() 的索引值超出范围会抛出异常,如
list0 = [0, 1, 2, 3, 4, 5, 6]
print(list0.index(8))
  • 运行结果

ValueError: 8 is not in list

(2) 索引君的朋友 in 上线

  • 少废话,上例子
# 例1.1

list1 = [0, 1, 2, 3, 4, 5, 6]
if 8 in list1:
print("8 is in this list.")
else:
print("Sorry, 8 is not in this list.")
  • 运行结果

Sorry, 8 is not in this list.


# 例1.2

list1 = [0, 1, 2, 3, 4, 5, 6]
boolean = 8 in list1
print(boolean)
  • 运行结果

False

in 返回 True 或 False,且不报错,但不能像 index() 那样索引到具体的值。


(3) 既然说了 in,不妨再说一说 not in

  • 少废话,上例子
# 例2

list2 = [0, 1, 2, 3, 4, 5, 6]
if 8 not in list1:
print("8 is in this list.")
else:
print("Sorry, 8 is not in this list.")
boolean = 8 in list2
print(boolean)
  • 运行结果

8 is in this list.

False


(4) 一些补充

  • in、not in 只能判断一层关系
# 例3

list3 = [0, 1, 2, [3, 4, 5]]
if 3 in list3:
print("YES")
else:
print("NO")
  • 运行结果

NO


  • 解决办法
# 例4

list4 = [0, 1, 2, [3, 4, 5]]
if 3 in list4[3]:
print("YES")
else:
print("NO")
  • 运行结果

YES


我的学识有限,如果有同学、老师或者前辈看到我写的东西,发现错误之处,还请不吝赐教!谢谢!

[Python3 填坑] 008 索引君的朋友 in的更多相关文章

  1. [Python3 填坑] 006 “杠零”,空字符的使用

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 \0 是空字符,输出时看不到它,但它占 1 个字符的长度 2.2 \0 "遇八进制失效" 2.3 \0 与 '' 不 ...

  2. [Python3 填坑] 011 元组中有多个最值,索引出来的是哪一个

    目录 1. print( 坑的信息 ) 2. 开始填坑 (1) max() (2) min() (3) 结论 1. print( 坑的信息 ) 挖坑时间:2019/01/11 明细 坑的编码 内容 P ...

  3. [Python3 填坑] 004 关于八进制

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 2.2.1 先说结论 2.2.2 八进制的用途 2.2.3 少废话,上例子 1. print( 坑的信息 ...

  4. [Python3 填坑] 001 格式化符号 & 格式化操作符的辅助指令

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python 格式化符号表 举例说明 (1) %c (2) %s 与 %d (3) %o (4) %x (5) %f (6) %e (7 ...

  5. [Python3 填坑] 012 字典的遍历在 Python2 与 Python3 中区别

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python2 中字典的遍历 2.2 Python3 中字典的遍历 2.3 结论 1. print( 坑的信息 ) 挖坑时间:2019/ ...

  6. [Python3 填坑] 009 深拷贝与浅拷贝

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python3.7 官方文档 2.2 赋值.切片与 copy() 分析 分析 分析 分析 2.3 copy 模块 分析 分析 2.4 小 ...

  7. [Python3 填坑] 005 如何“响铃”

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 1. print( 坑的信息 ) 挖坑时间:2019/01/08 明细 坑的编码 内容 Py004-2 ...

  8. [Python3 填坑] 003 关键字?保留字?预留字?

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 网上搜索 2.3 结论 2.4 后记 1. print( 坑的信息 ) 挖坑时间:2019/01/04 明细 坑的编 ...

  9. [Python3 填坑] 018 组装类的几个例子

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 MetaClass 举例 2.2 type 举例 2.3 MetaClass 举例 1. print( 坑的信息 ) 挖坑时间:2019 ...

随机推荐

  1. bean获取Spring容器

    Person.java public class Person implements ApplicationContextAware{ private String name; private int ...

  2. Vscode中解决Html文件中不能打断点问题

    Vscode中解决Html文件中不能打断点问题: 默认情况下,VSCode是不能在Html文件里打断点的,但是可以修改设置,依次打开:文件->首选项->设置,然后功能->调试-> ...

  3. HDU 5446 Unknown Treasure(lucas + 中国剩余定理 + 模拟乘法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, ...

  4. compile and link C/CPP programs on Mac

    ref: https://stackoverflow.com/questions/29987716/cannot-use-gsl-library-on-macos-ld-symbols-not-fou ...

  5. handy源码阅读(四):Channel类

    通道,封装了可以进行epoll的一个fd. struct Channel: private noncopyable { Channel(EventBase* base, int fd, int eve ...

  6. JavaSE——柏羲

    MySql Redis源码分析呢?

  7. APK文件结构和安装过程

    APK文件结构Android应用是用Java编写的,利用Android SDK编译代码,并且把所有的数据和资源文件打包成一个APK (Android Package)文件,这是一个后缀名为.apk的压 ...

  8. CCPC E Problem Buyer

    题目描述 有一些区间,还有一些点. 问最小的k使得选出任意k个区间,每个点都可以匹配上区间,一个区间只能匹配一次. 题解 考虑对于每一个点,我们有\(x\)个区间包含它,那么答案的一个下界是\(n-x ...

  9. [ethereum源码分析](3) ethereum初始化指令

    前言 在上一章介绍了关于区块链的一些基础知识,这一章会分析指令 geth --datadir dev/data/02 init private-geth/genesis.json 的源码,若你的eth ...

  10. Hello Kotlin! Kotlin学习资料

    今天谷歌搞了条大新闻.宣布Kotlin成为android开发的一级(One Class)语言,这说明谷歌是被甲骨文恶心坏了,打算一步步脱离掉java或者说是甲骨文公司的束缚了.原先网上大家还琢磨着会不 ...