If statement:Python uses If-elif-else to describe multiple branching decisions, simplifying nested problems in branching structures. There may be 0 to multiple elif parts, or else is optional. Keyword ELIF is the abbreviation for else if, which can effectively avoid deep indentation. If...elif....elif sequences are used to replace switch or case statements in other languages.
For example:
Print ("Enter an integer of (0-3):")
x = Int (input ())
if x = = 0:
print ("Enter number is 0")
elif x = = 1:
print ("number entered is 1")
elif x = = 2:
print ("entered number is 2")
elif x = = 3:
print ("Number of inputs 3")
program run Result:
Please enter an integer for (0-3):
2
the number entered is 2
For statement:
The For statement in Python is slightly different from c. Typically, loops may be based on a linear numerical step, or there is a user-defined iterative step and termination condition. The For statement for the Python iteration is based on the children in any sequence (list or string), in the order in which they are in the sequence.
>>> List1 = [1,2,3,4,5,6]
>>> for i in List1:
print (list1[i-1])
1
2
3
4
5
6
range () function:
If you need a numeric sequence, the built-in function range () is handy, and it generates a Kercha list. Range (5,10), which defines a arithmetic progression of 5 to 10. Range (0,10,2) defines a sequence of 0 to 10 increments of 2.
>>> for I in Range (1,10,2):
print (i)
1
3
5
7
9
Break and CONTINUE statements and ELSE clauses in loops:The break statement is similar to C in that it is used to jump out of the last level of the loop. The continre statement simply ends this loop, not ending the execution of the entire loop. A loop can have an else clause that executes when the loop iterates through the entire list (for a for) or when the execution condition is false (for a while), but the loop is aborted by a break.
For n in range (2,10): With
x in range (2,n):
if (n% 2 ==0):
print (n, multiples of 2) break
else:
print (N, "not multiples of 2")
#程序运行结果: 2 is not a multiple of 2, 3 is not a multiple of 2, 4 is a multiple of 2 is 5 is a multiple of 2 is 6 is a multiple 2
is
not
7 is a multiple of 2 is 8 is a multiple of 2 is a multiple of 9
Pass statement:
The pass statement does nothing. It is used for those words that are syntactically necessary, but where the program does nothing.
Pass, on the other hand, can be used as a placeholder for a function or control body when creating new code. Allows you to think at a more abstract level. Pass can be ignored silently. def statement:
Keyword DEF introduces the definition of a function. You must follow it with a function name and parentheses that include formal arguments. The function body statement begins on the next line and must be indented. function: The purpose of using functions: to reduce the difficulty of programming code to reuse the four steps performed by a function call: The calling program pauses the function at the call when the parameter is assigned to the argument at the time the function body function is called and the return value of the return value function is given: Return statement: The program exits the function and returns the value returned by the local return statement that the function was invoked to pass to the calling program the return value of the Python function is returned in two forms: Returns a value that returns a return statement with no returned value for multiple values, equivalent to return none The return value can be either a variable or an expression. Exception Handling:
Python uses try...except ... For exception handling, the basic format is as follows:
Try:
<body>
except <erroreype1>:
When the Python interpreter encounters a try statement, it attempts to execute the statement within the TRY statement
If there is no error, control is transferred to the statement following the try-except if an error occurs, the Python interpreter looks for an exception statement that matches the error, and then executes the processing code.