Python3 basic data type conversion, python3 Data Type
'''
Basic Data Type
1: although the variables in python do not need to be declared, they must be assigned a value during use.
1. Integer
2. Floating Point Variables
3. Balanced
2: You can assign values to multiple variables, or assign values to multiple variables.
3: there are 6 standard data types in python3
* Number)
* True = 1
* False = 0
* Division (/) always returns a floating point number. To obtain an integer, use the // operator.
* In mixed computing, python converts an integer to a floating point number.
* String (String)
* The string is enclosed by 'or ", and the \ escape special string is used at the same time.
* If you do not want to escape the backslash, you can add an r before the string to indicate the original string.
* The index value starts with 0 and-1 is the starting position at the end.
* The plus sign + is the connector of a string. The asterisk (*) indicates the number of times the current string is copied.
* List)
* List is written between square brackets. elements are separated by commas.
* Like strings, a list can be indexed and sliced.
* List can be connected using the + operator.
* The elements in the list can be changed.
* Tuple (tuples)
* Tuples are similar to lists. The difference is that elements of tuples cannot be modified, and tuples are written in parentheses. Elements are separated by commas (,).
* Tuples can also be indexed and sliced in the same way.
* Note that special syntax rules are used to construct tuples containing zero or one element.
* Tuples can also be spliced using the + operator.
* Sets)
* Set is a sequence that does not need to be repeated. Its basic function is to test the member relationship and delete repeated elements.
Dictionary)
* A dictionary is a ing type, which is identified by {}. It is an unordered set of key: value pairs.
* The key must use an unchangeable type. The key must be unique in the same dictionary.
* Create an empty dictionary {}
4: conversions between types
* Int (x, base = 10) x string or number, base number, converted to integer by default in decimal floating point
* Float Integer Conversion to float type
* Complex () is converted to a plural number.
* Str (10) converts an object to a string.
* Repe () converts an object to an expression string
* Repr (dict) converts an object to an expression string
* Eval (str) is used to calculate a valid python expression in a string and return an object.
* Tuple (listi) converts a list to a tuples
* List () converts tuples to lists.
* Set conversion set
'''
Print ('------------------ 1 ----------------') a = 100 # integer variable B = 100.0 # Floating Point variable c = 'zifuxing' # string print (a, B, c) print ('--------------------- 2 ----------------') a = B = c = 1 print (a, B, c) a, B, c = 1, 2, 3 print (a, B, c) print ('-------------------- 3 -------------------') print ('Number number') a, B, c = 20, 5.5, true # type can be used to query the data types indicated by the variable print (type (a), type (B), type (c) # You can also use isinstance to determine # type () does not consider subclass as a parent class type # isinstance () considers subclass as a parent class type print ('string string') str1 = 'zifuchuanc' print (str1 [0: -1]) # output the first to the second print (str1 [0]) # output the first string print (str1 [2: 5]) # output print (str1 [2:]) from the third to fifth string # output print (str1 * 2) of all strings after the third string) # print the output string twice (str1 + 'test') # Link string print ('LIST') listp = ['abc', 768, 2.33, 'lifeciao ', 70.2] tinylist = [123, 'lifeciao'] print (listp) # print the complete output list (listp [0]) # print the first element in the output list (listp []) # print (listp [2:]) from the second element to the third element # print (tinylist * 2) of all elements starting with the third element) # print (listp + tinylist) # link two lists # Element a = [1, 2, 4, 4, 5, 6] a [0] = 9a [2: 5] = [13, 14, 5] a [2: 5] = [] # You can delete the specified print ('tuple tuples, usage is the same as above, but it cannot be modified. ') print ('set set') student = {'Tom', 'Jim ', 'Mary', 'Tom ', 'Jack ', 'Rose '} print (student) # output set. duplicate elements are automatically removed if 'Rose' in student: print ('Rose in set') else: print ('Rose is not in the set') # set can perform the set operation a = set ('aba') B = set ('alac') print () # set can be repeated, so the output is a, B, rprint (a-B) # The difference between a and B is print (a | B) # a and B, print (a & B) # print (a ^ B) of the intersection of a and B # print ('dictionary Dictionary ') of Elements a and B that do not exist simultaneously ') tinydict = {'name': 'runoob', 'code': '1', 'SITE': 'www .runoob.com '} print (tinydict) # output the complete dictionary print (tinydict. keys () # output all build print (tinydict. values () # print ('---- data type conversion --------') print (int (3.6) # convert a floating point to an integer print (float (1 )) # integer to floating point print (float ('20140901') # string to floating point print (complex (123) # integer to print (complex ('1 ')) # The string is negative dict = {'runoob': 'runoob. com ', 'Google': 'goole. com '} print (str (dict) I = int (10) print (str (I) print (repr (dict )) x = 7 print (eval ('3 * x') # You can operate on the string listi = ['Google ', 'taobao', 'runoob ', 'baidu'] print (tuple (listi) tpo = tuple (listi) t = ('1', '2', '3') print (list (t )) print (tpo) x = set ('runoob') y = set ('Google ') print (x, y)