print('before')
print()
print('after')before
after
A function is a named piece of reusable code that you invoke by writing its name followed by parentheses. Values passed inside the parentheses are called arguments. You must always include the parentheses — even when calling a function with no arguments — so Python knows you are making a call rather than referencing the function object itself.
print takes zero or more arguments and displays them on a single line, separated by spaces. With no arguments it prints a blank line:
len takes exactly one argument and returns the number of items in it. int, str, and float each take one argument and return a converted value.
max and min return the largest and smallest values from a set of arguments. They work on both numbers and strings (strings are compared character by character using Unicode order):
round rounds a floating-point number. By default it rounds to zero decimal places; pass a second argument to specify the precision:
Functions enforce their requirements. max and min need at least one argument, and the arguments must be mutually comparable — mixing incompatible types raises a TypeError:
Every function call produces a result. If a function has no meaningful value to return it returns the special value None. print is a common example — its job is the side effect of writing to the screen, not producing a value:
When you call functions inside other expressions, nested calls are evaluated from the inside out, just as in mathematics: max(len('tin'), len('copper')) first computes the two lengths, then finds the maximum.
Python checks your code for syntax errors — structural problems that make the program impossible to parse — before running a single line. A forgotten closing quote, a misplaced =, or an unclosed parenthesis will produce a SyntaxError with a ^ pointing at approximately where the problem is:
Cell In[7], line 1 name = 'Feng ^ SyntaxError: unterminated string literal (detected at line 1)
Runtime errors only appear when the problematic line actually executes. A misspelled variable name raises a NameError:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[9], line 2 1 age = 53 ----> 2 remaining = 100 - aege # mis-spelled 'age' NameError: name 'aege' is not defined
Fix syntax errors by reading the source code carefully around the ^ marker; fix runtime errors by tracing what value each variable holds when the error line is reached.
For detailed guidance on finding answers when you are stuck, see the Getting Help lesson.
radiance?1.1 * radiance → 1.11.1 - 0.5 → 0.6min(1.0, 0.6) → 0.62.0 + 0.6 → 2.6max(2.1, 2.6) → 2.6radiance = 2.6print statement below will output.max(len(rich), poor) run or raise an error? If it runs, does the result make sense?c
tin
4
c
tin
4
max(len(rich), poor) raises a TypeError — it tries to compare the integer 4 with the string 'tin', which Python cannot order.Why don’t max and min return None when given no arguments?
Returning None silently would hide a programming mistake — the caller would store None, use it later, and get a confusing error far from the actual problem. Raising a TypeError immediately points you directly to the bug.
If Python indexes from zero and len returns the number of characters, what index expression retrieves the last character of name?
name[len(name) - 1]
(A simpler form using negative indexing — name[-1] — is covered in the Variables and Assignment lesson.)
#) to document the why behind non-obvious code.max, min, and round are commonly-used built-in functions.None.SyntaxError before running the program; a runtime error only appears when the bad line executes.
Comments document your code
A
#character tells Python to ignore the rest of the line. Use comments to explain why your code does something when the reason is not obvious from reading it: