Python Core Data type notes

Source: Internet
Author: User

Here, first of all, the Python core data type is considered to be the python built-in data type here.

In Python. Sequence types contain strings, lists, and tuples

String:

String literals: Introducing text into single quotes, double quotes, and three quotation marks

The default encoding type is character encoding (8bit)

In Python2, if you want to use Unicode encoding (16bit), you need to precede the quotation marks in the definition string

In Python, there is the concept of a document string, the so-called document string is in the module, the class or function is the first statement is a character (in quotation marks), then the character is a document string, you can use the __doc__ property reference

Eg:def stringtest ():

The "test string"//document string also follows the format, for example, there are four spaces in front of it

print "Python"

The reference function Stringtest () can be

Reference properties, such as referencing the __doc__ property here, stringtest.__doc__

The operations supported by the string:

Indexes, shards, extended shards (note that shards generate a new memory object and do not modify the original object)

Built-in method of str type (can be obtained using DIR (str))



List:

List is container type

Definition: An ordered set of arbitrary objects that access elements through an index, a mutable object that supports heterogeneous (containing different types of memory objects), supports arbitrary nesting

Support in situ modification:

Modifies the specified element, modifies the specified shard, deletes the statement, and the built-in method (Reverse,sort,count,append,extend,index,pop,remove)

List1 + list2: Merges two lists, returns a new list, does not modify the original list

List1*n: Repeats the list of NC times to return a new list without modifying the original list

In: Member relationship Judgment character, obj in container

Not in: ditto

The use of append is attached, and if a list is attached, the list is treated as an element, and the slices and extend are appended as separate elements (if the attached object is a list)

How to copy a list:

1.list2 = list1///This way List1 and List2 refer to the same memory object, so one of the lists is modified and the other list is modified accordingly (shallow copy)

2.list2 = list[0:]//This is equivalent to removing all the elements of List1 and re-assigning to List2

3.list2 = Copy.deepcopy (list1)//This method requires importing a Python built-in module copy and then using the Deepcopy method in the module to achieve deep replication.


Tuple (tuple):

is also a container type

An ordered set of arbitrary objects that access the elements through an index, support for heterogeneous and nested, but immutable types, which, once defined, are fixed-length

Built-in method: Count,index

Common actions: Defining empty tuples ()

Parentheses can be omitted when defining tuples (not recommended)

Tuple1 + tuple2: Generate a new tuple

Tuple*n: Similar to the meaning of a list

Tuples also support Member relationship judgments

Although the tuple itself is immutable, changes to such elements do not return a new tuple when a mutable element is nested inside the tuple (such as a list as an element of that tuple)


Dictionary (dict):

Dictionaries are stored in Key-value key-value pairs, in other programming languages also known as associative arrays or hash lists, in Python, dictionaries are mutable types of containers , support for heterogeneous and nested, and the list is different from the dictionary elements are unordered, Element access can only be achieved by key, and any type may be a key, and the sequence type can only be used as a numeric key.

Dictionary definition: {}, each element inside is used, separated, and used between key values: Split

{key1:value1,key2:value2 ...}

Built-in method: Clear//Erase all elements

Copy//Dictionary replication: Dict2 = Dict1.copy (), equivalent to deep copy

Dict2 = dict1//Shallow copy

Formkeys//dict2 = Dict1.formkeys (S,[v]) reads the key from the dictionary S, then the value is specified using V

Get//Specify key value

Has_key//Determine if there is a key

Items//Returns a list in which each element is a tuple of key-value pairs in the previous dictionary

Variable unpacking (unpack):

Dict1 = {' A ': [[+], ' B ': [4,5,6]}

The result of Dict1.items () is: [(' a ', [+]], (' B ', [4,5,6])]

Tuple1,tuple2 = Dict1.items () Result://This method is the variable unpacking in Python

Tuple1 = (' a ', [+])

Tuple2 = (' B ', [4,5,6])

Variable unpacking, must correspond to the left and right

Keys://Returns a list of keys

Values//Returns a value list

Pop//eject the corresponding key value pair for the specified key

Popitem//Random Popup key-value mapping


This article is from the "Zxcvbnm Xuan ye" blog, please be sure to keep this source http://10764546.blog.51cto.com/10754546/1723034

Python Core Data type notes

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.