设为首页 收藏本站
查看: 794|回复: 0

[经验分享] Learning.Python.3rd.Edition 笔记 [0:4]

[复制链接]

尚未签到

发表于 2017-4-29 14:33:47 | 显示全部楼层 |阅读模式
前三章介绍Python的历史,特性,以及相关编辑器等关系,暂时不考虑研究,如有需要可以参考wiki百科,自行阅读

其中一些取自Wiki的资料,可以了解一下

我们日常使用比较多的是C版本的Python,也就是用C语言实现的Python解释器,除此之外,还有Java实现的Jython和.net实现的IronPython


第四章 Introducing Python Object Types

Python’s Core Data Types
Python 核心数据类型

Numbers        1234, 3.1415, 999L, 3+4j, Decimal
Strings        'spam', "guido's"
Lists          [1, [2, 'three'], 4]
Dictionaries   {'food': 'spam', 'taste': 'yum'}
Tuples         (1,'spam', 4, 'U')
Files          myfile = open('eggs', 'r')
Other types    Sets, types, None, Booleans

Python的动态在于,可以将一个变量指向不同的类型,静态在于调用方法时,必须是指定类型才可以进行调用

Number类型,相关的模块
Math--提供与Java类似的Java包功能
random--随机数,功能比Java使用更方便一些,如random.random(), random.choice([1,3,4,5,6])

字符串,简单的sequences of one-character strings 字符链表,可以直接使用下标的方式,访问指定索引的字

符,注意Python下不包含char类型

[1:3] 链表中所用的切片取值

也可以使用* 与+ 来进行字符串的拼接,不过需要注意,字符串是不可修改的,所有的操作将会返回一个新的对

象,代表为,s[0]='3' 将会抛出异常

在Python中,Number,string,tuples都是不可修改的, list 和dictionaries是可以修改的

List,tuples,string中很多方法都是共同拥有的,可以使用dir查看所拥有的属性与方法

'''xx''' 用于定义多行字符串,多用于HTML/CSS,以及docString

Python支持使用r和u关键字修饰字符串: 如u'xxx',其中u代表Unicode  r代表2进制流格式,主要是用于忽视/

的转义符


正则模式匹配
使用re模块,需要import


match = re.match('Hello[ \t]*(.*)world', 'Hello Python world')
match.group(1)

List,可以修改的序列,可以动态的收缩和添加,对对象本身方法的调用将会影响到对象本身

使用的时候,需要注意区域检查,防止索引越界,尤其进行添加元素时候,需要使用append方法,移除的话,可以使

用pop或者remove方法

List中可以随意嵌套其他类型,如List

一种比较特殊的List遍历取值方式
M=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
col2 = [row[1] for row in M]
col2==[2, 5, 8],区域每个元素中的第2个字符串,不知可否多重进行嵌套,不过增加了不少代码复杂度

上述需要注意[]的使用,下面加上过滤条件的例子
[row[1] for row in M if row[1] % 2 == 0]
[c * 2 for c in 'spam']  //对结果进行计算

Dictionaries 字典, 定义的格式 d={key:value}
其中key一般使用字符串表示,value可以为任意的类型

可以通过将对象的引用变量 =0,将一个对象回收 //并不确认

字典的循环, 使用keys()方法,获取到字典所有的key,然后在循环keys中进行访问

Ks = D.keys()
for key in Ks:
print key, '=>', D[key]

在进行循环的时候,可以注意效率的写法
>>> squares = []
>>> for x in [1, 2, 3, 4, 5]: # This is what a list comp does
squares.append(x ** 2)
>>> squares
这样只会创建一个List对象

使用字典时,也要防止出现key不存在,而导致的访问错误
D.has_key('f'),进行判断


Tuples,元组 定义方式(), 类似List的数据类型,区别在于值不可修改,而且定义单元素的tuples时,需要注意

使用,逗号, 常用于参数传递时,防止被修改

Files 类型
可以使用内置的open函数,传入方法名+打开方式进行文件的创建
f = open('data.txt', 'w')  //w 代表写入,如果不存在将会创建,如果存在将会被覆盖.
主要使用close方法进行关闭

具体方法可以使用help 与dir进行查看
dir(file)
help(file.seek)

其他内置类型

Sets 集  
X = set('spam'),其中包含的内容都是唯一的

decimal 小数,代表高精度的小数运算格式

Booleans 布尔  包含 False True, 注意首字符大写,转换成int类型时,分别为0,1

None类型, 类似Null的存在

type(),同样也返回一个特殊的type类型:type(type(False))进行获取.

下面复制书上一些章节的问题,都是不错的题目,用空可以复习一下(其实我是为了凑字数)

1. Name four of Python’s core data types.
2. Why are they called “core” data types?
3. What does “immutable” mean, and which three of Python’s core types are considered immutable?
4. What does “sequence” mean, and which three types fall into that category?
5. What does “mapping” mean, and which core type is a mapping?
6. What is “polymorphism,” and why should you care?

Quiz Answers
1. Numbers, strings, lists, dictionaries, tuples, and files are generally considered to
be the core object (data) types. Sets, types, None, and Booleans are sometimes
classified this way as well. There are multiple number types (integer, long, floating
point, and decimal) and two string types (normal and Unicode).
2. They are known as “core” types because they are part of the Python language
itself, and are always available; to create other objects, you generally must call
functions in imported modules. Most of the core types have specific syntax for
generating the objects: 'spam,' for example, is an expression that makes a string
and determines the set of operations that can be applied to it. Because of this,
core types are hardwired into Python’s syntax. In contrast, you must call the
built-in open function to create a file object.
3. An “immutable” object is an object that cannot be changed after it is created.
Numbers, strings, and tuples in Python fall into this category. While you cannot
change an immutable object in-place, you can always make a new one by running
an expression.
4. A “sequence” is a positionally ordered collection of objects. Strings, lists, and
tuples are all sequences in Python. They share common sequence operations,
such as indexing, concatenation, and slicing, but also have type-specific method
calls.
5. The term “mapping” denotes an object that maps keys to associated values.
Python’s dictionary is the only mapping type in the core type set. Mappings do
not maintain any left-to-right positional ordering; they support access to data
stored by key, plus type-specific method calls.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-370885-1-1.html 上篇帖子: hive结合python: Transform的使用 下篇帖子: Python线程编程的两种方式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表