Python basic data types-Python tutorial

Source: Internet
Author: User
Python provides the following basic data types: Boolean, integer, floating point, string, list, tuples, sets, dictionaries, etc. Python basic data types

1. null (None)
Indicates that the value is an empty object, and the null value is a special value in Python, which is expressed as None. None cannot be understood as 0, because 0 is meaningful, and None is a special null value.
2. Boolean)
In Python, None, 0 in any numeric type, empty string "", empty tuples (), empty list [], and empty dictionary {} are treated as False, there is also a custom type. if The _ nonzero _ () or _ len _ () method is implemented and the method returns 0 or False, the instance is also treated as False, all other objects are True.
The Boolean value is exactly the same as that of the Boolean algebra. a Boolean value can only be True or False, either True or False. in Python, you can use True or False to represent a Boolean value (case sensitive). You can also use a Boolean operation to calculate the value:

The code is as follows:

>>> TrueTrue>>> FalseFalse>>> 3 > 2True>>> 3 > 5False


Boolean values can also be calculated using and, or, and not.

1). The and operation is the same as the operation. only if all operations are True, and the operation result is True:

The code is as follows:

>>> True and TrueTrue>>> True and FalseFalse>>> False and FalseFalse


2). The or operation is an or operation. if either of them is True, the or operation returns True:

The code is as follows:

>>> True or TrueTrue>>> True or FalseTrue>>> False or FalseFalse


3) the not operation is not an operation. it is a single-object operator that converts True to False, and False to True:

The code is as follows:

>>> not TrueFalse>>> not FalseTrue


4). Boolean values are often used in condition judgment, for example:

The code is as follows:

if age >= 18:    print 'adult'else:    print 'teenager'

3. integer (Int)
In Python, integer processing is divided into common integers and long integers. the length of common integers is the length of the machine bit, usually 32 bits. integers beyond this range are automatically processed as long integers, the range of long integers is almost unlimited.
Python can process integers of any size, including negative integers. the representation method in the program is exactly the same as that in mathematics, such as 1,100,-8080,0.
4. Float)
A Python floating point number is a decimal point in mathematics, similar to a double in c.
In the operation, the result of integer and floating-point operations is a floating-point number.
A floating point is also a decimal point. it is called a floating point because the decimal point of a floating point is variable according to the scientific notation. for example, 1.23x109 and 12.3x108 are equal. Floating point numbers can be written in mathematics, such as 1.23, 3.14,-9.01, and so on. However, large or small floating point numbers must be represented by scientific notation, replacing 10 with e. 1.23x109 is 1.23e9, or 12.3e8, 0.000012 can be written as 1.2e-5.
Integers and floating-point numbers are stored in computers in different ways. integer operations are always accurate (is division accurate? Yes !), Floating-point operations may have rounding errors.
5. String)
Python strings can be enclosed in single or double quotation marks, or even three quotation marks.
A string is any text enclosed by ''or" ", such as 'ABC' and" xyz. Note that ''or" "is only a representation, not a part of the string. Therefore, the string 'ABC' contains only three characters: a, B, and c. If 'is also a character, it can be enclosed by "". for example, "I'm OK" contains the characters I,', m, space, O, k.

What if the string contains both 'and? It can be identified by escape characters \, for example:

The code is as follows:

'I\'m \"OK\"!'


The string content is:

The code is as follows:

I'm "OK"!


Escape character \ can escape many characters. for example, \ n represents a line break, \ t represents a tab, and the character \ itself also needs to be escaped \, you can use print to print strings in the Python interactive command line:

The code is as follows:

>>> print 'I\'m ok.'I'm ok.>>> print 'I\'m learning\nPython.'I'm learningPython.>>> print '\\\n\\'\\


If many characters in the string need to be escaped, you need to add a lot of \. for simplicity, Python also allows the use of R'' to indicate that the internal string is not escaped by default. you can try it yourself:

The code is as follows:

>>> print '\\\t\\'\       \>>> print r'\\\t\\'\\\t\\


If a string contains many line breaks, \ n is not easy to read in a line. to simplify it, Python allows '''... ''' format indicates multi-line content. try it by yourself:

The code is as follows:

>>> print '''line1... line2... line3'''line1line2line3


The above is input in the interactive command line. if it is written as a program, it is:

The code is as follows:

print '''line1line2line3'''


You can add r to the front of the multiline string '''... '''. Please test it yourself.
6. List)
The list is represented by the symbol []. the elements in the middle can be of any type and are separated by commas. List is similar to an array in C language and used for sequential storage structure.
Built-in functions:

The code is 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, other elements push backward, if I is greater than the length of the list, add it at the end. if I is less than 0, add remove (x) at the beginning to delete the first element with the value of x, if it does not exist, an exception will be thrown. the reverse () reverse sequence pop ([I]) returns and deletes the elements at the position I. by default, I is the last element index (x) returns the position where x appears for the first time in the List. If no position exists, an exception is thrown. count (x) returns the number of times that x appears. sort () sorting len (List) returns the length of the List. del list [I] deletes the list. the specified I + 1 variable slicing part refers to a part of the extraction sequence, in the form of list [start: end: step]. The extraction rule is: Generally, the default step is 1, but can also be customized.


7. Tuple)
The tuples are similar to the data structure of the list, but they cannot be changed once initialized. the speed is faster than the list. tuple does not provide dynamic memory management. you need to understand the rules:
Tuple can return an element or child tuple with subscript
The tuple method that contains only one element is: (d,) followed by a comma, used to distinguish it from a separate variable.
8. Set)
A set of unordered, non-repeated element sets. similar to a set in mathematics, logical and arithmetic operations can be performed.
9. dictionary (Dict)
A dictionary is an unordered storage structure, including the key and the value corresponding to the key ). The dictionary format is: dictionary = {key: value }. Key words are of an unchangeable type, such as strings, integers, tuples containing only unchangeable objects, and lists. If a keyword pair exists in the list, you can use dict () to construct a dictionary directly.

The above is a detailed description of the basic Python data types. For more information, see The PHP Chinese network (www.php1.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.