python basis of automatic conversion and casts

A: automatic conversion

Note for automatic conversion Number of data types,

When two different types of data for computing, the more accurate the conversion default

Data from low to high precision type: bool int float complex

# Two values for type bool: True 1 False is converted to an integer is converted to integer 0
#bool + int
RES = True + 2
Print (RES)

# + a float bool
RES = 4.45 + True
Print (RES)

# BOOL + Complex
RES = False +. 5J
Print (RES)

# int + a float
# 3.14 15 to 18 exist in nature precision consuming because the decimal binary infinite loop without # terminates generally taken 15 to 18 in memory
res = 5 + 3.56
Print (RES)

# int + Complex
RES = 2 + (4-5j)
Print (RES)

#float + Complex
RES = 3.67 + (+ -5. 4J)
Print (RES)

II: cast

1.Number casts (Complex BOOL a float int)
Number The part:

int: Integer float Boolean strings of pure digital

float: int float boolean pure numeric string

complex: integer floating point Boolean pure numeric string (plural)

BOOL:    (container type data / Number of data types may be)


=. 5 var1
var2 = 4.85
var3 = True
var3_2 = False
var4. 3 = + 9j
var5 = "88777"
var6 = "dasa33"

(. 1) converted to integer int mandatory
RES = int (var2)
RES = int (var3) # = ture>. 1
RES = int (var3_2) #False => 0
#res = int (var4) error
RES = int (var5)
#res = int (var6) #error 
Print (RES)

(2) cast into a float float
RES = float (var1)
RES = float (var3)
RES = float (False)
# RES = float (var4) #error
RES = float (var5)
Print (RES)


(3) complex cast into a plurality
RES = Complex (var1)
RES = Complex (var2)
RES = Complex (var3)
RES = Complex (False)
RES = Complex (var5)
Print (RES)

(. 4) BOOL cast as a result there are only two or Boolean type Ture or False
BOOL can convert all data types everything

BOOL = RES (var1)
RES = BOOL (var2)
RES = BOOL (var6)
RES = BOOL (None)
Print (RES)


Boolean false ten cases (important):
0  

0.0  

0j  

False  

'' Empty string 

() Empty tuple

[] Empty list

set () empty set

} {Empty dictionary

None 
of what represents the # key None System does not generally used to initialize variables when using
RES = None
Print (None)

 

2. The container type data cast (str list tuple set dict)
container type portion

STR:   (container type data / Number of data types may be)

list:     list of strings tuples dictionary

tuple:    list of strings tuples dictionary

set:      a list of strings tuples dictionary (Note: The same value will only keep a copy)

dict:   the use of secondary or secondary list of tuples (two on the set of grammar is good, but the disorder is not recommended)

 

var1 = "sunny weather is good today,"
var2 = [1,2,3,4]
var3 = ( "Gai", "Huang Xiong," "Patrick")
var4 = { "Galen", "compression" "Juggernaut"}
var5 = { 'WFD': "flashed increase", "wer": "never die"}
var6 = 6J. 3 +

 (. 1) str All data types can be converted into the string str
law: based original type, the outside layer of quotation sleeve

RES = STR (var2)
Print (RES, type (RES))
RES = STR (var3)
RES = STR (var5)
RES = STR (var6)
Print (RES, type (RES ))
Print (the repr (RES)) # print out the character string type that marks <prototyping output>

 (2) list
rule:
If a string, mixed among the characters rub each character as a new element in the list
of other types are based on the original data on a set of [] to the identifier conversion

res = list (var1)
RES = list (var3)
when res = list (var5) # turn into the dictionary strong list, just get the key, ignore the value of
# list (var6) #error
Print (RES, of the type (RES))

(3) tuple 
law:
If a string, each character the characters rub mixed among the new elements in the list as
other types are based on the original data on a sleeve () can be converted identifier

RES = tuple (var1)
RES = tuple (var2)
RES = tuple (var5) # dictionary to turn into strong yuan group, only the key acquisition, the value is ignored
RES = tuple (var4)
Print (RES)

#Print ({1,2,3,4, "2323" , "111"})

(. 4) SET
rule:
If a string, mixed among the characters rub each character as a new element in the list
of other types are based on the original data on a set of {} for conversion set

res = set (var1)
when res = set (var5) # the dictionary strong turn into a set, only the capture key, ignore the value
Print (RES)

# remove duplicate list of elements among
listvar = [ "Jay", "Chow" "Stephen Chow", "Wang", "state toll"]
RES = the SET (listvar)
Print (RES)
RES = List (RES)
Print (RES)

# shorthand
RES = List (the SET (listvar))
Print (RES)

(5) cast into a dictionary
# multilevel container data: This data is the data container, and the container elements inside or data type

# ## the second container
# two listing

listvar = [1,2,3,4,5 , [6,7, 8,9]]
RES = listvar [-1] [. 3]
Print (listvar)
Print (RES)

# two yuan group
tuplevar = (1,2,3,4,5, (6, 7,8,9))
# two sets (tuples can only be put inside a container as a data set inside)
setvar = {l, 2,3, (4,5,6)}
#setvar = {1,2 , 3 (4,5,6, { 'a ': 1, 'b': 2})} #error hash must be immutable data
# setvar = {1,2,3, [4,5 , . 6]} #error

Print (setvar)
# two dictionaries
dictvar = { "a": { "C":}. 3, "B": 2}
RES dictvar = [ "a"]
Print (RES)
# a-phase output value corresponding to { "c": 3}

Print (RES [ 'c'])
# output value corresponding to c 3, a value belonging to the sub-dictionary

# shorthand
print (dictvar [ "a"] [ 'c'])

# Four container
= Content [1,2,3,4, (4,5, { 'A': {4,5,6,88}, "B": 2},. 7),. 5]
Print (Content [. 4])
Print (Content [. 4] [2])

# Gets set
Print (Content [. 4] [2] [ 'a'])

# isometric secondary containment
first container 2 is representative of two containers
each container element a the same numbers are called the isometric

listvar = [(1,2,3,4), (5,6,7,8)]
tuplevar = ([1,2], [3,4-], [5,6 ])

# ##  dict cast element into a dictionary which only :( 2)
(1) is outside the list, which is a list or tuple or string
'' 'only the number of string elements is 2 a has its limitations, not recommended '' '
' '' [[ "a", [1,2,3,4]]] '' '
listvar = [[' a ',. 1], (' B ', 2), "C3"] # C33
RES = dict (listvar)
Print (RES, type (RES))

# special considerations:
RES = dict ([[ "A", [1,2,3,4]] , ( "b", [5,6,6 ,. 7])])
Print (RES, "<============>")
'' '
# produce unexpected results, not recommended
listvar = [{ 'C',. 1}, { 'D', 2}]
RES = dict (listvar)
Print (RES, type (RES))
'' '

(2) the outside is a tuple, which is a list or meta group or string
'' can only be a number of string elements 2, has its limitations, not recommended '' '
tuplevar = ([ "C",. 3], ( "D", 33 is), "F2")
RES = dict (tuplevar)
Print (RES, type (RES))

 (. 3) are set out, there is a tuple or string
setvar = {( 'a', 1), ( 'b', 2 )}
RES = dict (setvar)
Print (RES, type (RES))

summary:
Strong turn about the container type data of a total of the following:
STR () List () SET () tuple () dict () can be converted to the corresponding data type
on the Number data type strong turn total follows:
int () BOOL () a float () Complex () can be converted into a corresponding number of types of data
may be initialized with the value of a function of a strong turn
RES = STR ()
Print (the repr (RES))
RES = BOOL ()
Print (RES)

Print ( '--- ----------------- ')
#TypeError: unhashable type: 'List'
#res = SET ((1,2,3,4, [4,5,6,6,2], {3,4,5,6}))
#Print (RES )

# tuples can not change its son, but can change its grandson
all elements # tuples of the first stage can not be modified, but if the element is a list, by the list itself, you can modify its own value
tup = (1, 2,3, [4,5, (9,2, [99,88],. 1),. 6],} {11,22,3)
#tup [3] = 90 # tuples can not be modified
#tup [3 ] [2] [- 1] = # 18 in the inner layer is a tuple element can not be modified

Print (TUP)
#Print (TUP [. 3])
#tup [. 3] [- 1] = # tuple. 7 the list of values can be modified
#Print (TUP)  
(2) values and a set of keys among the dictionaries are required, must be immutable data hash value of the dictionary is not required
when performing the type of container ships # strong rotation, said is a tuple list set dict special string comparison are not included

# Strong turn into a dictionary that:

(1) as long as the second container

(2) the inner elements inside a container can only be 2

Guess you like

Origin blog.csdn.net/qq_24036403/article/details/90726785