2nd Python Basic Syntax-data type

Source: Internet
Author: User

2.2 Data types

The value that the variable is stored in memory. This means that there is a space in memory when creating variables. Based on the data type of the variable, the interpreter allocates the specified memory and determines what data can be stored in memory.

2.2.1 Standard data types

There are several types of data that can be stored in memory. Python has five standard data types:

    • Numbers (digital)
    • String (String)
    • List (lists)
    • Tuple (tuple)
    • Dictionary (dictionary)
2.2.2 Number Types

Numeric data types are used to store numeric values, which are immutable data types, which means that changing the numeric data type assigns a new object. When you specify a value, the number object is created. You can also use the DEL statement to delete some object references.

The syntax for the DEL statement is:

Del Var1[,var2[,var3[....,varn] []]

Python supports four different numeric types:

    • int (signed integral type)
    • Long (longer integer [can also represent octal and hexadecimal])
    • Float (float type)
    • Complex (plural)

Long integers can also use lowercase "l", but it is recommended that you use uppercase "L" to avoid confusion with the number "1". Python uses "L" to display the long integer type.

Python also supports complex numbers, which consist of real and imaginary parts, can be represented by a + BJ, or complex (a, b), and the real and imaginary parts of a complex number are floating point types

2.2.3 Python string

A string or series (string) is a string of characters consisting of numbers, letters, and underscores. It is the data type that represents the text in the programming language.

The Python string list has 2 order of values:

    • Left-to-right index starts at default 0, with a maximum range of 1 less string lengths
    • Right-to-left index starts with default-1, the maximum range is the beginning of the string

If you want to get a substring, you can use the variable [head subscript: Tail subscript], you can intercept the corresponding string, where the subscript is starting from 0, can be positive or negative, subscript can be null to take the head or tail. Like what:

s = ' Ilovepython '

S[1:5] The result is love.

When using a colon-delimited string, Python returns a new object that contains the contiguous content identified with the offset, and the beginning of the left contains the bottom bounds. The result above contains the value L of s[1], and the maximum range taken does not include the upper boundary, or the value p of s[5]. The plus sign (+) is a string join operator, and an asterisk (*) is a repeating operation.

2.2.4 Python List

The list is the most frequently used data type in Python. A list can accomplish the data structure implementation of most collection classes. It supports characters, numbers, and even strings that can contain lists (so-called nesting). The list is identified by []. The list of values can also be used to split the variable [head subscript: Tail subscript], you can intercept the corresponding list, from left to right index default 0, starting from right to left index default-1, subscript can be empty to take the head or tail. The plus sign (+) is the list join operator, and the asterisk (*) is a repeating operation. The following example:

2.2.5 python tuples

A tuple is another data type, similar to a list. The tuple is identified with a "()". The inner elements are separated by commas. However, an element cannot be assigned two times, which is equivalent to a read-only list.

2.2.6 Python meta Dictionary

The Dictionary (dictionary) is the most flexible built-in data structure type in Python, except for lists. A list is an ordered combination of objects, and a dictionary is a collection of unordered objects. The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets. The dictionary is identified with "{}". A dictionary consists of an index (key) and a value corresponding to it.

2.3 Data Type Conversions

Sometimes, we need to convert the data-built type into the data type, and you just need to use the data type as the function name. The following several built-in functions can perform conversions between data types. These functions return a new object that represents the value of the transformation.

2nd Python Basic Syntax-data type

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.