Python basic data types detailed description

Source: Internet
Author: User
python Basic data types detailed description

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, The instance is also treated as false, and the other objects are true
The Boolean value is exactly the same as the Boolean algebra, with a Boolean value of only true, false two, or true, or false, in Python, which can be used to indicate a Boolean value directly with True, false (note case), or by Boolean operations:

The code is as follows:

>>> truetrue>>> falsefalse>>> 3 > 2true>>> 3 > 5False


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:

The code is as follows:

>>> true and truetrue>>> true and falsefalse>>> False and Falsefalse


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

The code is as follows:

>>> true or truetrue>>> true or falsetrue>>> False or Falsefalse


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

The code is as follows:

>>> not truefalse>>> not falsetrue


4). Boolean values are often used in conditional judgments, such as:

The code is as follows:

If age >=:    print ' adult ' else:    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, the integer over this range automatically when the long integer processing, and the range of long integers is almost completely unlimited
Python can handle integers of any size, including, of course, negative integers, which are represented in the program in the same way as mathematically, for example: 1,100,-8080,0, etc.
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 an integer and floating-point operation is a floating-point number
Floating-point numbers, which are decimals, are called floating-point numbers because, when represented by scientific notation, the decimal position of a floating-point number 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,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 quotes or double quotes, or even enclosed in three quotation marks.
strings are arbitrary text enclosed in ' or ', such as ' abc ', ' XYZ ', and so on. Note that the ' or ' itself is only a representation, not a part of the string, so the string ' abc ' only a,b,c these 3 characters. If ' itself is also a character, then you can use "", 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:

The code is as follows:

' I\ ' m \ ' ok\ '! '


The string content represented is:

The code is as follows:

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:

>>> print ' i\ ' m ok. ' I ' m ok.>>> print ' I\ ' m learning\npython. ' i ' m learningpython.>>> print ' \ \ \n\\ ' \ \


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 to use R ' ' to indicate that the inside of the string is not escaped by default, you can try it yourself:

Copy the Code code as follows:

>>> print ' \\\t\\ ' \       \>>> print R ' \\\t\\ ' \\\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:

>>> print "Line1 ... line2 ... line3" Line1line2line3


The above is entered in the interactive command line, if written as a program, that is:

Copy the Code code as follows:

print ' Line1line2line3 '


Multi-line string ' ... ' can also be used in front with R, please test yourself.
6. Lists (list)
The list is represented by the symbol [], and the middle element can be any type, separated by commas. List similar to array in C, for sequential storage structure
Built-in functions:

Copy the Code code as follows:

Append (x) append to the end of the chain extend (L) append a list, equivalent to +=insert (i,x) in position i insert X, the rest of the elements are pushed backwards, if I is greater than the list length, at the end of the add, if I is less than 0, the first to add the Remove (x) Delete the first element with the value x if it does not exist will throw an exception reverse () reverse sequence pops ([i]) return and delete the element with position I, I default to the last element index (x) returns x where it first appears in the list, does not exist then throws an exception count (x) Returns the number of occurrences of x sort () Sort Len (list) returns the length of List del list[i] Delete list the i+1 variable slice slice specified in the lists refers to the part of the extraction sequence, in the form: 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, like collections in mathematics, that can be performed in logical and arithmetic operations
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 ()

The above is the Python basic data type detailed description of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

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