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. bzoj4165 矩阵 堆维护多路归并

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4165 题解 大概多路归并是最很重要的知识点了吧,近几年考察也挺多的(虽然都是作为签到题的). ...

  2. springboot easyexcel

    pom..xml <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel&l ...

  3. easyui自学模板代码

    index.jsp源码 <%@ page language="java" contentType="text/html; charset=UTF-8" p ...

  4. vue请求数据

    vue-resource: 推荐教程:https://www.runoob.com/vue2/vuejs-ajax.html 1. 需要安装vue-resource模块, 注意加上 --save np ...

  5. 激活函数:Sigmod&tanh&Softplus&Relu详解

    什么是激活函数? 激活函数(Activation functions)对于人工神经网络模型去学习.理解非常复杂和非线性的函数来说具有十分重要的作用. 它们将非线性特性引入到我们的网络中.其主要目的是将 ...

  6. 滑块QAbstractSlider

    继承于 QWidget 抽象类-必须子类化 提供的范围内的整数值 QAbstractSlider import sys from PyQt5.QtWidgets import QApplication ...

  7. 【leetcode】1138. Alphabet Board Path

    题目如下: On an alphabet board, we start at position (0, 0), corresponding to character board[0][0]. Her ...

  8. 【leetcode】Decode Ways

    题目如下: 解题思路:这个题目的本质是一个爬楼梯问题,在爬楼梯问题中,每次可以选择走一步或者两步.而本题也是一样的,解码的时候选择解码一个字符或者两个字符,但是加大了一点点难度,要考虑这些情况.1,Z ...

  9. 《x86汇编语言:从实模式到保护模式 》学习笔记之:第一次编写汇编语言

    1.汇编语言源文件:first.asm mov ax,0x3f add bx,ax add cx,ax 2.用nasm编译成二进制文件:first.bin nasm -f bin first.asm ...

  10. 学习日记18、easyui 文件框上传文件

    前台 <tr> <td style="width:100px; text-align:right;"> @Html.LabelFor(model => ...