Today, let's take a look at recursion. The basic content is suitable for beginners!
1. Floor division and remainder
1. Floor division operator (floor division operator) by // That is, divide first, and then keep the result down to an integer. 2. Another way is to use the remainder operator (modulus operator)，% ，It divides the two numbers and returns the remainder.
2. Boolean expression
use ***== operator***. It compares two operands, If they are equal, the result is True ，Otherwise, the result is False .
>>>5==5 >True >>> 5==6 >Flase >#True and False are special values of type bool; They are not strings. >>> type ( True ) <class 'bool '> >>> type ( False ) <class 'bool '>
Other relational operators are
x != y # x is not equal to y x > y # x is greater than y x < y # x is less than y x >= y # x is greater than or equal to y x <= y # x is less than or equal to y
3. Logical operators
Three logical operators( local operators): and or not
>>> #The operand of a logical operator should be a Boolean expression >>> 42 is 45 False >>> 42 or 45 is 42 42 >>> 42 >= 45 False >>> 4s is 45 SyntaxError: invalid syntax >>> 42 is 45 False >>> 42 and Flase Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> 42 and Flase NameError: name 'Flase' is not defined >>> 42 and True True >>> not(42>45) True >>>
4. Conditional implementation
A statement header is followed by an indented statement body. Similar statements are
They are called compound statements.
>>> if x<0: pass
Only one of the two options will
These choices are called * * * branches***
>>> if x%2==0: print("even") else: print("odd")
>>> if x<y: print("small") ***elif*** x>y: print('d') else: print("a")
5. Nested conditions
A condition can be embedded in another page
The print statement is executed only when the two condition tests are passed, so we can use the * * * and operator * * *
As the name suggests: the function calls itself
Whenever a function is called, Python generates a * * * new stack frame to save the local variables and formal parameters of the function***
For a recursive function, there may be multiple stack frames * * * on the * * * stack at the same time.
7. Infinite recursion
Infinite recursion reports the following errors
Traceback (most recent call last): File "1.py", line 15, in <module> a() File "1.py", line 13, in a a() File "1.py", line 13, in a a() File "1.py", line 13, in a a() [Previous line repeated 1022 more times] RecursionError: maximum recursion depth exceeded
8. Keyboard input
Python provides a * * * built-in function input * * *, which can pause the program and wait for user input. When the user presses * * * return or enter * * *, the program resumes execution, and input returns what the user typed in the form of * * * string * * *. In
In Python 2, the name of this function is raw_input.
>>> a=input() assd >>> a 'assd' >>>
input accepts the * * * prompt as an argument***
>>> b=input("b=") b=45 >>> b '45' >>>
>>> c=input("c?\n") c? 43 >>>
Here is the reference \ n representing a new line
If you expect a number, try converting the return value to int
>>> int(c) 43 >>> type(c) <class 'str'> >>>
When syntax errors and runtime errors occur, the error message contains a lot of information, but the amount of information may be too large. Usually, the most useful parts are: • what kind of errors are they and • where they occur
Syntax errors are usually easy to find, but there are some things to pay attention to. White space separator errors are tricky because spaces and tabs are invisible and we are used to ignoring them.