What you need to know about Python basic learning

Source: Internet
Author: User
Objects of 1.1Python

Python has a lot of built-in object types, including numbers, strings, lists, collections, tuples, dictionaries, and so on, all objects in Python

Pickle pickling-in Python, if some objects require persistent storage and do not lose the type and data of the object, then the data needs to be serialized, serialized, used, and then restored to the original data, and the serialized process becomes pickle (pickled)

One of the data types built into Python is the list: lists.

A list is an ordered collection.

A list consists of a series of elements arranged in a particular order. Use [] to denote.

The data types of the elements in the list can also be different, such as:

>>> L = [' Apple ', 123, True]

2.1 Index List

Starting from 0 instead of 1. Python will report a indexerror error when the index is out of range, so make sure the index does not cross the boundary.

If you want to take the last element, in addition to calculating the index location, you can also use 1 to index, directly get the last element.

2.2 Modifying, adding, and deleting list elements

2.2.1 Adding elements

Add with Append ()

2.2.2 Inserting elements

Inserts insert () to specify the index and value of the new element.

>>> Classmates.insert (1, ' Jack ')

>>> Classmates

[' Michael ', ' Jack ', ' Bob ', ' Tracy ', ' Adam ']

2.2.3 deleting elements

1.1. Delete Del to know index location del abc[0]

1.2. To delete the element at the end of the list, use the Pop () method.

1.3. To delete the element at the specified position, use the pop (i) method, where I is the index position.

1.4. Use the method Pop () to delete the element (eject), you can then use the delete value.

1.5. Remove the element by value remove ()

2.2.4 Modifying elements

To replace an element with another element, you can assign a value directly to the corresponding index position.

Classmates[1] = ' Sarah '

2.3 Organization List

Method sort () to sort the list in a permanent order, with the first letter sorted.

The function sorted () Sorts the list in a temporary order.

Reverse the Print List reverse ().

Determines the list length Len ().

The 2.3.1 list contains another list

The list element can also be another list, such as:

>>> s = [' Python ', ' Java ', [' asp ', ' php ', ' scheme ']

>>> Len (s)

4

To get ' php ' can write p[1] or s[2][1], so s can be regarded as a two-dimensional array.

3.1 Traversing the entire list

Using a For loop

For a in a: do not miss the colon

Print () Note indent

Print () Summary After the end of the loop

3.2 Creating a list of values

1. Function range (), range (1,5) can only print 4 digits cannot print 5

2. Use the function list () to convert range () to list (range (1,5))

3. * * 2 heart numbers mean square

4. Perform a simple statistical calculation for the number list min () min; max () maximum; sum () sum

5. List resolution merges the code for the For loop and the creation of the new element into one line and attaches the new element automatically:

[Value**2 for value in range (1,11)]

3.3 Using part of the list

3.3.1 Slices

Looping is cumbersome for operations that often take a specified range of indexes, so Python provides a slicing (Slice) operator that simplifies operations.

1. [:] The start of the slice starting from 0, [: 4] means 0 to 3,[-3:] = 3 of the countdown

2. [2:] indicates from 3rd to the end

3. Don't even write anything, just write [:] to copy a list as it is.

4. [:: Step];[::-1] turn the character upside down

5. The string ' xxx ' can also be seen as a list, each element being a character. Therefore, a string can also be manipulated with a slice, but the result of the operation is still a string.

6. Number of returns: STR (n) = = str (n) [::-1]

3.4 tuples

Another ordered list is called a tuple: a tuple.

The tuple and list are very similar, but once the tuple is initialized it cannot be modified, and Python is called a immutable, immutable list of values that cannot be changed. Because the tuple is immutable, the code is more secure. If possible, you can use a tuple instead of a list as much as possible.

3.4.1 Defining tuples

1. Add elements in (), separated by commas, with the same access as the list. However, you cannot modify the value. But you can assign a value. Numbers can be directly (1,2,3,4), characters need to be quoted (' A ', ' B ', ' C ')

2.Python when displaying a tuple of only 1 elements, a comma is added to prevent you from misunderstanding the parentheses in the mathematical sense.

t = (1,)

3. The so-called "invariant" of a tuple is that each element of a tuple is directed to never change. That point ' a ', it cannot be changed to point to ' B ', pointing to a list, cannot be changed to point to other objects, but the list itself is variable! What do you do if you want to create a single tuple that does not change the content? It is important to ensure that each element of a tuple cannot be changed.

3.5 Formatting Code

1. PEP8

2. Indent format, 4 spaces

3. No more than 79 characters of the President

4. Use the empty Line organization structure

Related Article

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.