The five standard data types of Python basics

Source: Internet
Author: User

Learning a language is often the beginning of Hello World. But I think, in a black box output a "Hello, the World" and nothing great, to see through the nature of things, familiar with a language, it is necessary to understand the underlying, is what we often say the basis. This article starts with the variable type in Python.

Five standard data types

There are several types of data that can be stored in memory.

For example, a person's name can be stored in characters, age can be stored in numbers, hobbies can be used to store the collection and so on.

Python has five standard data types:

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

Data types that belong to a collection type have lists, tuples, and dictionaries .

0x00. Number (Numbers)

Numeric data types are used to store numeric values.

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

var1 = 1VAR2 = 2

The DEL statement removes references to some objects whose syntax is:

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

You can delete a reference to a single or multiple objects by using the DEL statement. For example:

Del Var1del var1, VAR2

four different types of numbers :

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

A. Int (integral type)

On a 32-bit machine, the number of integers is 32 bits and the value range is -2**31~2**31-1, which is -2147483648~2147483647
On a 64-bit system, the number of integers is 64 bits and the value range is -2**63~2**63-1, which is -9223372036854775808~9223372036854775807

B. Long (L-integer)
Unlike the C language, Python's long integers do not refer to the positioning width, that is, Python does not limit the size of long integer values, but in fact, because of limited machine memory, long integer values cannot be infinitely large.
Note that, since Python2.2, Python automatically converts integer data to long integers if an integer overflows, so it does not cause any serious consequences if you do not add the letter L after long integer data.

C. Float (floating point type)

A floating-point number is used to process real numbers, which are numbers with decimals. Similar to the double type in C, accounting for 8 bytes (64 bits), where 52 bits represent the bottom, 11 bits represent the exponent, and the remaining one represents the symbol.
D. complex (plural)
The complex number consists of real and imaginary parts, the general form is X+yj, where x is the real part of the complex, and Y is the imaginary part of the complex, where x and y are real numbers.

Note: Small number pools exist in Python:-5 ~ 257

Small integer Object--Small integral object pool

In practical programming, a small number of integers, such as 1,2,29, may appear very frequently. In Python, all objects exist on the system heap. Think about it? If a small integer appears in very large number of occurrences, Python will have a lot of malloc/free operations, which greatly reduces the efficiency of the operation, but also causes a lot of memory fragmentation, which seriously affects the overall performance of Python.

In Python2.5 and even 3.3, small integers are placed in the number between [-5,257] and the small integer object pool is slow.

0x01. Strings (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 implement a string to get a string, you can use the variable [header 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 = ' I love python '

S[2:6] The result is love. (Gu Tou regardless of tail, or left close right open)

Operation Example:

str = ' Hello world ' Print (str)                 #输出完整字符串print (str[0])              #输出字符串中的第一个字符print (Str[2:5])            # The character print (str[2:]) between the third and fifth in the output string             #输出从第三个开始到最后的字符串print (str*2)               #输出字符串两次print (' say: ' + str)       #输出连接的字符串

  

0x02. Lists (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).

Operation Example:

List =  [' Apple ', ' Jack ', 798, 2.22, 36]otherlist = [123, ' Xiaohong ']print (list)                             #输出完整列表print (list[0])                          # Output list first element print (List[1:3])                        #输出列表第二个至第三个元素 print (list[2:])                         #输出列表第三个开始至末尾的所有元素print (otherlist * 2)                    # Output list two times print (list + otherlist)                 #输出拼接列表

0x03. Ganso (Tuple)

A tuple is another data type, similar to a list.

The tuple is identified with a "()". The inner elements are separated by commas. However, tuples cannot be assigned two times, which is equivalent to a read-only list.

Action instances are similar to lists

0x04. Dictionary (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.

Operation Example:

Dict = {}dict[' one '] = ' This was one ' dict[2] = ' This is the ' and ' tinydict = {' name ': ' John ', ' Code ': 5762, ' dept ': ' Sales '}
Print (dict[' one ") #输出键为 the value of ' one ' print (dict[2]) #输出键为2的值print (tinydict) #输出完整的字典print (Tinydict.keys ( )) #输出所有键print (Tinydict.values ()) #输出所有值

To be continued, next, seven operators of the Python Foundation

The five standard data types of Python basics

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.