[{"data":1,"prerenderedAt":556},["ShallowReactive",2],{"post-\u002Fposts\u002Funiversity-python-notes":3,"post-nav-\u002Fposts\u002Funiversity-python-notes":550},{"id":4,"title":5,"body":6,"date":537,"description":538,"draft":539,"extension":540,"meta":541,"navigation":93,"path":542,"seo":543,"stem":544,"summary":545,"tags":546,"__hash__":549},"posts\u002Fposts\u002Funiversity-python-notes.md","开学前的那几页 Python 笔记",{"type":7,"value":8,"toc":531},"minimark",[9,16,20,64,68,71,187,190,424,428,478,487,492,495,527],[10,11,12],"blockquote",{},[13,14,15],"p",{},"这篇是 2019 年 8 月记的，上大学前的那个暑假。原文照抄，只调了排版，当时写错的地方一处都没改。",[17,18,19],"h2",{"id":19},"语法零碎",[21,22,23,27,30,33,39,42,45,48,51,58,61],"ul",{},[24,25,26],"li",{},"（）不可变",[24,28,29],{},"【】可变",[24,31,32],{},"｛｝可变，无序",[24,34,35,36],{},"形式常亮用大写！CONSTANT = ",[37,38],"span",{},[24,40,41],{},"模块变量按规则都应该是常亮，变量放函数或者类里头",[24,43,44],{},"Pycharm好像没有模块常量波浪线标注建议大写功能？",[24,46,47],{},"Range(0,10) 表示1-9",[24,49,50],{},"Print(x,end=’|’)打印完成不换行，以|分隔",[24,52,53,54,57],{},"B = a",[37,55,56],{},"0:len(a):2"," b数组获取a 相当于range(0,len(a),2)",[24,59,60],{},"Break终止循环 contiune跳过这一轮循环",[24,62,63],{},"Len(a) a的字串长度",[17,65,67],{"id":66},"random-模块","random 模块",[13,69,70],{},"用法是从别处抄来的，第一段的作者署名我留在了代码里：",[72,73,78],"pre",{"className":74,"code":75,"language":76,"meta":77,"style":77},"language-python shiki shiki-themes github-light github-dark","#!\u002Fusr\u002Fbin\u002Fpython\n\n## -*- coding: UTF-8 -*-\n\nimport random\n\nprint( random.randint(1,10) )        # 产生 1 到 10 的一个整数型随机数\nprint( random.random() )             # 产生 0 到 1 之间的随机浮点数\nprint( random.uniform(1.1,5.4) )     # 产生  1.1 到 5.4 之间的随机浮点数，区间可以不是整数\nprint( random.choice('tomorrow') )   # 从序列中随机选取一个元素\nprint( random.randrange(1,100,2) )   # 生成从1到100的间隔为2的随机整数\n\na=[1,3,5,6,7]                # 将序列a中的元素顺序打乱\nrandom.shuffle(a)\nprint(a)\n\n# 冷面做的梦\n# 1年前 (2018-05-23)\n","python","",[79,80,81,88,95,101,106,112,117,123,129,135,141,147,152,158,164,170,175,181],"code",{"__ignoreMap":77},[37,82,85],{"class":83,"line":84},"line",1,[37,86,87],{},"#!\u002Fusr\u002Fbin\u002Fpython\n",[37,89,91],{"class":83,"line":90},2,[37,92,94],{"emptyLinePlaceholder":93},true,"\n",[37,96,98],{"class":83,"line":97},3,[37,99,100],{},"## -*- coding: UTF-8 -*-\n",[37,102,104],{"class":83,"line":103},4,[37,105,94],{"emptyLinePlaceholder":93},[37,107,109],{"class":83,"line":108},5,[37,110,111],{},"import random\n",[37,113,115],{"class":83,"line":114},6,[37,116,94],{"emptyLinePlaceholder":93},[37,118,120],{"class":83,"line":119},7,[37,121,122],{},"print( random.randint(1,10) )        # 产生 1 到 10 的一个整数型随机数\n",[37,124,126],{"class":83,"line":125},8,[37,127,128],{},"print( random.random() )             # 产生 0 到 1 之间的随机浮点数\n",[37,130,132],{"class":83,"line":131},9,[37,133,134],{},"print( random.uniform(1.1,5.4) )     # 产生  1.1 到 5.4 之间的随机浮点数，区间可以不是整数\n",[37,136,138],{"class":83,"line":137},10,[37,139,140],{},"print( random.choice('tomorrow') )   # 从序列中随机选取一个元素\n",[37,142,144],{"class":83,"line":143},11,[37,145,146],{},"print( random.randrange(1,100,2) )   # 生成从1到100的间隔为2的随机整数\n",[37,148,150],{"class":83,"line":149},12,[37,151,94],{"emptyLinePlaceholder":93},[37,153,155],{"class":83,"line":154},13,[37,156,157],{},"a=[1,3,5,6,7]                # 将序列a中的元素顺序打乱\n",[37,159,161],{"class":83,"line":160},14,[37,162,163],{},"random.shuffle(a)\n",[37,165,167],{"class":83,"line":166},15,[37,168,169],{},"print(a)\n",[37,171,173],{"class":83,"line":172},16,[37,174,94],{"emptyLinePlaceholder":93},[37,176,178],{"class":83,"line":177},17,[37,179,180],{},"# 冷面做的梦\n",[37,182,184],{"class":83,"line":183},18,[37,185,186],{},"# 1年前 (2018-05-23)\n",[13,188,189],{},"第二段：",[72,191,193],{"className":74,"code":192,"language":76,"meta":77,"style":77},"#!\u002Fusr\u002Fbin\u002Fpython\n\n## -*- coding: UTF-8 -*-\n\nimport random\nimport string\n\n#### 随机整数：\n\nprint random.randint(1,50)\n\n#### 随机选取0到100间的偶数：\n\nprint random.randrange(0, 101, 2)\n\n#### 随机浮点数：\n\nprint random.random()\nprint random.uniform(1, 10)\n\n#### 随机字符：\n\nprint random.choice('abcdefghijklmnopqrstuvwxyz!@#$%^&*()')\n\n#### 多个字符中生成指定数量的随机字符：\n\nprint random.sample('zyxwvutsrqponmlkjihgfedcba',5)\n\n#### 从a-zA-Z0-9生成指定数量的随机字符：\n\nran_str = ''.join(random.sample(string.ascii_letters + string.digits, 8))\nprint ran_str\n\n#### 多个字符中选取指定数量的字符组成新字符串：\n\nprint ''.join(random.sample(['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'], 5))\n\n#### 随机选取字符串：\n\nprint random.choice(['剪刀', '石头', '布'])\n\n#### 打乱排序\n\nitems = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]\nprint random.shuffle(items)\n",[79,194,195,199,203,207,211,215,220,224,229,233,238,242,247,251,256,260,265,269,274,280,285,291,296,302,307,313,318,324,329,335,340,346,352,357,363,368,374,379,385,390,396,401,407,412,418],{"__ignoreMap":77},[37,196,197],{"class":83,"line":84},[37,198,87],{},[37,200,201],{"class":83,"line":90},[37,202,94],{"emptyLinePlaceholder":93},[37,204,205],{"class":83,"line":97},[37,206,100],{},[37,208,209],{"class":83,"line":103},[37,210,94],{"emptyLinePlaceholder":93},[37,212,213],{"class":83,"line":108},[37,214,111],{},[37,216,217],{"class":83,"line":114},[37,218,219],{},"import string\n",[37,221,222],{"class":83,"line":119},[37,223,94],{"emptyLinePlaceholder":93},[37,225,226],{"class":83,"line":125},[37,227,228],{},"#### 随机整数：\n",[37,230,231],{"class":83,"line":131},[37,232,94],{"emptyLinePlaceholder":93},[37,234,235],{"class":83,"line":137},[37,236,237],{},"print random.randint(1,50)\n",[37,239,240],{"class":83,"line":143},[37,241,94],{"emptyLinePlaceholder":93},[37,243,244],{"class":83,"line":149},[37,245,246],{},"#### 随机选取0到100间的偶数：\n",[37,248,249],{"class":83,"line":154},[37,250,94],{"emptyLinePlaceholder":93},[37,252,253],{"class":83,"line":160},[37,254,255],{},"print random.randrange(0, 101, 2)\n",[37,257,258],{"class":83,"line":166},[37,259,94],{"emptyLinePlaceholder":93},[37,261,262],{"class":83,"line":172},[37,263,264],{},"#### 随机浮点数：\n",[37,266,267],{"class":83,"line":177},[37,268,94],{"emptyLinePlaceholder":93},[37,270,271],{"class":83,"line":183},[37,272,273],{},"print random.random()\n",[37,275,277],{"class":83,"line":276},19,[37,278,279],{},"print random.uniform(1, 10)\n",[37,281,283],{"class":83,"line":282},20,[37,284,94],{"emptyLinePlaceholder":93},[37,286,288],{"class":83,"line":287},21,[37,289,290],{},"#### 随机字符：\n",[37,292,294],{"class":83,"line":293},22,[37,295,94],{"emptyLinePlaceholder":93},[37,297,299],{"class":83,"line":298},23,[37,300,301],{},"print random.choice('abcdefghijklmnopqrstuvwxyz!@#$%^&*()')\n",[37,303,305],{"class":83,"line":304},24,[37,306,94],{"emptyLinePlaceholder":93},[37,308,310],{"class":83,"line":309},25,[37,311,312],{},"#### 多个字符中生成指定数量的随机字符：\n",[37,314,316],{"class":83,"line":315},26,[37,317,94],{"emptyLinePlaceholder":93},[37,319,321],{"class":83,"line":320},27,[37,322,323],{},"print random.sample('zyxwvutsrqponmlkjihgfedcba',5)\n",[37,325,327],{"class":83,"line":326},28,[37,328,94],{"emptyLinePlaceholder":93},[37,330,332],{"class":83,"line":331},29,[37,333,334],{},"#### 从a-zA-Z0-9生成指定数量的随机字符：\n",[37,336,338],{"class":83,"line":337},30,[37,339,94],{"emptyLinePlaceholder":93},[37,341,343],{"class":83,"line":342},31,[37,344,345],{},"ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 8))\n",[37,347,349],{"class":83,"line":348},32,[37,350,351],{},"print ran_str\n",[37,353,355],{"class":83,"line":354},33,[37,356,94],{"emptyLinePlaceholder":93},[37,358,360],{"class":83,"line":359},34,[37,361,362],{},"#### 多个字符中选取指定数量的字符组成新字符串：\n",[37,364,366],{"class":83,"line":365},35,[37,367,94],{"emptyLinePlaceholder":93},[37,369,371],{"class":83,"line":370},36,[37,372,373],{},"print ''.join(random.sample(['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'], 5))\n",[37,375,377],{"class":83,"line":376},37,[37,378,94],{"emptyLinePlaceholder":93},[37,380,382],{"class":83,"line":381},38,[37,383,384],{},"#### 随机选取字符串：\n",[37,386,388],{"class":83,"line":387},39,[37,389,94],{"emptyLinePlaceholder":93},[37,391,393],{"class":83,"line":392},40,[37,394,395],{},"print random.choice(['剪刀', '石头', '布'])\n",[37,397,399],{"class":83,"line":398},41,[37,400,94],{"emptyLinePlaceholder":93},[37,402,404],{"class":83,"line":403},42,[37,405,406],{},"#### 打乱排序\n",[37,408,410],{"class":83,"line":409},43,[37,411,94],{"emptyLinePlaceholder":93},[37,413,415],{"class":83,"line":414},44,[37,416,417],{},"items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]\n",[37,419,421],{"class":83,"line":420},45,[37,422,423],{},"print random.shuffle(items)\n",[17,425,427],{"id":426},"模块导入和别的","模块、导入和别的",[21,429,430,433,436,439,442,445,448,451,454,457,460,463,466,469,472,475],{},[24,431,432],{},".dll是C#里类似模块的 组织结构 动态链接库 .lib静态链接库",[24,434,435],{},".jar也是",[24,437,438],{},"Pack._init_这个写法是错的，_init_直接用pack表达",[24,440,441],{},"Import c7",[24,443,444],{},"Print(c7.a)这样能把c7模块的a打印出来",[24,446,447],{},"解释型语言在意import print顺序",[24,449,450],{},"同级模块不用目录直接名字就好，高级导入次级需要 子报名.模块名",[24,452,453],{},".pyc是编译器做的自检码，提升运行效率，可以被反编译，谨防小偷！",[24,455,456],{},"Round(a,b) a数字b位四舍五入",[24,458,459],{},"Python命令行help()查询函数",[24,461,462],{},"Sys.函数能改很多人工限制",[24,464,465],{},"循环调用最多3923 3925次",[24,467,468],{},"定义立刻执行，声明等着以后执行",[24,470,471],{},"_不推荐外部调用；__防止被继承覆盖",[24,473,474],{},"__dict__是类内变量",[24,476,477],{},"a = list(range(1,5)) list()只接受单个参数，结果：",[72,479,481],{"className":74,"code":480,"language":76,"meta":77,"style":77},"a = [1, 2, 3, 4]\n",[79,482,483],{"__ignoreMap":77},[37,484,485],{"class":83,"line":84},[37,486,480],{},[21,488,489],{},[24,490,491],{},"序列解包不是并行过程",[17,493,494],{"id":494},"现在回头看",[21,496,497,503,517,524],{},[24,498,499,502],{},[79,500,501],{},"range(0, 10)"," 是 0 到 9。我那时候写「表示 1-9」，两头都记错了。",[24,504,505,508,509,512,513,516],{},[79,506,507],{},"_init_"," 少了下划线，是 ",[79,510,511],{},"__init__","；",[79,514,515],{},"pack"," 那句到现在我也没还原出当时想说什么。",[24,518,519,520,523],{},"第二段 random 的代码是 Python 2 的（",[79,521,522],{},"print"," 不带括号），我照着抄下来，没看出来它和第一段的区别。",[24,525,526],{},"「循环调用最多 3923 3925 次」这个数，我至今不知道是从哪儿抄的。",[528,529,530],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":77,"searchDepth":97,"depth":97,"links":532},[533,534,535,536],{"id":19,"depth":90,"text":19},{"id":66,"depth":90,"text":67},{"id":426,"depth":90,"text":427},{"id":494,"depth":90,"text":494},"2019-08-15","2019 年 8 月，上大学前的暑假自学 Python 记的几页：列表字典、range 的边界、两段抄来的 random 用法，还有一串当时没弄明白的语法。",false,"md",{},"\u002Fposts\u002Funiversity-python-notes",{"title":5,"description":538},"posts\u002Funiversity-python-notes","大学开学前自学 Python 的原始笔记，写错的、想岔的都原样留着。",[547,548],"Python","大学旧笔记","2RMQgL_P1ZC-Z9iyl7X5mRdzSo_DoqecOWGExN51P_c",{"prev":551,"next":555},{"path":552,"title":553,"date":554},"\u002Fposts\u002Funiversity-excel-vba-macro","大学时写的第一段宏：比对 Excel 的两列","2020-06-13",null,1792556025030]