Python learns a __python

Source: Internet
Author: User

------------------------Data Structure------------------------

If you've ever had programming experience, then you probably know what a data structure is.

The data structure is the way in which it is organized. In Python, the simplest data structure is an array, which is a series of numbers. Each data in this group has an iconic number, called an index, with the index of the first item 0(instead of 1), the second is 1, and the third is 2 ...

The index can also be negative, the index of the first and last item is 1, and the penultimate is-2.

------------------------Python built-in array------------------------

Python has 6 built-in data structures, and in this chapter I'll just introduce you to 2 kinds of list (lists) and tuple (tuples). The difference between list and tuple is that the list can be changed but tuple cannot be modified.

------------------------List------------------------

The use of list is to include data in "[", "]", with commas "," to distinguish between data, such as:

>>>zhang = [' Zhang San ', 13]

>>>li = [' Li Si ', 23]

>>>friends = [Zhang, Li]

>>>friends

[[' Zhang San ', +] [' Li Si ', 23]]

You can put almost any data in the list

------------------------Index------------------------

The string also has an index similar to the list:

>>>greeting = ' Hello world '

>>>GREETING[0]

H

>>>GREETING[-1]

' d '

>>>greeting[:6]

' Hello '

>>>greeting[6:]

' World '

>>>greeting[2:5]

' Llo '

>>>greeting[2:-2]

' Llo Wor '

The index is the number that is written in the square brackets "[]" after that variable. An index with a colon is called a "slice" of an array, and the number before the colon is the index of the starting point, followed by the endpoint index. If there is no starting point default is 0, if there is no end of the default is to the end.

Slices can also have three colons:

>>>number = [1,2,3,4,5,6,7]

>>>number[1:5:2]

[2, 4]

>>>NUMBER[2::2]

[3, 5, 7]

>>>number[2::]

[3, 4, 5, 6, 7]

The first two numbers represent the start and end points, the average increment of our cut (one element at a few extracts) is 1, and if you set a third number, you can change the increment and the default value is 1.

Addition and multiplication of------------------------list------------------------

Let's take a look at an example:

>>>[1,2,3]+[4,5,6]

[1, 2, 3, 4, 5, 6]

>>> ' Hello ' + ' world '

' HelloWorld '

>>>[1,2,3]+ ' World '

Traceback (most recent call last):

File "<stdin>", line 1, in<module>

Typeerror:can only concatenate list (not "str") to List

Addition is the merging of two lists into one. The list can only be added to the list, and string can only be added to string.

>>> ' Hello ' * 5

' Hellohellohellohellohello '

>>>[12] * 5

[12, 12, 12, 12, 12]

Multiplication is to repeat the string or list n times.

------------------------"Members"------------------------

>>>names = [' Zhang ', ' Li ', ' Wang ']

>>> ' Wang ' in Names

True

>>> ' Zhao ' in Names

False

To see if a value is in a list, simply use the In keyword, and there is no return true without return false.

------------------------len, Min, Max------------------------

Describes the three functions for list:

>>>numbers = [10, 401, 201]

>>>len (Numbers)

3

>>>min (Numbers)

10

>>>max (Numbers)

401

>>>min (10,21)

10

>>>max (21,342,10)

342


Len is a function that looks at the length of a list. Min and Max are the maximum values for the list, and Min and Max can directly give the parameters (No limit).

------------------------Summary------------------------

The index starts at 0 instead of 1.

The slice of a list can have two colons, the first number is the starting point of the extraction, the second is the endpoint of the extraction, and the third is the increment between the extracted elements.

The addition of list is merging, and multiplication is repetition.

In keyword to see if a value is in a list.

Len, Min, Max, respectively, can see the length, the minimum value, the maximum value.


From:http://blog.sina.com.cn/s/blog_5c6760940100b7vl.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.