Five standard data types based on python and five major python Data Types

Source: Internet
Author: User

Five standard data types based on python and five major python Data Types

Learning a language often begins with Hello World. However, I believe that output a "Hello, world" in a black box is nothing remarkable. To understand the essence of things and to be familiar with a language, we need to understand its underlying layer, this is what we often say. This article starts with the variable type in python.

Five Standard Data Types

Data stored in the memory can be of multiple types.

For example, a person's name can be stored with characters, ages can be stored with numbers, hobbies can be stored with collections, and so on.

Python has five standard data types:

  • Numbers (number)
  • String (String)
  • List)
  • Tuple (tuples)
  • Dictionary)

The data types that belong to the set type include:List, tuples, and dictionaries.

0x00. Number (Numbers)

The numeric data type is used to store numeric values.

They are unchangeable data types, which means that changing the numeric data type will allocate a new object.

When you specify a value, the Number object will be created:

var1 = 1var2 = 2

The del statement deletes some object references. Its syntax is:

del var1[,var2[,var3[....,varN]]]]

You can use the del statement to delete the reference of one or more objects. For example:

del var1del var1, var2

Four different numeric types:

  • Int (signed integer)
  • Long (long integer [can also represent octal and hexadecimal])
  • Float (float type)
  • Complex (plural)

A. int (integer)

On a 32-bit machine, the number of digits of an integer is 32 bits and the value range is-2 ** 31 ~ 2 ** 31-1, I .e.-2147483648 ~ 2147483647
In a 64-bit system, the number of digits of an integer is 64-bit, and the value range is-2 ** 63 ~ 2 ** 63-1, that is,-9223372036854775808 ~ 9223372036854775807

B. long (long integer)
Different from the C language, the Length Integer of Python does not specify the Bit Width. That is, Python does not limit the size of the Length Integer. However, due to limited machine memory, the Length Integer cannot be infinitely large.
Note: Since Python2.2, if an integer overflows, Python will automatically convert the integer data to a long integer. Therefore, without the letter L after the long integer data, it will not cause serious consequences.

C. float (float type)

Floating point numbers are used to process real numbers, that is, numbers with decimals. Similar to the double type in C, it occupies 8 bytes (64-bit), where 52 bits represent the bottom, 11 bits represent the index, and the remaining one represent the symbol.
D. complex (plural)
A complex number is composed of a real number and a virtual number. Generally, x + yj is used. x is the real number of a complex number, and y is the virtual number of a complex number. Here, x and y are both real numbers.

Note: there is a small number pool in Python:-5 ~ 257

Small integer object -- small integer Object pool

In actual programming, integers such as 1, 2, and 29 may appear very frequently. In python, all objects exist on the system stack. Think about it? If a small integer appears frequently, a large number of malloc/free operations will occur in Python, which greatly reduces the running efficiency and causes a large amount of memory fragments, seriously affects the overall performance of Python.

In Python2.5 or even 3.3, the number of small integers located between [-5,257) is cached in the small integer Object pool.

 

0x01. String (String)

A String is a String of numbers, letters, and underscores.

It is the data type that represents text in programming languages.

The Python string list has two values:

  • From left to right index starts from 0 by default, and the maximum range is 1 less String Length
  • From right to left index start from-1 by default. The maximum range is the start of the string.

If you want to obtain a substring from a string, you can use the variable[Head Subscript: tail subscript]To extract the corresponding string. The subscript starts from 0 and can be a positive or negative number. The subscript can be null to indicate that the header or the end is obtained.

For example:

s = 'i love python'

The result of s [2: 6] is love. (Ignore the tail, or close the left and right open)

Operation instance:

Str = 'Hello world' print (str) # print (str [0]) # print (str [2: 5]), the first character in the output string # print (str [2:]) character between the third and fifth output strings # print (str * 2) Output string from the third to the last) # print the output string twice ('say: '+ str) # output the connected string

  

0x02. List)

List is the most frequently used data type in Python.

The list can implement the data structure of most collection classes. It supports characters, numbers, strings, and even lists (so-called nesting ).

Operation instance:

List = ['apple', 'jack', 798, 2.22, 36] otherlist = [123, 'xiaohong '] print (list) # print (list [0]) of the output complete list # print (list []) of the first element in the output list # print (list [2:]) of the second to third elements in the output list # print (otherlist * 2) of all elements from the third to the end of the output list) # print (list + otherlist) Output list twice # output splicing list

 

0x03. Ancestor (Tuple)

Tuples are another data type, similar to List ).

The tuples are identified. Internal elements are separated by commas. However, tuples cannot be assigned values twice, which is equivalent to read-only lists.

The operation instance is similar to the list

 

0x04. Dictionary (Dictionary)

A dictionary is the most flexible built-in data structure type in Python except the list. A list is a combination of ordered 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, rather than by offset.

The dictionary is identified. A dictionary consists of an index (key) and its corresponding value.

Operation instance:

Dict = {} dict ['one'] = 'this is one' dict [2] = 'this is two' tinydict = {'name': 'john ', 'code': 5762, 'dept': 'sales'} print (dict ['one']) # print (dict [2]) of the output key 'one' # print (tinydict) # output the complete dictionary print (tinydict. keys () # print (tinydict. values () # output all values

 

To be continued, the next article describes seven operators based on python.

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.