Original
If statement
>>> x = Int (input ("Please enter a integer:")) Please enter an integer:42>>> if x < 0: ... x = 0 ... Print (' negative changed to zero ') ... elif x = = 0: ... Print (' Zero ') ... elif x = = 1: ... Print (' single ') ... else: Print (' More ') ... More
Used in place of other languages switch
or case
statements. The keyword ' elif
is an abbreviation of ' else if ', which effectively avoids excessive indentation.
For statement
The statements in Python are for
slightly different from the one you use in C or Pascal. Unlike in Pascal, which is always based on a linear numeric sequence iteration, it is also different from allowing the user to define both the iteration step and the termination condition in C, the statements in Python for
When iterating in a sequence (list or string), the elements are always iterated sequentially in the order in which they appear in the sequence.
It is unsafe to modify an object that is iterated during a loop (this can only occur on a mutable sequence type, such as a list).
If you want to modify the sequence of your iteration in the loop (for example, to copy a selected item in a sequence), it is best to make a copy with a slice.
>>> # Test Some strings: ... a = [' Cat ', ' window ', ' defenestrate ']>>> for x in a: ... Print (x, Len (x)) ... cat 3window 6defenestrate 12
Modify the sequence to make a copy first
>>> for X in a[:]: # Make a copy of a slice of the entire list ... If Len (x) > 6:a.insert (0, x) ...>>> a[' defenestrate ', ' Cat ', ' window ', ' defenestrate ']
Range Function Type
Generate arithmetical progression Columns
can accept 1, 2, or 3 parameters.
>>> for I in range (5): ... Print (i) ... 01234
Setting the starting position, length, and increment
Range (5, ten) 5 through 9range (0, 3) 0, 3, 6, 9range ( -10, -100, -30) -10, 40, 70
* You can also iterate over a list
>>> a = [' Mary ', ' had ', ' a ', ' little ', ' lamb ']>>> for I in range (Len (a)): ... Print (I, a[i]) ... 0 Mary1 had2 A3 little4 Lamb
Although the range function behaves like a list, it is actually not. If you iterate over it, it can return the required contiguous items, but in fact to save space it does not actually generate a list of manufacturing. This feature is called iterative (iterable)
>>> Print (range) range (0, 10)
Objects that have an iterative nature can be iterated by iterators. For example for
, the statement is the iterator ( iterator ). In addition, there is a list()
function; it generates a list from an iterative object:
>>> List (range (5)) [0, 1, 2, 3, 4]
ELSE clause in a loop
A looping statement can have a clause, which is executed when the else
loop is terminated by exhausting the entire list (used for
) or when the condition becomes false (used while
), but it will not be executed if the loop is break
terminated because the statement terminates.
When used with loops, the act of the Else clause and its collocation with the try statement have more commonality when compared to the IF statement: the ELSE clause of the TRY statement is executed when no exception occurs, and the ELSE clause of the loop is executed without a break statement.
>>> for N in range (2, ten): ... For x in range (2, N): ... If n% x = = 0: ... Print (n, ' equals ', X, ' * ', n//x) ... Break ... else: ... # Cycle stopped because no factor was found ... Print (n, ' is a prime number ') ... 2 is a prime number3 are a prime number4 equals 2 * is a prime number6 equals 2 * PNs is a prime Number8 equals 2 * $ eq Uals 3 * 3
(yes, this is the correct code.) Look closely: the ELSE clause belongs to the For loop, not the IF statement. The Else statement only executes when the most recent loop ends and the loop is not executed to break. )
Pass Statement
pass
The statement does nothing. You can use it when the syntax requires a statement, but the program does not move. For example:
>>> while True: ... Pass # Busy Waiting for keyboard interrupt (CTRL + C) ...
Can generally be used to create minimal classes
>>> class Myemptyclass: ... Pass ...
Another pass
place to use is that, as a placeholder for a function or condition body, when you work in new code, it allows you to keep thinking at a more abstract level. pass
silently ignored:
>>> def initlog (*args): ... Pass # Remember to realize this!...
Process Control statements in Python