首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

Python 2.7.x 和 3.x 版

python现在很火,最近花了些时间去了解了一下,最初了解的是2.7.x版本,感觉,从书写上是很不习惯,少了一双大概号,取而代之的是缩进;然后跟kotlin和swift一样省去了每行的分号,象我们这种分号强迫症的人真心的不习惯;还有!True的条件改成not True、while后面可以跟else等等这些,真心不习惯啊!用2.7.x做了几天的测试,基本慢慢算有个了解了,也试着爬了些行业网的数据,感觉这个比PHP写爬虫方便很多。然后昨晚就在家里装了个3.X的版本,很悲催的发现,原来写的有很多的错误,万般无奈的检查之下,发现语句上是没什么问题,只是3.X版本不兼容部分的语句,例如最常用的print,raw_input都不一样了,今天花了些时间查一查,并总结了一下它们的区别。

03

Python数据分析(中英对照)·Tuples 元组

元组是不可变的序列,通常用于存储异构数据。 Tuples are immutable sequences typically used to store heterogeneous data. 查看元组的最佳方式是将其作为一个由多个不同部分组成的单个对象。 The best way to view tuples is as a single object that consists of several different parts. 元组在Python编程中有很多用途。 Tuples have many uses in Python programming. 一个特别重要的用例是当您希望从Python函数返回多个对象时。 One especially important use case is when you want to return more than one object from your Python function. 在这种情况下,您通常会将所有这些对象包装在一个元组对象中,然后返回该元组。 In that case, you would typically wrap all of those objects within a single tuple object, and then return that tuple. 现在让我们看一下使用元组可以执行的一些基本操作。 Let’s now take a look at some of the basic operations that we can do using tuples. 我首先要构造一个元组。 I’m first going to construct a tuple. 我将把它称为大写字母T,让我们在元组中输入一些数字。 I’m going to just call it capital T. And let’s just put in a few numbers in my tuple. 比如说1,3,5,7。 Let’s say 1, 3, 5, and 7. 同样,元组是序列的一种类型。 Again, tuples are a type of sequence. 因此,如果我想知道元组中有多少个对象,我可以使用len函数。 So if I wanted to know how many objects I have in my tuple,I can use the len function. 我还可以连接元组。 I can also concatenate tuples. 所以我可以做一些像T+。 So I can do something like T plus. 我需要一个新的元组。 I need a new tuple here. 比如说9号和11号。 Let’s say 9 and 11. 在本例中,Python向我返回一个新的元组,其中两个元组被放在一起。 And in this case, Python returns a new tuple to me where the two tuples have been put together. 因为元组是序列,所以访问元组中不同对象的方式取决于它们的位置。 Because tuples are sequences, the way you access different objects within a tuple is by their position. 因此,如果我想访问元组中的第二个对象,我会键入大写字母T、方括号和1。 So if I wanted to access the second object in my tuple,I would type capital T, square bracket, and 1. 记住,使用位置1将得到元组中的第二个对象,因为Python中的索引从0开始。 And remember, using position 1 is going to give me the second object in the tuple, because indices in Python start at 0. 您需要熟悉的另一个操作是如何打包和解包元组。 Another operation that you need to be familiar with is how to pack and unpack tuples. 假设我有两个数字,两个变量,x和y。 Imagine I have two numbers– two variables, x and y. 让我们快速创建它们。 Let’s just quickly create them.

02

Python数据分析(中英对照)·Lists 列表

列表是任何类型的对象的可变序列。 Lists are mutable sequences of objects of any type. 它们通常用于存储同质项目。 And they’re typically used to store homogeneous items. 列表是序列的一种类型,就像字符串一样,但它们确实有区别。 Lists are one type of sequence, just like strings but they do have their differences. 如果我们比较字符串和列表,一个区别是字符串是单个字符的序列, If we compare a string and a list, one difference is that strings are sequences of individual characters, 而列表是任何类型Python对象的序列。 whereas lists are sequences of any type of Python objects. 字符串和列表之间的另一个区别是字符串是不可变的,而列表是可变的。 Another difference between strings and lists is that strings are immutable, whereas lists are mutable. 除了这两个区别之外,字符串和列表当然也有自己的方法。 In addition to these two differences, strings and lists, of course,come with their own methods. 通常情况下,列表只包含一种类型的对象,尽管这不是严格的要求。 It is common practice for a list to hold objects of just one type,although this is not strictly a requirement. 让我们尝试几个简单的列表来测试它们。 Let’s try a couple of simple lists to experiment with them. 让我们构造一个简单的数字列表,以进一步了解列表。 Let’s construct a simple list of numbers to learn a little bit more about lists. 所以我要构造一个数字列表。 So I’m going to construct a list of numbers. 我要称之为数字。 I’m going to call it numbers. 我将使用数字2、4、6和8。 And I’ll use numbers 2, 4, 6, and 8. 假设我想提取或访问列表中的第一个元素。 Imagine I wanted to extract, or access, the first element of my list. 我要做的第一件事是键入列表的名称,然后我需要方括号。 The first thing for me to do is type the name of the list,then I need my square brackets. 现在请记住,在Python中,索引从零开始。 Now remember, in Python, indexes start at zero. 因此,为了能够查看该列表的第一个元素,我需要将其放入索引0,位置0。 So for me to be able to look at the first element of that list,I need to put in index 0, position 0. 在这里,Python告诉我第一个对象,即位于位置0的对象,是数字2。 Here, Python tells me that the first object, meaning the object located at position 0, is number 2. 如果我将索引更改为1,Python将给我第二个对象。 If I change the index to 1, Python gives me the second object. 现在,如果我想知道列表上最后一个对象是什么,我可以从右到左计算位置。 Now if I wanted to find out what is the very last object on my list,I can count positions from right to left. 这意味着我必须使用负指数。 And

02
领券