Standard data types
There are six standard data types in the Python3:
- Number (numeric)
- String (String)
- List (lists)
- Tuple (tuple)
- Set (SET)
- Dictionary (dictionary)
Among the six standard data types of Python3:
- Immutable data (3): Number, String (string), tuple (tuple);
- variable data (3): List, Dictionary (dictionary), set (collection).
# assigning values to multiple variables """ Python allows you to assign values to multiple variables at the same time """ = b = c = 1# above the instance, create an integer object with a value of 1, assign a value from backward forward, and three variables to the same memory address # Create multiple variables for multiple objects " /c10>runoob"
Number (numeric)
Python3 supports int, float, bool, complex (plural).
In Python 3, there is only one integer type int, represented as a long integer, and no long in Python2.
Like most languages, the assignment and calculation of numeric types is straightforward.
The built-in type () function can be used to query the object type that the variable refers to.
A, B, c, d = A, 5.5, True, 4+3jprint(type (a), type (b), type (c), type (d))<class'
int
'> <
class
'
float
'> <
class
'
bool
'> <
class
'
complex
'>
In addition, isinstance can be used to judge:
Isinstance (A, int)
The difference between isinstance and type is:
- Type () does not consider a subclass to be a parent class type.
- Isinstance () considers a subclass to be a parent class type.
Note: There is no Boolean in Python2, which represents False with the number 0 and 1 for True. To Python3, True and False are defined as keywords, but their values are still 1 and 0, and they can be added to the numbers.
Number operation
>>>5 + 4 # addition # subtraction 2.3>>> 3 * 7 # multiplication 21 >>> 2/4 # division, get a floating-point number 0.5>>> 2//4 # division, get an integer 0 # # exponentiation 32
Some tips:
- 1. Python can assign values to multiple variables at the same time, such as a, B = 1, 2.
- 2. A variable can point to different types of objects by assigning values.
- 3. The Division of numeric values contains two operators:/Returns a floating-point number,//returns an integer.
- 4. In mixed calculations, Python converts an integer to a floating-point number.
String (String)
The strings in Python are enclosed in single quotation marks or double quotes , with backslashes \ Escaping special characters.
The syntax for intercepting a string is as follows:
str ='Runoob' Print(str)#Output StringPrint(Str[0:-1])#outputs all characters from the first to the penultimatePrint(Str[0])#The first character of the output stringPrint(Str[2:5])#outputs the characters from the third start to the fifthPrint(str[2:])#output all characters from the beginning of the thirdPrint(STR * 2)#output String two timesPrint(str +"TEST")#Connection String
Operation Result:
Runoobrunoornoonoobrunoobrunoobrunoobtest
Python uses a backslash (\) to escape a special character, and if you do not want the backslash to escape, you can add an R to the string before it, representing the original string:
Print ('ru\noob') Ruoob Print (R'ru\noob') Ru\noob
In addition, a backslash (\) can be used as a continuation character, indicating that the next line is a continuance of the previous row. You can also use "" "," "" "" "" "or" "" ... "across multiple lines.
Note that Python does not have a separate character type, and one character is a string of length 1.
' Python ' Print (Word[0], word[5]) P n Print (Word[-1], word[-6]) n P
Unlike the C string, the Python string cannot be changed. Assigning a value to an index location, such as word[0] = ' m ', results in an error.
Attention:
- 1, the backslash can be used to escape, using R can make the backslash does not escape.
- 2. Strings can be concatenated with the + operator and repeated with the * operator.
- 3. The string in Python is indexed in two ways, starting at 0 from left to right and starting from right to left with-1.
- 4. The string in Python cannot be changed.
List (lists)
The list is the most frequently used data type in Python.
A list can accomplish the data structure implementation of most collection classes. The types of elements in a list can be different, it supports numbers, and strings can even contain lists (so-called nesting).
A list is a comma-delimited list of elements written between square brackets [].
As with strings, lists can also be indexed and truncated, and a new list containing the required elements is returned when the list is truncated.
The syntax format for the list interception is as follows:
The plus + is the list join operator, and the asterisk * is a repeating operation. The following example:
List = ['ABCD', 786, 2.23,'Runoob', 70.2]tinylist= [123,'Runoob'] Print(list)#Output Complete listPrint(List[0])#The first element of the output listPrint(List[1:3])#output from the second start to a third elementPrint(list[2:])#outputs all elements starting from the third elementPrint(Tinylist * 2)#output Two-time listPrint(List + tinylist)#Connection List
The result of the above example output:
['ABCD', 786, 2.23,'Runoob', 70.2]abcd[786, 2.23][2.23,'Runoob', 70.2][123,'Runoob', 123,'Runoob']['ABCD', 786, 2.23,'Runoob', 70.2, 123,'Runoob']
Unlike the Python string, the elements in the list can be changed!
The list has many methods, such as append (), Pop (), and so on, which is discussed later.
Attention:
- 1. The list is written between square brackets, and the elements are separated by commas.
- 2, like a string, a list can be indexed and sliced.
- 3, list can use the + operator for stitching.
- 4, the elements in the list can be changed.
Tuple (tuple)
Tuple (tuple) is similar to a list, except that the elements of a tuple cannot be modified. Tuples are written in parentheses (), and the elements are separated by commas.
The elements in a tuple can also be of different types:
Tuple = ('ABCD', 786, 2.23,'Runoob', 70.2) Tinytuple= (123,'Runoob') Print(tuple)#Output Full tuplePrint(Tuple[0])#the first element of an output tuplePrint(Tuple[1:3])#output starts from the second element to the third elementPrint(tuple[2:])#outputs all elements starting from the third elementPrint(Tinytuple * 2)#output two tuplesPrint(Tuple + tinytuple)#Connecting tuples
The result of the above example output:
('ABCD', 786, 2.23,'Runoob', 70.2) ABCD (786, 2.23)(2.23,'Runoob', 70.2)(123,'Runoob', 123,'Runoob')('ABCD', 786, 2.23,'Runoob', 70.2, 123,'Runoob')
Tuples are similar to strings, can be indexed, and subscript indexes start at 0, and 1 is where they start at the end of the list. It is also possible to intercept (see above, not repeat here).
In fact, a string can be thought of as a special tuple.
Tup = (1, 2, 3, 4, 5, 6)print(tup[0])1print(tup[1:5]) (2, 3, 4, 5= 1 1 # Modifying tuple element operations is illegal Traceback (most recent call last): "<stdin >" in <module>'tuples'not Support Item Assignment
Although the elements of a tuple cannot be changed, it can contain mutable objects, such as list lists.
Constructing tuples that contain 0 or 1 elements is special, so there are some additional syntax rules:
Tup1 = () # empty tuple # an element that needs to be added with a comma after the element
String, list, and tuple all belong to sequence (sequence).
Attention:
- 1. As with strings, elements of tuples cannot be modified.
- 2, tuples can also be indexed and sliced, the same way.
- 3. Note the special syntax rules that construct tuples that contain 0 or 1 elements.
- 4, tuples can also use the + operator for stitching.
Python 3 Basic Data type