Python built-in data types

Source: Internet
Author: User

Python built-in data types

Positioning of Python in programming languages:

Script Language-scriptinglanguage

Advanced Dynamic Programming Language.


Python is dominated by data, and variable value change refers to a variable pointing to an address.

Id (variable) -- "shows the variable address.


A specific value has different variable names ..


Python data type:

Numbers, strings, lists, tuples, and dictionaries


Numbers and strings are actually very basic data types, which are not very different from other languages in Python. I will not elaborate on them here.


  1. Dictionary Introduction

    Dictionary is one of Python's built-in data types. It defines the one-to-one relationship between keys and values.

    It is actually a hash array.

    Simple definition of Dictionary: Dic = {'key1': 'value1', 'key2': 'value2 '}

    A Dictionary cannot contain duplicate keys. assigning values to the same Key overwrites the original values.

    You can add a Key-Value pair to the Dictionary at any time.


    There is no element order in Dictionary! There is no order for finding the Value through the Key.

    In Dictionary, the Key is case sensitive!


    Dictionary is not just used to store strings. The value of a Dictionary can be of any data type, including the Dictionary itself. At the same time, in a single Dictionary, the values of the Dictionary do not need to all be of the same data type and can be mixed and matched. The data type of a Dictionary Key has relative requirements, but it can be mixed and matched by multiple data types.


    Del can use a Key to delete the corresponding element in the Dictionary. >>> Del dic [12]

    Clear () can delete all values in a Dictionary, but the original Dictionary is still there, but it becomes null {}. >>> dic. clear ()


    1. List Introduction

      List is the most frequently used data type in Python.

      The data types in the List can be arbitrary and dynamic expansion is supported.

      Simple definition of List: Lis = ['A', 'B', 'abc', 'asd ']. List is a set of ordered elements enclosed by square brackets.

      List supports two index modes: normal index mode, starting from 0.

      Negative indexes are counted from the end of the List. The last original of any non-empty List is always List [-1].


      List supports sharding, which is used to retrieve data from the List. Note the starting position of Slice.


      Add data to the List: Lis. Append ('new') add data to the end of the List;

      Lis. insert (2, 'new') inserts a value at the position of List 2;

      Lis. extend (['new', 'nwe']) links to a New List (at the end) in the original List ).


      Differences between Append () and Extend:

      The Append () parameter can be of any data type, but can also be a List, but it regards how List as an element and adds it to the original List.

      The parameter of Extend () can only be a List, and all elements of the entire List are added to the original List one by one.


      Search in List: Lis. index ('A ')

      Index searches for a value in the List and returns its first index value. If it appears multiple times, only the first index value is returned. If it is not in the List, an exception is returned.

      Test whether a value is In the List. The value returned by using in: >>> 'C' In lis is False.


      Delete all elements in the List:

      Lis. remove ('A') first appearance of deleting a value from the List (not all ).

      Lis. 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 ). However, the + operator uses a new list (after link) as the return value, while extend () only modifies the existing List. Therefore, for large lists, the execution speed of Extend () is faster.

      List supports the + = Operator.

      * The operator can act on the List as a repeat. Lis = [1, 2] * 3 is equivalent to lis = [1, 2] + [1, 2] +. That is, link the three lists into one.


      1. Tuple Introduction

        Tuple is an unchangeable List. Once a Tuple is created, it cannot be changed in any way.

        Simple definition of Tuple: Tup = ('A', 'B', 'abc', 'asd ') the entire element set is surrounded by parentheses.

        Because Tuple is immutable, there is no way to add or delete elements. You can partition indexes in the same way as List indexes. When a List is split, a new List is obtained. When a Tuple is split, a new Tuple is obtained.

        Tuple can also use the in method to check whether an element exists in Tuple.


        Tuple can be considered as a special List, which has the following advantages:

        Tuple is faster than List operations. To define a constant set, you only need to use Tuple for storage. Your unique operation on it is to continuously traverse it.

        Tuple can be seen as "Write protection" for data that does not need to be changed ". This makes the code more secure.

        Tuple can be used as the Key in Dictionary, while List cannot! Because the key in Dictionary must be unchangeable.


        Tuple can be converted to List. The built-in tuple function can receive a List and return a Tupelo with the same elements. The List function receives a Tuple and returns a List.



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