{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2beecc31",
   "metadata": {},
   "source": [
    "## 01 两点距离"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dd511359",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p1 = (0, 4), p2 = (3, 0)\n",
      "两点距离 = 5.0\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "p1 = (0, 4)\n",
    "p2 = (3, 0)\n",
    "\n",
    "distance = math.dist(p1, p2)\n",
    "print(f'p1 = {p1}, p2 = {p2}')\n",
    "print(f'两点距离 = {distance}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "996ada10",
   "metadata": {},
   "source": [
    "## 02 元组去重并按从大到小排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ab0209d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原元组: (68, 87, 59, 92, 92, 76, 87, 59, 89, 76)\n",
      "结果元组: (92, 89, 87, 76, 68, 59)\n"
     ]
    }
   ],
   "source": [
    "tp = (68, 87, 59, 92, 92, 76, 87, 59, 89, 76)\n",
    "result_tp = tuple(sorted(set(tp), reverse=True))\n",
    "print('原元组:', tp)\n",
    "print('结果元组:', result_tp)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ed1c158",
   "metadata": {},
   "source": [
    "## 03 两列表集合运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "98fbca04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lst1: [1, 2, 3, 5, 6, 3, 2]\n",
      "lst2: [2, 5, 7, 8, 9]\n",
      "同时存在于lst1和lst2中的数: {2, 5}\n",
      "在lst1中但不在lst2中的数: {1, 3, 6}\n",
      "总共不同数字的个数: 8\n"
     ]
    }
   ],
   "source": [
    "lst1 = [1, 2, 3, 5, 6, 3, 2]\n",
    "lst2 = [2, 5, 7, 8, 9]\n",
    "\n",
    "set1, set2 = set(lst1), set(lst2)\n",
    "common = set1 & set2\n",
    "only_in_lst1 = set1 - set2\n",
    "total_distinct = len(set1 | set2)\n",
    "\n",
    "print('lst1:', lst1)\n",
    "print('lst2:', lst2)\n",
    "print('同时存在于lst1和lst2中的数:', common)\n",
    "print('在lst1中但不在lst2中的数:', only_in_lst1)\n",
    "print('总共不同数字的个数:', total_distinct)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27d646e4",
   "metadata": {},
   "source": [
    "## 04 用字典展示购物过程（4个阶段）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bcd07846",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "阶段1: {'牙膏': 3}\n",
      "阶段2: {'牙膏': 3, '面包': 4}\n",
      "阶段3: {'牙膏': 3, '面包': 4, '矿泉水': 5}\n",
      "阶段4: {'牙膏': 3, '面包': 4, '矿泉水': 3}\n"
     ]
    }
   ],
   "source": [
    "cart = {}\n",
    "\n",
    "cart['牙膏'] = 3\n",
    "print('阶段1:', cart)\n",
    "\n",
    "cart['面包'] = 4\n",
    "print('阶段2:', cart)\n",
    "\n",
    "cart['矿泉水'] = 5\n",
    "print('阶段3:', cart)\n",
    "\n",
    "cart['矿泉水'] -= 2\n",
    "print('阶段4:', cart)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "021a1689",
   "metadata": {},
   "source": [
    "## 05 字典长度、修改值、求总成绩"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "70b55e1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原字典: {'python': 95, 'math': 99, 'english': 100, 'chinese': 77}\n",
      "字典长度: 4\n",
      "修改后字典: {'python': 95, 'math': 98, 'english': 100, 'chinese': 77}\n",
      "所有科目总成绩: 370\n"
     ]
    }
   ],
   "source": [
    "dic = {'python': 95, 'math': 99, 'english': 100, 'chinese': 77}\n",
    "\n",
    "print('原字典:', dic)\n",
    "print('字典长度:', len(dic))\n",
    "\n",
    "dic['math'] = 98\n",
    "print('修改后字典:', dic)\n",
    "\n",
    "total_score = sum(dic.values())\n",
    "print('所有科目总成绩:', total_score)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dca15cd1",
   "metadata": {},
   "source": [
    "## 06 字符串 split 后转列表、元组、集合、字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "451e265f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表: ['张三', '李四', '张三', '赵六']\n",
      "元组: ('张三', '李四', '张三', '赵六')\n",
      "集合: {'张三', '李四', '赵六'}\n",
      "字典(姓名:出现次数): {'张三': 2, '李四': 1, '赵六': 1}\n"
     ]
    }
   ],
   "source": [
    "str1 = '张三,李四,张三,赵六'\n",
    "\n",
    "name_list = str1.split(',')\n",
    "name_tuple = tuple(name_list)\n",
    "name_set = set(name_list)\n",
    "\n",
    "name_dict = {}\n",
    "for n in name_list:\n",
    "    name_dict[n] = name_dict.get(n, 0) + 1\n",
    "\n",
    "print('列表:', name_list)\n",
    "print('元组:', name_tuple)\n",
    "print('集合:', name_set)\n",
    "print('字典(姓名:出现次数):', name_dict)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
