dangdawei 发表于 2018-11-28 10:41:45

入门Python

如果这篇文章还不能让你入门Python,那我也是没办法了!Python编程之家 2018-11-27 08:10:00
http://p99.pstatp.com/large/pgc-image/7dee41777e884529b9bf8d79ac23414d


一,输入输出当然在学习Python的道路上肯定会困难,没有好的学习资料,怎么去学习呢?所以小编准备了一份零基础入门Python的学习资料。关注,转发,私信“01”即可领取!http://p1.pstatp.com/large/pgc-image/ee949762bbe847ac8310319451848102


输入:input(输入的都是字符串)输出:printhttp://p99.pstatp.com/large/pgc-image/5ee9bcff27df4f098eb52261511c5d0c



二,导入模块import ...或 from ... import ...或 import ... as ...http://p9.pstatp.com/large/pgc-image/bd3340d75eec49268893bbf0544670c3



三,基础语法1,标识符标识符由字母、数字、下划线组成,区分大小写,不能以数字开头。以下划线开头的标识符有特殊含义。以单下划线开头的(_foo)的代表不能直接访问的类属性,以双下划线开头的(__foo)代表类的私有成员;以双下划线开头和结尾的(__foo__)代表Python里特殊方法专用的标识,如__init__() 代表类的构造函数。2,缩进Python的代码块不使用大括号来控制类、函数、以及其他逻辑判断,而是使用缩进来写实现代码分组。通常用四个空格来进行缩进。3,注释python中单行注释采用 # 开头。多行注释使用三个单引号(''')或三个双引号(""")。4,一条语句分多行显示Python语句中一般以新行作为为语句的结束符。但是我们可以使用斜杠( )将一行的语句分为多行显示,如下所示:http://p99.pstatp.com/large/pgc-image/96cb8e5afa0640798881616c158ca7c2


语句中包括[],{}或()括号就不需要使用多行连接符。http://p3.pstatp.com/large/pgc-image/2a09e64e94ac436182f75b3f60d7fcc9


5,同一行显示多条语句Python可以在同一行中使用多条语句,语句之间使用分号分割。
四,数据结构python内建的数据结构有列表,元组,字符串,字典,集合等。此外常用的还有numpy中的array,以及pandas中的dataframe和series。1,有序数据结构:List(列表),是有序集合,没有固定大小,可以通过对偏移量以及其他方法修改列表大小。列表的基本形式如:Tuple(元组),是有序集合,是不可变的,可以进行组合和复制运算后会生成一个新的元组。元组的基本形式比如:(1,3,6,10)String(字符串),也是有序集合,字符串的基本形式比如:’hello'。2,无序数据结构:Set(集合),是一个无序不重复元素的集。基本功能包括关系运算和消除重复元素。集合的基本形式如:{'apple','orange','banana'}Dictionary(字典)是无序的键:值对 (key:value 对)的集合。键必须是互不相同的(在同一个字典之内)。字典的基本形式如:{'jack': 4098, 'sape': 4139}
五,列表1,建立列表http://p99.pstatp.com/large/pgc-image/6d4563cc0b4143488e051204164acfac


2,访问列表可以使用下标访问和切片访问http://p99.pstatp.com/large/pgc-image/eb7ec928aa584e02a41a0f99e75b1e3d


3,修改列表http://p3.pstatp.com/large/pgc-image/d3ea51af67684771badd915067116d0b


4,列表常用函数http://p99.pstatp.com/large/pgc-image/96285ad36e7e41d4b92aafd4c33e5328


5,列表常用方法http://p3.pstatp.com/large/pgc-image/4f1ab1c4cc4445699ee43013196bd77a



六,字典1,创建字典http://p1.pstatp.com/large/pgc-image/82e6d15e756e4f2cbcce6243b4cd1524


2,常用字典操作方法http://p3.pstatp.com/large/pgc-image/1f5efde9dd39411fb188a2762107f394



七,元组1,创建元组http://p9.pstatp.com/large/pgc-image/4521b2dce92e40f18a3f4fe55c87c80f


2,修改元组http://p3.pstatp.com/large/pgc-image/6da2122bc4a24b5dbc36b34e16e1af5b



八,字符串1,创建字符串可以用单引号,双引号,或者三重引号创建字符串。( 表示换行符,        表示制表符)http://p99.pstatp.com/large/pgc-image/7d3a360d04d145c988500fe3b102b535


2,字符串基本操作http://p9.pstatp.com/large/pgc-image/20264c8b29c04d7b9936424c678e3e80


http://p3.pstatp.com/large/pgc-image/98171b893ff14f81bb5e5c4e93afb5d5


http://p3.pstatp.com/large/pgc-image/d76bd08fdfa84947815bd5247a596619


3,格式化字符串http://p99.pstatp.com/large/pgc-image/76478b8a8ca54faabc121daeef0e829d



九,条件语句1,多分支结构python中不支持switch语句,只能用if...(elif...)else...来实现多分支选择结构。此外,还可以巧妙地通过字典数据结构实现多分支选择结构。http://p3.pstatp.com/large/pgc-image/ec1192a54d0e435fa8cb834ac6f9d142


http://p3.pstatp.com/large/pgc-image/ec1192a54d0e435fa8cb834ac6f9d142


2,and 和 orpython中的与、或、非分别用关键字 and,or,not 表示。python做逻辑运算时把空的列表、元组、集合等当做False。对or而言,Python会由左到右求算操作对象,然后返回第一个为真的操作对象。Python会在其找到的第一个真值操作数的地方停止,通常叫短路计算。and 会停在第一个为假的对象上。http://p1.pstatp.com/large/pgc-image/cb60fa5b6dc64499ba8a9ff52bec7183



十,循环语句Python提供了for循环和while循环(在Python中没有do...while循环)。for循环一般比while计数器循环运行得更快。break语句,在语句块执行过程中终止循环,并且跳出整个循环。continue语句,在语句块执行过程中终止当前循环,跳出该次循环,执行下一次循环。pass语句,是空语句,是为了保持程序结构的完整性。不做任何事情,一般用做占位语句。俗称代码桩。http://p3.pstatp.com/large/pgc-image/89daf300077548f2972233fb466dc0ff



十一,函数1,定义函数使用 def关键字定义函数,用return返回函数值,若没有return,返回值为None。http://p3.pstatp.com/large/pgc-image/ec1192a54d0e435fa8cb834ac6f9d142


2,默认参数默认参数放在普通参数后面http://p99.pstatp.com/large/pgc-image/0efff3cc254d4410b4c5979d2eee7ca3


3,可变参数可变参数就是传入的参数个数是可变的,可以是1个、2个到任意个,还可以是0个。可以用 *k 的方式定义可变参数。可变参数传入函数后自动组装为一个tuple。http://p9.pstatp.com/large/pgc-image/4b0a4927bc1d40269036968128145d3e


http://p3.pstatp.com/large/pgc-image/ec1192a54d0e435fa8cb834ac6f9d142


4,关键字参数关键字参数允许你传入 0 个或任意个含参数名的参数,这些关键字参数在函数内部自动组装为一个 dict。http://p3.pstatp.com/large/pgc-image/cc57e03a2dc84ab0b823e63b6620161c


5,混合参数http://p9.pstatp.com/large/pgc-image/6464eb599a2b40d38ac07b63bd02949e


6,递归函数递归可以代替循环,很多时候会更加简洁。http://p99.pstatp.com/large/pgc-image/cded35ac8f3b4aa68634176f326beac9


7,装饰器装饰器可以在不改变函数代码前提下赋予你的函数额外的功能。http://p99.pstatp.com/large/pgc-image/7e97bc71269b459ab58604202223311f


http://p3.pstatp.com/large/pgc-image/ec1192a54d0e435fa8cb834ac6f9d142


http://p3.pstatp.com/large/pgc-image/ec1192a54d0e435fa8cb834ac6f9d142



十二,lamda 匿名函数lambda只是一个表达式,适合定义较为简单的函数。lambda函数拥有自己的名字空间,且不能访问自有参数列表之外或全局名字空间里的参数。lamda函数定义语法是:fun = lamda 参数序列: 返回值表达式一般来说通过使用lamda函数可以节约程序开支并加快运行速度。http://p9.pstatp.com/large/pgc-image/25a342008db94571a42d9b029794c0c2



十三,迭代器迭代器在python中是以C语言的速度运行的,常用的迭代器如下。http://p99.pstatp.com/large/pgc-image/b055c0c844804c228b461a30708003bf


http://p3.pstatp.com/large/pgc-image/ec1192a54d0e435fa8cb834ac6f9d142


http://p3.pstatp.com/large/pgc-image/ec1192a54d0e435fa8cb834ac6f9d142



十四,Python 推导式Python中的推导式是Python语言所有语法规则中最大的一个彩蛋,简直是一个超级语法糖。淋漓尽致地体现了Python语言 simple,readable 和 powerful 的特点。一旦掌握则爱不释手,欲罢不能,永生难忘。Python推导式可以生成列表,集合和字典。1,列表推导式http://p1.pstatp.com/large/pgc-image/99d66fcb719b4de6bd8288a2797510f2


http://p99.pstatp.com/large/pgc-image/4c9d061a8a9247d2be03b9831bacc37a


2,字典推导式http://p3.pstatp.com/large/pgc-image/ec1192a54d0e435fa8cb834ac6f9d142


3,集合推导式http://p3.pstatp.com/large/pgc-image/ec1192a54d0e435fa8cb834ac6f9d142



十五,Python类和对象在Python中,万物皆为对象。对象由类创建而来,所有的类都是object基类的子类。1,类和对象基本概念类: class, 抽象数据结构,数据和算法的封装。如:定义一个类,dog。对象: object,类的实例。如:dog类的一个实例,点点 dot。属性:properties,和对象关联的数据部分。如:weight 体重,breed 品种。方法:methods,和对象关联的算法部分。如:run(), eat(), bark()。2,创建类和对象http://p9.pstatp.com/large/pgc-image/771a0d20a1504638b58fea1a4d0278e6


http://p9.pstatp.com/large/pgc-image/e0918b71186a4863ae0e4fb5448ba8f8


3,获取对象信息http://p3.pstatp.com/large/pgc-image/ec1192a54d0e435fa8cb834ac6f9d142


http://p9.pstatp.com/large/pgc-image/daddcd2537ef4918be19d555e151522e



页: [1]
查看完整版本: 入门Python


Warning: PHP Startup: Unable to load dynamic library 'fileinfo.so' (tried: /usr/local/zend/php7.4/include/php/ext/fileinfo.so (/usr/local/zend/php7.4/include/php/ext/fileinfo.so: cannot open shared object file: No such file or directory), /usr/local/zend/php7.4/include/php/ext/fileinfo.so.so (/usr/local/zend/php7.4/include/php/ext/fileinfo.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0