self-taught python in , Self study books are part of the Turing community 《 Programming x The first white book python Introductory book 》
When it comes to defining functions ,
Have said
ad locum , It is mentioned in the book that the statement after the function colon will automatically become a statement block (block)
there block It means that the statement is automatically indented after the function , Whole block Are all the statements in it functions ?
Yes . Carriage return after colon will automatically indent one space , As long as the input code is within the method , It belongs to the code block in the current method .
It can be understood as , The code on the right after the current method ( Non peer ) All belong to the current method .
A code block is a set of functions made up of code “ unit ”. A block of code can run alone . Like a function (function) Or a class (class) Definition 、 A module (module) All code blocks , for example :
def print_parity(x): if x % 2 == 0: print(x, "is even") else: print(x, "is odd")
This is a function definition (function definition), This function can be used to query the parity of an input number . This function is just a code block , Can operate independently .