Name = Raw_input (' Please enter your name: ') or ' <unknown> '
In other words, if the return value of the Raw_input statement is true (not an empty string), then his value is assigned to name, otherwise the default ' <unknown> ' is assigned to name.
This type of short-circuit logic can be used to implement so-called ' ternary operators '(or conditional operators) in C and Java.
A If B else c
If B is true, return a, otherwise, return C. (Note that this operator can be used directly without introducing a temporary variable to get the same result as in the raw_input example.) )
While loop
Name = ' '
While not name: # instead of Name.strip (): Is it more reasonable?!
Name = Raw_input (' Please enter your name: ')
print ' Hello,%s! '%name
This simple while loop, let the input name, but did not consider the name is a space case, is a blank although not error, you do not know the name Ah ah ah, hey
For loop
words = [' This ', ' was ', ' an ', ' ex ', ' Parrot ']
For word in words:
Print Word
Because iterations (another way of saying) a range of numbers are common, there is a built-in scope function for use:
>>> Range (0,10) #话说此函数又下限 (i.e. 0), but does not contain the upper limit ()
[0,1,2,3,4,5,6,7,8,9]
_____________________________________________________
Tip: If you can use a for loop, try not to use a while loop.
And of course there is a xrange function that xrange function is more efficient when iterating over a large sequence, but generally not.
Some iterative tools:
Parallel iterations
names = [' Anne ', ' Beth ', ' George ', ' Damon ']
ages = [12, 34,23,103]
If you want to print the name and the corresponding age, you can do the following:
For I in range (len (names)):
Print Names[i], ' is ', ages[i], ' years '
The built-in zip function can be used for parallel iterations, you can ' compress ' two sequences together, and then return a list of tuples:
>>>zip (names, ages)
[(' Anne ', '), (' Beth ', '), (' George ', '), (' Damon ', 103)]
Now I can unpack the tuple in the loop:
For name, age in Zip (names, ages):
Print name, ' is ', age, ' years '
The ZIP function can also be used for any number of sequences. The important thing about it is that the zip can handle unequal sequences, which stop when the shortest sequence is "exhausted".
For example # (Let me flirt, hehe)
>>>zip (Range (5), xrange (10000000))
[(0,0), (2,2), (3,3), (bis)]
In the preceding code, using range to replace xrange--is not recommended, although only the first 5 digits are required, but the range calculates all the numbers, which can take a long time.
There is no problem with using xrange, it only calculates the first 5 digits.
For index, string in Enumerate (strings):
If ' xxx ' in string:
String[index] = ' [censored] '
Enumerate this function iterates over "index-value" pairs where the index is provided.
Else clause in a loop
From math import sqrt
For n in range (99,81,-1):
root = sqrt (n)
if root = = Int (root):
Print n
Break
Else
print ' didnt find it! '
List derivation--lightweight loops
List comprehension is a method of creating a new list (like a set deduction in mathematical terms) with other lists.
>>>[x*x for x in range (10)]
[0,1,4,9,16,25,36,49,64,81]
>>>[x*x for x in range (ten) if x%3 = = 0]
[0,9,36,81]
You can also add more parts of the FOR statement:
>>>[(x, y) for x in range (3) for Y in range (3)]
[(0,0), (0,1), (0,2), (1,0), (a), ...]
In contrast, the following code creates the same list with two for statements:
result = []
For x in range (3):
For y in range (3):
Result.append ((x, y))
Pass, Del, exec
Reading notes--python Basic Tutorial 001