One, R's prevent character escaping
print R"a\nb"
Operation Result:
A\nb
Second, get the variable type
A = r"a\nb"print type (a)
Operation Result:
' Str '>
Iii. Types of Judgments
A = r"a\nb"print isinstance (A,STR)
Operation Result:
True
Iv. iterating over list, tuple, dict, set
4.1 Common ways
list = ['a','b','C'] for inch list: Print I
Dict = {"K1":"v1","K2":" v2"} for in Dict.items (): Print k,v
But this iterative approach will load the list into memory for iteration
4.2 Using iterators to iterate
list = ['a','b','C'] for inch iter (list): Print I
dict = { k1 : " v1 , " k2 : " v2 " for k,v in Dict.iteritems (): print k,v
Dict = {"K1":"v1","K2":" v2"} for in Dict.iterkeys (): Print K
The iterative comparison of this approach saves memory
Iteration subscript at the same time as the 4.3 iteration value
list = ['a','b','C'] for inch Enumerate (list): Print Index,value
V. List-Generated
Generate a list from a collection
List1 = ['a','b','C'= [ "printfor"inifand Y<3]
The operating result is:
['aa'bb'cc']
Vi. List Generator
With list generation, we can create a list directly. However, with memory limitations, the list capacity is certainly limited. Also, creating a list of 1 million elements takes up a lot of storage space, and if we just need to access the first few elements, the vast majority of the space behind it is wasted.
So, if the list element can be calculated according to an algorithm, can we continue to calculate the subsequent elements in the process of the loop? This eliminates the need to create a complete list, which saves a lot of space. In Python, this side loop computes the mechanism, called the generator: Generator.
As soon as a list-generated [] is changed to (), a generator is created:
g = (x * x for x in range (10))
Generator saves the algorithm, each time it calls next (g), calculates the value of the next element of G, until the last element is calculated, and when there are no more elements, the Stopiteration error is thrown.
But we usually iterate over it with a for loop, and we don't need to care about Stopiteration's error.
For N in G:
Print (n)
List-generated Python