{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "文章汇总:https://www.cnblogs.com/dotnetcrazy/p/9160514.html\n", "\n", "多图旧排版:https://www.cnblogs.com/dunitian/p/9103673.html\n", "\n", "VSCode设置python3的开发环境(linux下默认是python2)https://www.cnblogs.com/dotnetcrazy/p/9095793.html\n", "\n", "欢迎提出更简单的语法~(文章中案例有两个福利哦,一个是养生,一个是人工智能[ 密码:fqif])\n", "\n", "先说下感觉,`python`的编程有点`JavaScript`的感觉(比如:'和“有时候不区别),又感觉像外国版的易语言,整个过程像读书一样,比如一个元素不在列表之中==> `for item not in lists`。使用它做个大点的项目一定要先规定好编程风格,不然能让人崩溃的。先不深究,后面会继续深究。。。(Python2我就不讲了,官方推荐使用`Python3`)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.命名规则\n", "\n", "Python官方是推荐使用`_`来间隔单词,但一般开发人员都是以各自主语言的命名来定义的,这个就各人爱好了,不过团队一定要统一。\n", "\n", "**命名规则**:总的原则就是 **见名知意**,一般都是 **驼峰命名法**,纯Python的话推荐用 **`_`连接单词**\n", "\n", "扩充:`Python`的`关键词`可以自己打印一下:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n" ] } ], "source": [ "import keyword\n", "print(keyword.kwlist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.标识符\n", "标示符由字母、下划线和数字组成,且数字不能开头(这个基本上都一样)注意:**标识符是区分大小写的**\n", "\n", "### 1.2.Python" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "xxx===\n" ] } ], "source": [ "# Python标识符区分大小写的案例\n", "temp=\"xxx\"\n", "tEmp=\"===\"\n", "print(temp+tEmp)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.3.CSharp" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "xxx===\n" ] } ], "source": [ "%%script csharp\n", "//CSharp标识符区分大小写的案例\n", "var temp = \"xxx\";\n", "var tEmp = \"===\";\n", "Console.WriteLine(temp + tEmp);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.注释\n", "\n", "### 2.1.python\n", "\n", "python输出就直接`print`即可,C是`printf`(**不要搞混哦**)\n", "\n", "`#`**注释一行**,三个单引号或者三个双引号 **注释多行**:`'''XXX'''`或者`\"\"\"XXXX\"\"\"`(一般用`#`就够了,有点像`shell`脚本的感觉)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\n" ] } ], "source": [ "#单行注释 输出\n", "print(\"Hello World!\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'三个单引号多行注释:\\nprint(\"Hello World!\")\\nprint(\"Hello World!\")\\nprint(\"Hello World!\")'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'''三个单引号多行注释:\n", "print(\"Hello World!\")\n", "print(\"Hello World!\")\n", "print(\"Hello World!\")'''" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'三个双引号多行注释:\\nprint(\"Hello World!\")\\nprint(\"Hello World!\")\\nprint(\"Hello World!\")'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"三个双引号多行注释:\n", "print(\"Hello World!\")\n", "print(\"Hello World!\")\n", "print(\"Hello World!\")\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.2.CSharp\n", "\n", "C、Java、Net都是`//`注释一行,`/**/`注释多行\n", "```csharp\n", "Console.WriteLine(\"小明同学\");\n", "// Console.WriteLine(\"小明同学\"); 注释一行\n", "/*Console.WriteLine(\"小明同学\");\n", "Console.WriteLine(\"小明同学\"); 注释多行*/\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3.变量\n", "\n", "### 3.1.Python\n", "\n", "python定义变量比较牛逼,**直接写变量名**即可,句子后面 **不用加分号**,eg:`name=\"小明\"`" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "小明\n" ] } ], "source": [ "#定义一个变量并输出\n", "name=\"小明\"\n", "print(name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.2.CSharp\n", "\n", "可以用`var`来进行类型推断,eg:`var name=\"小明\";`" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "123\n" ] } ], "source": [ "%%script csharp\n", "var test = \"123\";//定义一个变量\n", "Console.WriteLine(test);//输出这个变量" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4.输入输出\n", "\n", "### 4.1.Python\n", "\n", "**换行**输出,**不换行**输出:(**\\n**使用这个就不说了,它们和C都是一样的)\n", "\n", "python:`print(\"dnt.dkill.net/now\",end='')` **默认end='\\n' (' 和 \" 随意)**" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dnt.dkill.net/now带你走进中医经络\n" ] } ], "source": [ "print(\"dnt.dkill.net/now\",end='')\n", "print(\"带你走进中医经络\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dnt.dkill.net/now带你走进中医经络\n" ] } ], "source": [ "print(\"dnt.dkill.net/now\",end=\"\")\n", "print(\"带你走进中医经络\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如果字符串内部既包含`'`又包含`\"`怎么办?可以用转义字符`\\`来标识" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm \"OK\"!\n" ] } ], "source": [ "#如果字符串内部既包含'又包含\"怎么办?可以用转义字符\\来标识\n", "print(\"I\\'m \\\"OK\\\"!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`r''`表示`''`内部的字符串默认不转义" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\\\\\\t\\\\\n" ] } ], "source": [ "# 如果字符串里面有很多字符都需要转义,就需要加很多\\,为了简化,Python还允许用r''表示''内部的字符串默认不转义\n", "print(r'\\\\\\t\\\\')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`'''...'''`的格式表示多行内容" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我请你吃饭吧~\n", "晚上吃啥?\n", "去厕所,你说呢?\n" ] } ], "source": [ "#如果字符串内部有很多换行,用\\n写在一行里不好阅读,为了简化,Python允许用'''...'''的格式表示多行内容\n", "print('''我请你吃饭吧~\n", "晚上吃啥?\n", "去厕所,你说呢?''')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "扩:Python提供一种以空格分隔的方式:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I Love You\n" ] } ], "source": [ "print(\"I\",\"Love\",\"You\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "python输出`多个重复字符`,不需要自己手打N个`*`或者`for`循环输出多个重复字符,eg:`print(\"x\"*10)`" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "xxxxxxxxxx\n" ] } ], "source": [ "print(\"x\"*10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如果你不太确定应该用什么,`%s`永远起作用,它会 **把任何数据类型转换为字符串**\n", "\n", "```\n", "%c 字符\n", "%s 通过str() 字符串转换来格式化\n", "%o 八进制整数\n", "%x 十六进制整数(小写字母)\n", "%X 十六进制整数(大写字母)\n", "%e 指数(小写'e')\n", "%E 指数(大写“E”)\n", "%f 浮点实数\n", "%g %f和%e 的简写\n", "%G %f和%E的简写\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "下面来个输入输出的简单的 **案例**吧:打印一张名片,**Name:毒逆天,Gender:男**\n", "\n", "`print(\"Name:%s,Gender:%s\"%(name,gender))`【**注意引号后面没有`,`哦**】" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "请输入用户名:毒逆天\n", "请输入性别:男\n", "Name:毒逆天,Gender:男\n" ] } ], "source": [ "#定义一个变量name,用户输入将赋值给name\n", "name=input(\"请输入用户名:\")\n", "\n", "#定义一个变量gender,用户输入将赋值给gender\n", "gender=input(\"请输入性别:\")\n", "\n", "#多个变量输出\n", "print(\"Name:%s,Gender:%s\"%(name,gender))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.2.CSharp\n", "\n", "输出用:`Console.Write` `Console.WriteLine`" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dnt.dkill.net/now带你走进中医经络\n" ] } ], "source": [ "%%script csharp\n", "Console.Write(\"dnt.dkill.net/now\");\n", "Console.WriteLine(\"带你走进中医经络\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "C#用`@`来转义字符,不管你是转义字符还是换行,**通杀**" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\\\\\\\\\\\\\\\n" ] } ], "source": [ "%%script csharp\n", "Console.WriteLine(@\"\\\\\\\\\\\\\\\");" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我请你吃饭吧~\n", "晚上吃啥?\n", "去厕所,你说呢?\n" ] } ], "source": [ "%%script csharp\n", "Console.WriteLine(@\"我请你吃饭吧~\n", "晚上吃啥?\n", "去厕所,你说呢?\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Csharp输入输出的简单的 **案例**:打印一张名片,`Name:毒逆天,Gender:男`\n", "\n", "C#:`Console.WriteLine($\"Name:{name},Gender:{gender}\");`\n", "```csharp\n", "Console.WriteLine(\"请输入用户名:\");\n", "var name = Console.ReadLine();\n", "\n", "Console.WriteLine(\"请输入性别:\");\n", "var gender = Console.ReadLine();\n", "\n", "Console.WriteLine($\"Name:{name},Gender:{gender}\"); //推荐用法\n", "Console.WriteLine(\"Name:{0},Gender:{1}\", name, gender); //Old 输出\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.类型转换\n", "\n", "### 5.1.Python\n", "\n", "**类型(值)**,eg:`int()`,`long()`,`float()`,`str()`,`list()`,`set()`...等等\n", "\n", "Python没有 **double**类型哦~ \n", "\n", "**扩**:转换成 **16进制**:`hex()`、转换为 **8进制**:`oct()`" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "输入第一个数字1\n", "输入第二个数字2\n", "num1+num2=3\n" ] } ], "source": [ "# 求和\n", "num1=input(\"输入第一个数字\")\n", "num2=input(\"输入第二个数字\")\n", "\n", "print(\"num1+num2=%d\" %(int(num1)+int(num2)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5.2.Csharp\n", "\n", "`C#`:该案例推荐使用 `int.TryParse`,我这边就用常用的`Convert`系列了【**支持类型比较多**】\n", "\n", "`Convert.ToInt64()`,`Convert.ToDouble()`,`Convert.ToString()`...\n", "\n", "```csharp\n", "//类型转换\n", "Console.WriteLine(\"输入第一个数字:\");\n", "var num1 = Console.ReadLine();\n", "Console.WriteLine(\"输入第二个数字:\");\n", "var num2 = Console.ReadLine();\n", "Console.WriteLine($\"num1+num2={Convert.ToInt32(num1) + Convert.ToInt32(num2)}\");\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6.算术运算符\n", "\n", "### 6.1.Python\n", "\n", "算术运算符编程语言基本上差不多,Python多了个 `//` 取商(`%`是取余)和 幂`**`,来个案例:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "num=9,下面结果是对2的除,取余,取商操作:\n", "4.5\n", "1.0\n", "4.0\n", "2^3=8\n" ] } ], "source": [ "num=9\n", "print(\"num=9,下面结果是对2的除,取余,取商操作:\")\n", "print(num/2.0)\n", "print(num%2.0)\n", "print(num//2.0)\n", "print(\"2^3=%d\"%2**3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python3现在这样写也行,`推荐和其他语言写法一致`(**不然你用惯了Python,切换的时候会出事的**)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "num=9,下面结果是对2的除,取余,取商操作:\n", "4.5\n", "1\n", "4\n", "2^3=8\n" ] } ], "source": [ "num=9\n", "print(\"num=9,下面结果是对2的除,取余,取商操作:\")\n", "print(num/2)\n", "print(num%2)\n", "print(num//2)\n", "print(\"2^3=%d\"%2**3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`+=` `-=` `*=` `/=` `%=` `**=` `//=` 这些就不用详说了吧?\n", "\n", "举个例子:`c += a` 等效于 `c = c + a` \n", "\n", "注意下,**Python中不见得等效**,Python都是**引用**,这个先不说**后面说**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6.2.Csharp\n", "\n", "C#常用数学方法都在`Match`类中" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "num=9,下面结果是对2的除,取余,取商操作:\n", "4.5\n", "1\n", "4\n", "8\n" ] } ], "source": [ "%%script csharp\n", "var num=9;\n", "Console.WriteLine(\"num=9,下面结果是对2的除,取余,取商操作:\")\n", "Console.WriteLine(num/2.0);\n", "Console.WriteLine(num%2.0);\n", "Console.WriteLine(num/2);\n", "Console.WriteLine(Math.Pow(2,3));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7.if else\n", "\n", "### 7.1.Python\n", "\n", "说Python像外国版的`易语言`,这边就可以看出来一点了,如果再结合Python命名规则就感觉在阅读文章一样\n", "\n", "先说说Python的`逻辑运算符`:与`and` 或`or` 非`not`,这个倒是跟C、C#、Java等大大不同,和SQL倒是差不多\n", "\n", "关系运算符和其他语言基本上差不多(`==` `!=` `<>` `>` `<` `>=` `<=`)\n", "\n", "就一点不一样:不等于也可以用`<>`,这是兼容SQL的写法吗?\n", "\n", "来个`if else`基础语法:**括号可加可不加**,但是**记得加`:`**。**不用大括号**,但是if里面的代码**注意缩进**" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "成年了\n" ] } ], "source": [ "age=19\n", "\n", "if age>=18:\n", " print(\"成年了\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "再来个**嵌套**的:注意哦~ `else if` 在python里面 **简写**成了:**`elif`**" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "七大姑曰:工作了吗?八大姨问:买房了吗?异性说:结婚了吗?\n" ] } ], "source": [ "age=24\n", "\n", "if age>=23:\n", " print(\"七大姑曰:工作了吗?八大姨问:买房了吗?异性说:结婚了吗?\")\n", "elif age>=18:\n", " print(age)\n", " print(\"成年了哇\")\n", "else:\n", " print(\"好好学习天天向上\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "结合前面知识,再来个案例:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "请输入(1-7)2\n", "星期二\n" ] } ], "source": [ "input_int=int(input(\"请输入(1-7)\"))\n", "\n", "#if后面的:,tab格式,else if 现在是elif\n", "if input_int==1:\n", " print(\"星期一\")\n", "elif input_int==2:\n", " print(\"星期二\")\n", "elif input_int==3:\n", " print(\"星期三\") \n", "elif input_int==4:\n", " print(\"星期四\")\n", "elif input_int==5:\n", " print(\"星期五\")\n", "elif input_int==6:\n", " print(\"星期六\")\n", "elif input_int==7:\n", " print(\"星期日\")\n", "else:\n", " print(\"别闹\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7.2.Csharp\n", "\n", "C# `if else` **单行代码可以不用写括号**\n", "\n", "```csharp\n", "int age = 24;\n", "\n", "if (age >= 23)\n", " Console.WriteLine(\"七大姑曰:工作了吗?八大姨问:买房了吗?异性说:结婚了吗?\");\n", "else if (age >= 18)\n", "{\n", " Console.WriteLine(age);\n", " Console.WriteLine(\"成年了哇\");\n", "}\n", "else\n", " Console.WriteLine(\"好好学习天天向上\");\n", "```\n", "\n", "**NetCore现在推荐,如果是单行,建议`Code`和`if else`写在一行:**\n", "```csharp\n", "int age = 24;\n", "if (age >= 23) Console.WriteLine(\"七大姑曰:工作了吗?八大姨问:买房了吗?异性说:结婚了吗?\");\n", "else if (age >= 18)\n", "{\n", " Console.WriteLine(age);\n", " Console.WriteLine(\"成年了哇\");\n", "}\n", "else Console.WriteLine(\"好好学习天天向上\");\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.While\n", "\n", "### 8.1.Python\n", "\n", "python里面没有`++` 和 `--`,这点的确用的有点小不方便,**扩展部分有相关说明**\n", "\n", "**while循环一般通过数值是否满足来确定循环的条件**\n", "\n", "来几个个案例(PS:感觉用C了,捂脸^_^)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "9\n", "8\n", "7\n", "6\n", "5\n", "4\n", "3\n", "2\n", "1\n" ] } ], "source": [ "num=10\n", "\n", "while num>0: \n", " print(num)\n", " num-=1" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "*\n", "**\n", "***\n", "****\n", "*****\n" ] } ], "source": [ "i=1\n", "#输出一个三角形\n", "while i<6:\n", " j=1\n", " while j<=i:\n", " print(\"*\",end=\"\")#不换行输出\n", " j+=1\n", " print(\"\")#下一行\n", " i+=1" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5050\n" ] } ], "source": [ "# 1~100求和\n", "i=1\n", "sum=0\n", "while i<=100:\n", " sum+=i\n", " i+=1\n", "print(sum)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 8.2.Csharp\n", "\n", "用法差不多,来个案例" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5050\n" ] } ], "source": [ "%%script csharp\n", "int index = 1;\n", "int sum = 0;\n", "while (index <= 100)\n", "{\n", " sum += index;\n", " index++;\n", "}\n", "Console.WriteLine(sum);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 8.3.Python扩展(++ --)\n", "\n", "其实Python分为 **可变类型**(`list`,`dict`,`set`等等)和 **不可变类型**(`int`,`str`,`tuple`等等)\n", "\n", "像数字这类的是不可变类型(后面会继续说)所以结果往往和你预期的不一样哦~看个案例:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "93994553510560\n", "93994553510560\n", "True\n", "93994553510592\n", "93994553510560\n", "False\n" ] } ], "source": [ "# python 中,变量是以内容为基准而不是像 c 中以变量名为基准,所以只要你的数字内容是5\n", "# 不管你起什么名字,这个变量的 ID 是相同的,同时也就说明了 python 中一个变量可以以多个名称访问\n", "a=5\n", "b=5\n", "\n", "print(id(a))\n", "print(id(b))\n", "print(a is b)\n", "a+=1\n", "\n", "print(id(a))\n", "print(id(b))\n", "print(a is b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "你还可以看看这篇文章:**Python没有 ++/--** 参考文章(点我)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9.for\n", "\n", "### 9.1.Python\n", "\n", "`python`的`for`循环,类似于`js`里面的`for in`\n", "\n", "当然了,python的`for`还有很多诸如`列表生成式`的便捷功能,基础部分先不说\n", "\n", "看个基础案例:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "# 基础循环:后面会讲range\n", "for i in range(5):\n", " print(i)\n", " i+=1" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "https://pan.baidu.com/s/1weaF2DGsgDzAcniRzNqfyQ\n", " end ...\n" ] } ], "source": [ "#while循环一般通过数值是否满足来确定循环的条件\n", "#for循环一般是对能保存多个数据的变量,进行遍历\n", "name=\"https://pan.baidu.com/s/1weaF2DGsgDzAcniRzNqfyQ#mmd\"\n", "\n", "for i in name:\n", " if i=='#':\n", " break\n", " print(i,end='')#另一种写法:print(\"%s\"%i,end=\"\")\n", "print('\\n end ...')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 9.2.Csharp\n", "\n", "`foreach (var i in name)`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "https://pan.baidu.com/s/1weaF2DGsgDzAcniRzNqfyQ\n", " end ...\n" ] } ], "source": [ "%%script csharp\n", "var url = \"https://pan.baidu.com/s/1weaF2DGsgDzAcniRzNqfyQ#mmd\";\n", "foreach (var item in url)\n", "{\n", " if (item == '#')\n", " break;\n", " Console.Write(item);\n", "}\n", "Console.WriteLine(\"\\n end ...\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 9.3.Python扩展(for else)\n", "\n", "for的扩展:`for else`(一般不太用)\n", "\n", "**官方参考**:https://docs.python.org/3/reference/compound_stmts.html#the-for-statement\n", "\n", "![for-else图片](https://images2018.cnblogs.com/blog/1127869/201805/1127869-20180531172814702-1628372100.png)\n", "\n", "图片出处:https://www.cnblogs.com/dspace/p/6622799.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 其他扩展\n", "\n", "### 1.Python 没有 switch / case 语句\n", "\n", "Python 没有`switch / case`语句。为了实现它,我们可以使用`字典映射`:\n", "\n", "官方的解释说:https://docs.python.org/3.6/faq/design.html#why-isn-t-there-a-switch-or-case-statement-in-python\n", "\n", "\"用`if... elif... elif... else`序列很容易来实现`switch / case`语句,而且可以使用函数字典映射和类的调度方法\"\n", "\n", "```py\n", "def numbers_to_strings(argument):\n", "switcher = {\n", " 0: \"zero\",\n", " 1: \"one\",\n", " 2: \"two\",\n", "}\n", "return switcher.get(argument, \"nothing\")\n", "```\n", "这段代码类似于:\n", "```\n", "function(argument){\n", " switch(argument) {\n", " case 0:\n", " return \"zero\";\n", " case 1:\n", " return \"one\";\n", " case 2:\n", " return \"two\";\n", " default:\n", " return \"nothing\";\n", " };\n", "};\n", "```\n", "**在Python中字典映射也可以包含函数或者 lambda 表达式:**\n", "```py\n", "def zero():\n", " return \"zero\"\n", " \n", "def one():\n", " return \"one\"\n", " \n", "def numbers_to_functions_to_strings(argument):\n", " switcher = {\n", " 0: zero,\n", " 1: one,\n", " 2: lambda: \"two\",\n", " }\n", " func = switcher.get(argument, lambda: \"nothing\")\n", " return func()\n", "```\n", "**类的调度方法**:\n", "\n", "如果在一个类中,不确定要使用哪种方法,可以用一个调度方法在运行的时候来确定\n", "```py\n", "class Switcher(object):\n", " def numbers_to_methods_to_strings(self, argument):\n", " method_name = 'number_' + str(argument)\n", " method = getattr(self, method_name, lambda: \"nothing\")\n", " return method()\n", " \n", " def number_0(self):\n", " return \"zero\"\n", " \n", " def number_1(self):\n", " return \"one\"\n", " \n", " def number_2(self):\n", " return \"two\"\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Python设计相关的为什么,可以参考官方文档**:https://docs.python.org/3.6/faq/design.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.Csharp基础笔记\n", "\n", "C#基础(逆天上学那会做的笔记)\n", "\n", "易忘知识点\n", "\n", "C#基础汇总\n", "\n", "异常概况系\n", "\n", "**其实有了Code,笔记也就没啥用了,知识点直接Code验证一下即可**\n", "\n", "CodeBase ~ POP\n", "\n", "CodeBase ~ OOP" ] }, { "cell_type": "markdown", "metadata": {}, "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }