Python basic data types are described in detail

  • 2020-04-02 13:27:58
  • OfStack

1. Empty (None)
Means that the value is an empty object, which 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
In Python, None, 0 in any numeric type, empty string "", empty tuple (), empty list [], empty dictionary {} are all treated as False, and custom types are also treated as False if methods are implemented with either 0 or False returned by methods
Boolean value and Boolean algebra representation is exactly the same, a Boolean value only True, False two values, either True or False, in Python, you can directly use True, False Boolean value (please pay attention to the case), can also be calculated by Boolean operation:

>>> True
True
>>> False
False
>>> 3 > 2
True
>>> 3 > 5
False

Boolean values can also be evaluated with and, or, and not.

1). And is the operation of and, and is True only if all of them are True:


>>> True and True
True
>>> True and False
False
>>> False and False
False

2). Or operation is or operation, as long as one of them is True, or operation result is True:

>>> True or True
True
>>> True or False
True
>>> False or False
False

3). Not operation is a non-operation, it is a single operator, to make True to False, False to True:
>>> not True
False
>>> not False
True

4). Boolean value is often used in conditional judgment, such as:

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

3. Int
In Python, the processing of integers is divided into ordinary integers and long integers. The length of ordinary integers is machine bit length, which is usually 32 bits. Integers beyond this range are automatically treated as long integers, and the range of long integers is almost completely unlimited
Python can handle integers of any size, including negative integers, in exactly the same way it is written mathematically, such as 1,100, -8080, 0, and so on.
4. Float
Python floating point Numbers are decimal Numbers in mathematics, similar to double in C.
In an operation, the result of an integer operation with a floating point number is a floating point number
Floating point Numbers are also known as decimal Numbers, so called because in scientific notation, the decimal position of a floating point number is variable. For example, 1.23x109 is the same as 12.3x108. Floating point Numbers can be written mathematically, such as 1.23, 3.14, -9.01, and so on. But for very large or very small floating point Numbers, you have to use scientific notation, replace 10 with e, 1.23x109 is 1.23e9, or 12.3e8, 0.000012 can be written as 1.2e-5, and so on.
Integers and floating point Numbers are stored in computers in different ways, and integer operations are always accurate. Yes!) Floating point arithmetic may have rounding errors.
5. String
Python strings can be enclosed in either single or double quotation marks, or even in triple quotation marks
A string is any text enclosed in '' or "", such as' ABC ', 'xyz', and so on. Note that '' or '' is itself a representation, not part of the string, so the string 'ABC' has only three characters: a, b, and c. If 'itself is a character, then you can enclose it with "". For example, "I'm OK" contains six characters: I,', m, space, O, and K.

What if the string contains both 'and'? Can be identified by an escape character \, such as:

'I'm "OK"!'

Represents the string content as:
I'm "OK"!

Escape character \ can escape many characters, such as \n for newline, \t for TAB, and character \ itself to escape, so \\ represents character \, you can print the string with print on the interactive command line in Python:
>>> print 'I'm ok.'
I'm ok.
>>> print 'I'm learningnPython.'
I'm learning
Python.
>>> print '\n\'


If there are a lot of characters in the string that need to be escaped, you need to add a lot of \. To simplify, Python also allows the internal string to be unescaped by default using "r". Try this yourself:
>>> print '\t\'
       
>>> print r'\t\'
\t\

If there are many new lines inside the string, it is not easy to read in one line with \n. The "" format represents multi-line content, you can try it yourself:
>>> print '''line1
... line2
... line3'''
line1
line2
line3

Above is the interactive command line input, if written as a program, is:
print '''line1
line2
line3'''

Multi-line string ""... "" can also be used in front of the r, please test yourself.
6. List
The symbol [] represents the list, and the middle elements can be of any type, separated by commas. A list is similar to an array in C and is used to store structures sequentially
Built-in function:
append(x) Append to the end of the chain 
extend(L) Appending a list is equivalent to +=
insert(i,x) In the position i insert x , the rest of the elements push back, if i Greater than the list length, just add at the end if i Less than 0 , at the very beginning 
remove(x) Delete the first value as x Element that throws an exception if it does not exist 
reverse() Reverse sequence 
pop([i]) Returns and deletes the position as i The elements, i The default is the last element 
index(x) return x The first place in the list that does not exist throws an exception 
count(x) return x Number of occurrences 
sort() The sorting 
len(List) return List The length of the 
del list[i] Delete the list list Specified in i+1 A variable 
 slice 
 Slice refers to the extraction of a part of the sequence in the form of: list[start:end:step] . The rule is: the default step size is 1 , but also customizable. 

7. Tuple
A tuple is a data structure similar to a list, but it cannot be changed once it is initialized, which is faster than a list. Meanwhile, tuple does not provide dynamic memory management.
A tuple can return an element or a child tuple with a subscript
The way to represent a tuple with only one element is (d,) followed by a comma to distinguish it from a separate variable
8. Set
A set is an unordered, unrepeating set of elements, similar to a set in mathematics, that can be used for both logical and arithmetic operations
9. Dict
A dictionary is an unordered storage structure that includes a key and its value. The format of the dictionary is: dictionary = {key:value}. Keywords are immutable types, such as strings, integers, tuples containing only immutable objects, lists, and so on. If keyword pairs exist in the list, dict() can be used to construct the dictionary directly


Related articles: