Indenting Code
We'll cover the following...
Python functions have no explicit begin
or end
, and no curly braces to mark where the function code starts and stops. The only delimiter is a colon (:) and the indentation of the code itself.
Press + to interact
def approximate_size(size, a_kilobyte_is_1024_bytes=True): #①if size < 0: #②raise ValueError('number must be non-negative') #③#④multiple = 1024 if a_kilobyte_is_1024_bytes else 1000for suffix in SUFFIXES[multiple]: #⑤size /= multipleif size < multiple:return '{0:.1f} {1}'.format(size, suffix)raise ValueError('number too large')
① Code blocks are defined by their indentation. By “code block,” I mean functions, if
statements, for
loops, while
loops, and so forth. Indenting starts a block and unindenting ends it. There are no ...