Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Chapter 3 Summary

Here are the main points of Chapter 3:

bull.jpg 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.

bull.jpg The syntax of a function definition has this form:

def function_name(arguments): 
    indented_statements

bull.jpg The arguments may be blank, may have one argument name, or may be a series of argument names separated by commas.

bull.jpg 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 (:).

bull.jpg 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.

bull.jpg 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)

bull.jpg 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)

bull.jpg To get numeric input, use an int or float conversion, as appropriate.

var = int(input(prompt_message))
var = float(input(prompt_message))

bull.jpg 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.

bull.jpg You can use a format-specification string, in which {} indicates a print field. Here’s an example:

fss = ’The square root of {} is {}.’

bull.jpg You can then apply the format method to a format specification string to produce an output string.

format_spec_string.format(arguments)

bull.jpg Here’s an example:

fss.format(25, 5)
  • + Share This
  • 🔖 Save To Your Account