Python basic data types detailed description

Source: Internet
Author: User

The basic data types provided by Python are: boolean type, Integer, float, string, list, tuple, collection, dictionary, and so on.

1, Empty (None)

Indicates that the value is an empty object, and the null value is a special value in Python, denoted by none. None cannot be understood as 0, because 0 is meaningful, and none is a special null value.

2. Boolean Type (Boolean)

In Python, none, 0 in any numeric type, empty string "", Empty tuple (), empty list [], empty dictionary {} are treated as false, and custom types, if the nonzero () or Len () method is implemented and the method returns 0 or FALSE, Then its instance is also treated as false, the other objects are true, the Boolean value is exactly the same as the representation of the Boolean algebra, a Boolean value that is true, false two, or true, or false, in Python, which can be used directly with true, False indicates a Boolean value (note case), or it can be computed by a Boolean operation:
Copy the code code as follows:

1 >>> true2true3 >>> False4  False5 >>> 3 > 26True7 >>> 3 > 58 False

Boolean values can also be operated with and, or, and not.
1). And operations are associated with operations, and only the result of all True,and operations is true:
Copy the code code as follows:

1  and True 2 True 3  and False 4 False 5  and False 6 False

2). The or operation is or operation, as long as one of the true,or operation results is true:
Copy the code code as follows:

1 or True 2 True 3 or False 4 True 5 or False 6 False

3). The not operation is a non-operation, it is a single-mesh operator that turns true into False,false true:
Copy the code code as follows:

1  not True 2 False 3  not False 4 True

4). Boolean values are often used in conditional judgments, such as:
Copy the code code as follows:

1 if age >=:2     print ('adult')3 Else : 4     print ('teenager')
3, Integral type (INT)

In Python, the processing of integers is divided into ordinary integers and long integers, the ordinary integer length is the machine bit long, usually 32 bits, more than this range of integers automatically when long integer processing, and the range of long integers almost completely unrestricted python can handle any size of integers, of course, including negative integers, The representation method in the program is exactly the same as the mathematical notation, for example: 1,100,-8080,0, and so on.

4. Float type (float)

Python's floating-point number is a decimal number in mathematics, similar to a double in the C language.
In operations, the result of integer and floating-point arithmetic is floating-point number, floating-point number is a decimal, the reason is called floating-point number, because in the scientific notation, the decimal point position of a floating-point is variable, for example, 1.23x109 and 12.3x108 are equal. Floating-point numbers can be written in mathematical notation, such as 1.23,3.14,-9.01, and so on. But for very large or very small floating-point numbers, it must be expressed in scientific notation, the 10 is replaced with E, 1.23x109 is 1.23e9, or 12.3e8 (also can be written 12.3e+8), 0.000012 can be written 1.2e-5, and so on.
Integers and floating-point numbers are stored inside the computer in different ways, and integer operations are always accurate (is division accurate?). Yes! ), and the floating-point operation may have rounding errors.

5. Strings (String)

Python strings can be enclosed in single quotation marks (' single quotes ') or in double quotation marks ("double quotes"), and can even be enclosed in three quotation marks ("" "" "", "" "" "" "" "), and the string is any text enclosed in" or ", such as ' abc ', ' XYZ ', etc. Note that the "or" "itself is only a representation, not part of a string, so the string ' abc ' only a,b,c these 3 characters. If ' (single quotation marks) itself is also a character, then you can use "" (double quotation marks), such as "I m OK" contains the characters are I, ', M, space, o,k these 6 characters.
What if the inside of a string contains both ' and contains '? You can use the escape character \ To identify, for example:
Copy the code code as follows:

1 ' i\ ' m \ "Ok\"! '

The string content represented is:
Copy the code code as follows:

1 I'm "OK"!

Escape character \ Can escape many characters, such as \ n for newline, \ t for tab, character \ itself to escape, so \ means the character is \, you can print a string in Python's interactive command line to see:
Copy the code code as follows:

1>>>Print('i\ ' m OK.')2I'm OK.3>>>Print ('i\ ' m learning\npython.')4I'M Learning5 Python.6>>>Print ('\\\n\\')7 8\

If there are many characters in the string that need to be escaped, you need to add a lot of \, in order to simplify, Python also allows the use of R "means" inside the string is not escaped by default, you can try it yourself:
Copy the code code as follows:

1 Print ('\\\t\\') 2 \        3 print (r'\\\t\\') 4 \\\t\\

If there is a lot of line wrapping inside the string, writing in a row is not good to read, in order to simplify, Python allows the "' ..." format to represent multiple lines of content, you can try it yourself:
Copy the code code as follows:

1 Print ("'line12...line23...line3") 4 line1 5 line2 6 Line3
6. Lists (list)

The list is represented by the symbol [], and the middle element can be any type, separated by commas. List is similar to an array in C, which is an intrinsic function of the sequential storage structure:
Copy the code code as follows:

1Append (x)#Append to end of chain2Extend (L)#append a list, equivalent to + =3Insert (I,X)#in position i insert X, the remaining elements are pushed backwards, if I is greater than the list length, it is added at the end, and if I is less than 0, it is added at the very beginning4Remove (x)#removes the first element with a value of X and throws an exception if it does not exist5Reverse ()#Reverse Sequence6Pop ([i])#returns and deletes the element with position I, and I defaults to the last element7Index (x)#returns the position of the first occurrence of x in the list, and throws an exception if it does not exist8Count (x)#returns the number of occurrences of x9Sort ()#SortTenLen (List)#returns the length of the list One delList[i]#Delete the i+1 variable specified in list

Slice: A slice refers to a part of the extraction sequence in the form of: List[start:end:step]. The rule is that the default step size is 1, but it can also be customized.

7, tuple (tuple)

A tuple is a data structure similar to a list, but it cannot be changed once it is initialized, faster than a list, and the tuple does not provide dynamic memory management capabilities, so you need to understand the rules:
A tuple can use the subscript to return an element or a sub-tuple
The method that represents a tuple that contains only one element is: (d,) followed by a comma, which is used to distinguish it from a separate variable

8. Set (SET)

Collections are unordered, non-repeating sets of elements, similar to mathematical collections, basic functions include relationship testing and de-duplication elements, and collection objects also support Union (union), intersection (intersection), Difference (poor), and sysmmetric difference ( symmetric differential set), which allows for logical and arithmetic operations.

The following is a small experiment where the code is copied as follows:

1>>> x = Set ('span')2>>> y = set (['h','a','N'])3>>>x, y4({'s','a','P','N'}, {'a','h','N'})5>>> X & Y#intersection6{'a','N'}7>>> x | Y#and set8{'s','h','N','a','P'}9>>> X-y#Difference SetTen{'s','P'} One>>> X^y#Symmetric difference Sets A{'h','s','P'}

There is such a problem: ask how to remove the huge list of repeating elements, with hash to solve the line, but the performance is not very high, with set to solve or very good, examples are as follows:

1 >>> a = [11,22,33,44,11,22]2 >>> b = Set (a)3 >>& Gt b 4 {5 for in b]6 >>> C  7 [33, 11, 44, 22]
9. Dictionary (Dict)

A dictionary is an unordered storage structure that includes a keyword (key) and a value that corresponds to a keyword (value). The format of the dictionary is: dictionary = {Key:value}. A keyword is an immutable type, such as a string, an integer, a tuple that contains only immutable objects, and a list that cannot be used as a keyword. If there is a keyword pair in the list, you can construct the dictionary directly with Dict ().

Python basic data types detailed description

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.