结构化工程 https:pythonguidecn。readthedocs。iozhlatestwritingstructure。html readme:对项目的整体介绍,同时也是一份使用手册LICENS:阐述该项目的许可说明和授权setup。py:通过setup把核心代码打包apitest:存放项目的核心代码requirements。txt:项目依赖的第三方库docs:包的参考文档tests:代码的测试makefile:同于项目的管理,根据项目需求添加其他的文件和目录 编码规范 python之禅 importthis TheZenofPython,byTimPeters Beautifulisbetterthanugly。 Explicitisbetterthanimplicit。 Simpleisbetterthancomplex。 Complexisbetterthancomplicated。 Flatisbetterthannested。 Sparseisbetterthandense。 Readabilitycounts。 Specialcasesarentspecialenoughtobreaktherules。 Althoughpracticalitybeatspurity。 Errorsshouldneverpasssilently。 Unlessexplicitlysilenced。 Inthefaceofambiguity,refusethetemptationtoguess。 Thereshouldbeoneandpreferablyonlyoneobviouswaytodoit。 AlthoughthatwaymaynotbeobviousatfirstunlessyoureDutch。 Nowisbetterthannever。 Althoughneverisoftenbetterthanrightnow。 Iftheimplementationishardtoexplain,itsabadidea。 Iftheimplementationiseasytoexplain,itmaybeagoodidea。 Namespacesareonehonkinggreatidealetsdomoreofthose! 缩进 每一级缩进使用4个空格 左对齐foolongfunctionname(varone,vartwo,varthree,varfour)换行缩进foolongfunctionname(varone,vartwo,varthree,varfour)用更多的缩进来与其他行区分foolongfunctionname(varone,vartwo,varthree,varfour) 行的最大长度 最大79注释最大72 空行 函数之间2个空行类的方法之间1个空行 注释 块注释 与代码在同一行行内注释文档注释 第一行:对函数类整体功能说明 参数说明 :paramvarone:参数1是干嘛的 :typevarone:int 返回值说明 :return: PEP257:https:github。comqiuxiangpepblobmasterpeps257。md 模块包导入 先导入python内置模块和包导入第三方的模块和包导入自定义的模块和包 all〔〕使用导入时候只会导入all的〔〕中的的内容数据类型扩展 基本数据类型 数值 整数浮点数布尔类型decimal复数 序列类型 字符串元祖列表 元祖在定义的时候就确认了存放的空间,内存固定,占用空间较小 列表的空间可以动态变化,内存占用的空间比元祖大的多 在数据固定的情况下使用元祖 散列 字典集合 命名元祖 fromcollectionsimportnamedtuple 普通元祖: tu(zx,1994,男)print(tu〔0〕) 命名元祖: 使用namedtuple相当于创建了一个类,s1相当于实例话了一个包含name,age,gender三个属性的类。 main:当前模块下 fromcollectionsimportnamedtuplestudentnamedtuple(Student,〔name,age,gender〕)print(type(student))classtype一个类s1student(zx,1994,男)print(type(s1))classmain。Studentprint(s1。name)zxprint(s1〔0〕)zx 推导式 列表推导式 普通列表创建 l〔1,2,3,4〕urls〔〕foriinrange(0,101,10):urls。append(fhttp:。。。。?page{i})print(urls)〔http:。。。。?page0,http:。。。。?page10,http:。。。。?page20,http:。。。。?page30,http:。。。。?page40,http:。。。。?page50,http:。。。。?page60,http:。。。。?page70,http:。。。。?page80,http:。。。。?page90,http:。。。。?page100〕 列表推导式 urls〔fhttp:。。。。?page{i}foriinrange(0,101,10)〕print(urls)〔http:。。。。?page0,http:。。。。?page10,http:。。。。?page20,http:。。。。?page30,http:。。。。?page40,http:。。。。?page50,http:。。。。?page60,http:。。。。?page70,http:。。。。?page80,http:。。。。?page90,http:。。。。?page100〕 三目运算 a10ifa5:print(5)else:print(0) a4res5ifa5else0(判断语句成立)if(判断语句)else(判断语句不成立)print(res)0res5ifa5else(ifa5else!)print(res)! 结合列表推导式 〔iforiinrange(10)ifi20〕〔0,2,4,6,8〕〔iifi20else1foriinrange(10)〕〔0,1,2,1,4,1,6,1,8,1〕 字典推导式 dic{iforiin〔1,2,3〕}集合{1,2,3}dic{i:1foriin〔1,2,3〕}字典{1:1,2:1,3:1}dic{k:vforkin〔1,2,3〕forvin〔3,2,1〕}{1:1,2:1,3:1}错误dic{k:vfork,vinzip(〔1,2,3〕,〔3,2,1〕)}{1:3,2:2,3:1}正确其实就是:dict(zip(〔1,2,3〕,〔3,2,1〕)) 生成器表达式 tu(iforiinrange(5))generatorobjectgenexprat0x11433d138next(tu)0next(tu)1next(tu)2next(tu)3next(tu)4next(tu)Traceback(mostrecentcalllast):FileUserszhongxinDesktoppyzx0208072。py,line33,inmoduleprint(next(tu))StopIteration