Python learning Summary

Source: Internet
Author: User
Tags python list

Python has rich data types,
Including strings, lists, tuples, Dictionary sets, etc. Each type of data has the same features and makes good use of them.
It makes Python programming quite easy. to make full use of it, you should fully understand their features and summarize their features.

 

Python string:
A string is a string sequence of a single character in Python. You can perform operations such as slice connection on the string.
Below are some of the Python stringsArticle:
Python string replacement
Python string connection

Python list:
A list is an ordered collection composed of any data type. It is a bit like an array of other languages. If a friend who has used other languages believes that the List is not unfamiliar, the list operation is very
Rich. You can use dir to view its own methods.
The following are some articles about the python list:
Python list
Python List Operation Method

Python tuples:
Python tuples and lists are both ordered sequences. The difference is that tuples are immutable types and are often used in system configuration files as system configuration information, which is safer and more stable, it is not easy to be changed.
Below are some articles about Python tuples:
Python tuples

Python dictionary:
A dictionary is the only ing type in a python object. It is similar to a mathematical ing. Other languages also have approximate data types, and there are many built-in methods.
The following are some articles about the python dictionary:
Python dictionary:
Add a python dictionary

Python set:
The Python set seems to be rare in other languages, and it is still very good to do some intermediate processing.
The following are some articles about Python collections:
Python set

The following is a summary of some friends.

Commonalities:
1. They are all the core types of Py and are part of the py language.

Core Type and non-core type
Most core types can generate their objects through specific syntax. For example, "spam" is the expression used to create string-type objects;
Non-core types must be created by built-in functions. For example, the file type must be created by calling the built-in function open.
Class can also be understood as a custom non-core type.

2. Cross-border indexing is not allowed for border checks.

3. []
S = 'spam'
Print s [0]
S = ['s', 'P', 'A', 'M']
Print s [0]
S = {'name': 'bob', 'age': '12 '}
Print s ['name']
S = ('s', 'P', 'A', 'M ')
Print s [0]

4. Most of the protocols support iteration.

Differences:
1. orderliness
Strings, lists, and tuples are sequences. tuples can be considered as immutable lists.
Dictionary is ing
The sequence is ordered, and the dictionary does not have a reliable left or right sequence.

2. Variability
The string, tuples (and numbers) are non-mutable, that is, the content of the corresponding bucket is unchangeable after the value is assigned, unless the object is destroyed.
Lists and dictionaries are changeable.

S = 'spam'
S = 'Z' + s [1:]

The memory space (for example, address 0x000123) pointed to by S in the first line stores the string 'spam'
S in the second line is changed to 'zpam ', but it just points to a memory address that stores the 'zpam' string, the original memory space starting from 0x000123 may still be a 'spam' until the system cleans it up.

3. Sequential operations
Supported sequences (string, list, and ancestor), not ing (dictionary)
Note: The sequence operation generation type remains unchanged.

Col = 'spam'
Print Col [1:]
# The result is Pam.

Col = ['s ', 'P', 'A', 'M']
Print Col [1:]
# The result is ['P', 'A', 'M'].

Col = ('s', 'P', 'A', 'M ')
Print Col [1:]
# The result is ('P', 'A', 'M ')

4. List parsing expression
The expression supports sequences (strings, lists, and ancestor) and maps (dictionaries ).
Note: Unlike sequence operations,
A list parsing expression can only generate one list;
Double = [C * 2 for C in 'spam']
Print double
The result is ['ss', 'pp ', 'AA', 'mm'].

L1 = [1, 2, 3]
L2 = [4, 5, 6]
L3 = [(a + B) for (a, B) in zip (L1, L2)]
Print L3
# The result is [5, 7, 9].

Tup = (1, 2, 3)
Double = [C * 2 for C in Tup]
Print double
# The result is [2, 4, 6].

A = {'A': 'zw', 'B': 'ww '}
B = [I * 2 for I in A. Items ()]
Print B
# The result is [('A', 'zw', 'A', 'zw'), ('B', 'ww ',' B ', 'ww')].

The following sectionCodeIt seems that the List parsing expression supports the dictionary. The iteration of the dictionary should be the iteration of its keys, the items (), keys (), and values () of the dictionary () the methods are all returned lists. Therefore, the For I in a is best written as for I Ina. keys (), equivalent to the effect. In short, the list parsing expression does not support dictionaries. In essence, the list parsing expression uses a circular structure for the list and generates a new list. First understanding

A = {'A': 'zw', 'B': 'ww '}
B = [I * 2 for I IN A]
Print B
# The result is ['A', 'bb']

Copy code
5. nesting
In addition to strings, lists, tuples, and dictionaries can be nested at multiple layers.

# Section 1
B = [1, 2, 3]
Tup = (4, 5, B)
Print Tup [2]
# The result is [1, 2, 3].
B [0] = 'X'
Print Tup [2]
# The result is ['x', 2, 3].

# Section 2
B = '000000'
Tup = (4, 5, B)
Print Tup [2]
# The result is 123'
B = 'xxx'
Print Tup [2]
# The result is still '123'

The preceding two sections show that nesting does not conflict with variability.
In Tup = (4, 5, B), Memory B is a variable pointer or memory address. It is a number.
In the first section, B points to a list. The list is changeable. Therefore, after B [0] = 'X' is operated, the index of the original database can be changed;
In the second segment, B points to a string, which is non-mutable. Therefore, after B = 'xxx' is operated, the pointer B changes itself and points again to another memory space, the memory address in the Tup tuples (the former B) indicates that the space is not changed, or the string is '123 '.
Therefore, Tup is always non-Immutable in the first or second segment, and the change is also a change in the memory pointed to by its elements as pointers.

Summary: The most important thing about Python data types is to understand the variability and immutability. I believe that understanding it can reduce many errors and avoid detours!

Author: Lao Wang @ Python tutorial
Old Wang Python provides Python tutorials and Python downloads related to pythn.

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.