Chapter 3 Summary
Here are the main points of Chapter 3:
A function definition lets you perform a series of calculations over and over, without having to reenter all the steps in number crunching. At least this is a simple way to understand the concept.
The syntax of a function definition has this form:
def function_name(arguments):
indented_statements
The arguments may be blank, may have one argument name, or may be a series of argument names separated by commas.
If you enter the function-definition heading correctly, the Python interactive environment automatically creates indentation. Remember that a correct function-definition heading ends with a colon (:).
From within the Python interactive environment, you complete a function definition by typing an extra blank line after you’ve entered all the indented statements.
To call a function, enter the name of the function followed by parentheses and argument values. These values are then passed to the function-definition code. Here’s an example:
>>>convert(10)
You can prompt the user for string input by using the input statement. The prompt message is a string printed on the console to prompt the user for input.
string_var = input(prompt_message)
To get numeric input, use an int or float conversion, as appropriate.
var = int(input(prompt_message))
var = float(input(prompt_message))
The built-in print function prints all its arguments in order. By default, arguments are printed with a single blank space separating them. You can use the optional sep argument to specify another separator character. sep=’’ specifies that no separator character should be used.
You can use a format-specification string, in which {} indicates a print field. Here’s an example:
fss = ’The square root of {} is {}.’
You can then apply the format method to a format specification string to produce an output string.
format_spec_string.format(arguments)
Here’s an example:
fss.format(25, 5)