{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "test_str=\"www.baidu.com\"\n", "test_list=[1,\"d\",5]\n", "test_list1=[2,4,\"n\",\"t\",3]\n", "test_dict={\"name\":\"dnt\",\"wechat\":\"xxx\"}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "##################### 运算符扩展 #####################" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "www.baidu.comwww.baidu.com\n", "[1, 'd', 5, 2, 4, 'n', 't', 3]\n" ] } ], "source": [ "# + 合并 (不支持字典)\n", "print(test_str+test_str)\n", "print(test_list+test_list1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "www.baidu.comwww.baidu.com\n", "[1, 'd', 5, 1, 'd', 5]\n" ] } ], "source": [ "# * 复制 (不支持字典)\n", "print(test_str*2)\n", "print(test_list*2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n", "False\n", "True\n" ] } ], "source": [ "# in 是否存在(字典是查key)\n", "print(\"d\" in test_str) #True\n", "print(\"d\" in test_list) #True\n", "print(\"d\" in test_dict) #False\n", "print(\"name\" in test_dict) #True" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n", "True\n", "False\n" ] } ], "source": [ "# not in 是否不存在(字典是查key)\n", "print(\"z\" not in test_str) #True\n", "print(\"z\" not in test_list) #True\n", "print(\"z\" not in test_dict) #True\n", "print(\"name\" not in test_dict) #False" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "################# 内置函数扩展:(len,max,min,del) #################" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "13\n", "3\n", "2\n" ] } ], "source": [ "# len(item) 计算容器中元素个数\n", "print(len(test_str))\n", "print(len(test_list))\n", "print(len(test_dict))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'w'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# max(item) 返回容器中元素最大值\n", "max(test_str)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'>' not supported between instances of 'str' and 'int'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# 这个注意一种情况(当然了,你按照之前说的规范,list里面放同一种类型就不会出错了)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtest_list\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#TypeError: '>' not supported between instances of 'str' and 'int'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: '>' not supported between instances of 'str' and 'int'" ] } ], "source": [ "# 这个注意一种情况(当然了,你按照之前说的规范,list里面放同一种类型就不会出错了)\n", "max(test_list) #TypeError: '>' not supported between instances of 'str' and 'int'" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n", "wechat\n" ] } ], "source": [ "test_list=[1,3,5,7,9,2]\n", "print(max(test_list))\n", "print(max(test_dict)) #比较key" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ".\n", "1\n", "name\n" ] } ], "source": [ "# min(item) 返回容器中元素最小值\n", "print(min(test_str))\n", "print(min(test_list))\n", "print(min(test_dict))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# del(item)\t 删除变量\n", "# del() or del xxx" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 可以忽略 cmp(item1, item2) 比较两个值\n", "# Python2里面有 cmp(1,2) ==> -1 \n", "# cmp在比较字典数据时,先比较键,再比较值" ] } ], "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }