Python built-in data types

Source: Internet
Author: User
In general, Python is positioned in the programming language as the scripting language--scripting language high-order dynamic programming language.

Python is based on data, variable value change refers to a variable to refer to an address.
That is, the address of the display variable, Id (variable).
So a specific value, there will be a different variable name.

Data types for Python:

Numbers, strings, lists, tuples, dictionaries
Numbers and strings are actually very basic data types, and in Python there is not much difference between the other languages, and this is not the case.

Dictionary Introduction:

Dictionary is one of the built-in data types for Python, which defines a a-to relationship between keys and values.
is actually the hash array that is commonly referred to.
Simple definition of Dictionary: dic={' Key1 ': ' Value1 ', ' Key2 ': ' Value2 '}
Dictionary cannot have duplicate keys, assigning a value to the same key overrides the original value.
You can add Key-value value pairs to dictionary at any time.
There is no order of elements in the dictionary! Because it is through key to find value again, there is no order of the points.
In dictionary, the case of key is sensitive!
Dictionary is not just used to store strings, dictionary values can make arbitrary data types, including the dictionary itself. Also, in a single dictionary, the value of dictionary does not need to be all the same data type and can be mixed and matched. The data type of the dictionary key has a relative requirement, but it can be mixed and matched with a variety of data types.
Del can use a key to remove the corresponding element from the dictionary. >>>del Dic[12]
Clear () can delete all the values in a dictionary, but the original dictionary is still in, just becomes empty {}.>>>dic.clear ()

List Description:

List is the most frequently used data type in Python.
The data types in the list can be arbitrary and support dynamic expansion.
Simple definition of list: lis=[' A ', ' B ', ' abc ', ' ASD '. A list is a set of ordered elements that are enclosed in square brackets.
The list supports both positive and negative indexing modes: The positive index is the general case, starting with 0.
A negative index is counted from the tail of the list. The last primitive of any non-empty list is always list[-1].
The list supports sharding, fetching data from the middle of the list. It is important to note that the starting position of the slice.
Add data to List: Lis.append (' New ') adds data to the end of the list;
Lis.insert (2, ' New ') inserts a value at the 2 position of the list;
Lis.extend ([' New ', ' nwe ']) links the new list in the original list (at the end).

The difference between Append () and extend ():

The argument to ①.append () can be any data type, or it can be a list, but it is a list that is added to the original list as an element.
The parameter of ②.extend () can only be a list, and all the elements of the entire list are added to the original list one after the other.
③. Searching in list: Lis.index (' a ')
④.index finds a value in the list and returns the index value of its first occurrence. If multiple occurrences occur, only the first index value is returned, or an exception is returned if not in the list.
⑤. Tests if a value is in list, the value returned using in:>>> ' C ' in Lis is false.

To delete an element in the list:

①.lis.remove (' a ') removes the first occurrence (not all) of a value from the list.
②.lis.pop () Pop () will do two things: delete the last element of the list and return the deleted element.
③.list can be linked with the + operator. List=list+otherlist is equivalent to List.extend (otherlist). The + operator, however, takes a new list (linked) as the return value, and extend () modifies only the existing list. So for a large list, Extend () executes faster.
The ④.list supports the + = operator.
The * operator in ⑤.list can act as a repeating device in list. Lis=[1,2]*3 equivalent to lis=[1,2]+[1,2]+[1,2]. That is to link three lists into one.

Tuple Introduction:

A tuple is a immutable list. Once a tuple is created, it cannot be changed in any way.
A simple definition of a tuple: tup= (' A ', ' B ', ' abc ', ' ASD ') enclose the entire element set in parentheses.
Because the tuple is immutable, it has no method and cannot add and remove elements. There are indexes like List, which can be partitioned in the same way. When a list is split, a new list is obtained, and when a tuple is split, a new tuple is obtained.
A tuple can also use in to see if an element exists in a tuple.

A tuple can be seen as a special list that has the benefit of:

①.tuple operates faster than list. Define a set of constants that only need to be stored with a tuple, and the only thing you can do with it is to constantly traverse it.
②.tuple can be seen as "write-protected" for data that does not need to be changed. You can make your code more secure.
③.tuple in dictionary can be used as key, and list is not possible! Because the key in the dictionary must be immutable.
④.tuple can be converted to a list. The built-in tuple function can receive a list that returns a Tupelo with the same element, while the list function receives a tuple to return a list.

An in-depth understanding of the python built-in data types described in this article can be of great help in mastering Python programming.

  • 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.