python-7- data structure and type conversion

Foreword

In addition to the aforesaid python base type, here we need to explain the data structure, the data structure is stored inside the base type, such as numbers, etc. also can be nested.

  • Immutable data (3): Number The (digital), String (String), Tuple (tuple);
  • Variable data (3): List (list), Dictionary (dictionary), Set (collection).

A data structure

1, list the list, the symbol: [xxx]

# List list 
List1 = [. 1, ' XL ' , [. 1, 2 ]]
 Print ( ' list: ' , type (List1))

 2, tuple tuple, read-only, and can not be written to modify

# Tuple of tuples, and modified non-writable read only 
tuple1 = (. 1, 2, ' XL ' , { " SAD " : 2 })
 Print ( ' tuple: ' , type (tuple1))

 3, dict dictionary, key-value pairs

# Dict dictionary key-value pair 
dict1 = { " name " : " XL " , " Age " : [{ " name " : 123 }]}
 Print ( ' dictionary: ' , type (dict1))

 4, set collection

# SET set 
setl = { ' XL ' , ' tiger ' , 123 }
 Print ( ' set: ' , type (setl))

Second, the type conversion

We have many types of conversion scenarios used, such as input when the input is a string, we have to convert to other types.

1、int --> str

# 1、int --> str
i = 1
s = str(i)
print(type(s))

 2、str --> int,纯数字才可以转换

# 2、str --> int,纯数字才可以转换
q = '1'
w = int(q)
print(type(w))

 3、int --> bool, 非0就是 True

# 3、int --> bool, 非0就是 True
e = -1
b = bool(e)
print(type(b))
print(b)

 4、bool --> int

# 4、bool --> int
# True --> 1
# False --> 0

5、str --> bool,非空字符串都是 True

# 5、str --> bool,非空字符串都是 True
# s = '' --> False
# s = 'xx' --> True

QQ交流群:99941785

Guess you like

Origin www.cnblogs.com/gsxl/p/11938764.html