{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'三个单引号多行注释:\\nprint(\"Hello World!\")\\nprint(\"Hello World!\")\\nprint(\"Hello World!\")'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'''三个单引号多行注释:\n", "print(\"Hello World!\")\n", "print(\"Hello World!\")\n", "print(\"Hello World!\")'''" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'三个双引号多行注释:\\nprint(\"Hello World!\")\\nprint(\"Hello World!\")\\nprint(\"Hello World!\")'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"三个双引号多行注释:\n", "print(\"Hello World!\")\n", "print(\"Hello World!\")\n", "print(\"Hello World!\")\"\"\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\n" ] } ], "source": [ "#单行注释 输出\n", "print(\"Hello World!\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "小明\n" ] } ], "source": [ "#定义一个变量并输出\n", "name=\"小明\"\n", "print(name)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "xxxxxxxxxx\n" ] } ], "source": [ "print(\"x\"*10)" ] }, { "cell_type": "code", "execution_count": 6, "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": 7, "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": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm \"OK\"!\n" ] } ], "source": [ "#如果字符串内部既包含'又包含\"怎么办?可以用转义字符\\来标识\n", "print(\"I\\'m \\\"OK\\\"!\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\\\\\\t\\\\\n" ] } ], "source": [ "# 如果字符串里面有很多字符都需要转义,就需要加很多\\,为了简化,Python还允许用r''表示''内部的字符串默认不转义\n", "print(r'\\\\\\t\\\\')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我请你吃饭吧~\n", "晚上吃啥?\n", "去厕所,你说呢?\n" ] } ], "source": [ "#如果字符串内部有很多换行,用\\n写在一行里不好阅读,为了简化,Python允许用'''...'''的格式表示多行内容\n", "print('''我请你吃饭吧~\n", "晚上吃啥?\n", "去厕所,你说呢?''')" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "################## input用户输入 #############################" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "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": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "xxx===\n" ] } ], "source": [ "# 标识符是区分大小写的\n", "temp=\"xxx\"\n", "tEmp=\"===\"\n", "print(temp+tEmp)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "输入第一个数字10\n", "输入第二个数字2\n", "num1+num2=12\n" ] } ], "source": [ "# 求和\n", "num1=input(\"输入第一个数字\")\n", "num2=input(\"输入第二个数字\")\n", "\n", "print(\"num1+num2=%d\" %(int(num1)+int(num2)))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "################## ifelse #############################" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "成年了\n" ] } ], "source": [ "age=19\n", "\n", "if age>=18:\n", " print(\"成年了\")" ] }, { "cell_type": "code", "execution_count": 7, "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": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "请输入(1-7)6\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": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "################## while #############################" ] }, { "cell_type": "code", "execution_count": 10, "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": 11, "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": 12, "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": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "94879583644320\n", "94879583644320\n", "True\n", "94879583644352\n", "94879583644320\n", "False\n" ] } ], "source": [ "# why not have ++ ?\n", "# 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": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "################## for #############################" ] }, { "cell_type": "code", "execution_count": 14, "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": "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 }