{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 成员测试"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python 提供了“in” 和“not in”运算符,用于测试某对象是否为字符串、列表或元组等序列或集合中的<font color=Red>__成员__</font>,返回<font color=Red>__布尔值__</font>(True 或False)。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"应用“in”测试时,如果该对象在指定的序列中存在,则返回True,否则返回False。应用“not in”测试时,正好相反。 \n",
"成员测试一般用于<font color=Red>__条件运算__</font>,根据测试结果决定执行后续程序中的某个分支。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"northeast = ['辽宁', '吉林', '黑龙江']\n",
"province = input()\n",
"if province in northeast:\n",
" print(f'{province}是东北三省之一')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"northeast = ['辽宁', '吉林', '黑龙江']\n",
"province = input()\n",
"if province not in northeast:\n",
" print(f'{province}不是东北三省之一')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# 输入身份证号,输出性别\n",
"\n",
"id_number = input() # 测试数据是合法的18位身份证号\n",
"if id_number[16] in '13579': # 若第17位数字在'13579'中存在,为奇数,或if id_number[-2] in '13579':\n",
" gender = '男'\n",
"else:\n",
" gender = '女'\n",
"print(f'性别: {gender}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 实例 6.7 判断火车票座位 "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"我国高铁二等座车座席采用2+3布置,每排设有“3+2”方式排列五个座位,以“A、B、C、D、F”代表,字母“A”和“F”的座位靠窗,字母“C”和“D”靠中间过道,“B”代表三人座中间座席。 \n",
"用户输入一个数字和一个字母组成的合法的座位号,根据字母判断位置是窗口、过道还是中间座席。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"seat = input()\n",
"if seat[-1] in 'AF':\n",
" print(f'{seat}是{seat[:-1]}排{\"窗口\"}')\n",
"elif seat[-1] in 'CD':\n",
" print(f'{seat}是{seat[:-1]}排{\"过道\"}')\n",
"elif seat[-1] == 'B':\n",
" print(f'{seat}是{seat[:-1]}排{\"中间\"}')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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": 2
}