master
/ 2.6.2 成员运算.ipynb

2.6.2 成员运算.ipynb @master

d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 成员运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font size=\"4\">运算符 in 和 not in 用于成员检测,返回布尔值True 或 False。如果 x 是 s 的成员则 x in s 值为 True,否则为 False。 x not in s 返回 x in s 取反后的值。所有内置序列和集合类型以及字典都支持此运算,对于字典来说 in 检测其是否在字典的键中存在。\n",
    "对于字符串和字节串类型来说,当且仅当 x 是 y 的子串时 x in y 为 True。空字符串总是被视为任何其他字符串的子串,因此 \"\" in \"abc\" 将返回 True。成员运算符的描述如表 2.2 所示。</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| 运算符 | 描述    | 实例 |\n",
    "| :------ | :--------- | :------------ |\n",
    "| in     | 如果对象在某一个序列中存在,返回  True,否则返回 False  | print('u' in ['U','u','USD'])   # 输出 True |\n",
    "| not in    |如果对象在某一个序列中不存在,返回  True,否则返回 False | print('r' not in ['U','u','USD']) # 输出True |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python 中的成员运算使用语法是:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj [not] in sequence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ #####  `x in s` :如果 x <font color=Red>__是__</font> s 的<font color=Red>__成员__</font>则返回值值为True,否则为False。  \n",
    "\n",
    "+ #####  `x not in s` :如果 x <font color=Red>__不是__</font> s 的<font color=Red>__成员__</font>则返回值为True,否则为False。即返回x in s 取反后的值。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "s 可以是字符串,列表、元组、range、字典的键、集合等。\n",
    "+ 对于字符串和字节串类型来说,当且仅当x 是y 的<font color=Red>__子串__</font>时,`x in y` 为True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print('admin' in 'administrator')    # True\n",
    "print('ada' in 'administrator')      # False\n",
    "print(' ' in 'admin')                # False,空格字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font size=\"4\"><font color=Red>__空字符串__</font>总是被视为任何其他字符串的<font color=Red>__子串__</font>。</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "print('' in 'admin')    # True,空字符串\n",
    "print(' ' in 'admin')   # False,空格字符串\n",
    "\n",
    "for i in range(10):\n",
    "    print(i)\n",
    "    \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ ### 所有内置序列、集合类型、字典都支持<font color=Red>__成员运算__</font>,测试元素 x 是否为对象 s 的一个成员(元素)。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "False\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(10 in range(1, 20))               # True,range(1,20)生成的数据中包含10\n",
    "print('me' in {'you', 'me', 'he'})      # True,字符串'me'为集合中的一个元素\n",
    "print('she' in {'you', 'me', 'he'})     # False,集合元素中不包含字符串'she'\n",
    "print(2 in [1, 2, 3, 4, 5])             # True,整数2为列表中的一个元素\n",
    "print(6 in (1, 2, 3, 4, 5))             # False,元组元素中不包含整数6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font size=\"4\">注意下面两种情况的区别。</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "print([1, 2] in [[1, 1], [1, 2], [1, 3]])  # True,列表[1, 2]为后面列表中的一个元素\n",
    "print([1, 2] in [1, 2, 3, 4, 5])           # False,后面的列表中所有元素均为整数,并不包含列表1, 2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font size=\"4\">对于字典来说成员运算检测对象是否在<font color=Red>__字典的键__</font>中存在。</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('me' in {'you': 100, 'me': 1000, 'he': 50})  # True\n",
    "print(100 in {'you': 100, 'me': 1000, 'he': 50})   # False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实例2.2 判断字符类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入一个字符,判断这个字符是小写字母还是数字。\n",
    "判定一个字符的类型,可以用成员运算,用in检测目标是否在对应的字符集中存在,若目标在所有字母构成的字符串中存在则该字符是字母,若目标在所有数字构成的字符串中存在,则该字符是数字字符。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "letter = input()             # 输入一个字符\n",
    "ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'  # 所有字母\n",
    "if letter in ascii_letters:  # 成员检测,输入字符在字母字符串中存在时\n",
    "    print('这是字母')         # 输出'这是字母'\n",
    "if letter in '0123456789':   # 成员检测,输入字符在数字字符串中存在时\n",
    "    print('这是数字')         # 输出'这是数字'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font face='楷体' color='red' size=5> 练一练 </font>\n",
    "\n",
    "##### 完成下面的程序,判断输入的字符是否为元音字母(大小写不限)。\n",
    "\n",
    "用户输入一个字符,若该字符为英文元音字母,则输出'这是元音字母';否则输出'这不是元音字母'。\n",
    "\n",
    "**输入输出样例:**\n",
    "\n",
    "    输入:a\n",
    "    输出:这是元音字母\n",
    "\n",
    "    输入:Q\n",
    "    输出:这不是元音字母\n",
    "\n",
    "**提示**:考虑大小写,元音字母包括A、E、I、O、U、a、e、i、o、u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "c = input()\n",
    "# 参考前面的实例,补充代码,判断输入的c是否为元音字母\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "测试输出:\n",
    "A\n",
    "字母A是元音字母\n",
    "i\n",
    "字母i是元音字母\n",
    "z\n",
    "字母z不是元音字母"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}