Procedures, Python Programming

 

In Python, the fundamental abstraction of a computation is as a procedure (other  books call them "functions" instead; we will end  up  using  both  values).   A function that  takes  a number as an argument and returns the argument value  plus 1 is de?ned as:

 

def f(x):

return x + 1

The indentation is important here, too. All of the instructions of the procedure have to be indented one level below  the def.  It is important  to remind the return statement at the end,  if you want your  method to give a value.  So, if you described f as above,  then  operated with  it in the shell,4 you may  get something like that:

 

>>> f

>>> f(4)

5

>>> f(f(f(4)))

7

 

If we just evaluate f, Python tells us it is a method. Then we can give it to 4 and get 5, or give it multiple times, as given.

What if we de?ne

 

def g(x):

x + 1

Now,  when  we play with it, we might  get something like this:

>>> g(4)

>>> g(g(4))

Traceback (most recent call last):

File "", line 1, in ? File "", line 2, in g

 

TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

 

What happened!! First, when  we calculated g(4), we take nothing at all, because  our definition of g did not give anything. Well...strictly speaking, it given a special number known as None, which the file does  not bother printing out.  The number  None has a special  type,  known  NoneType.   So, then,  when  we tried  to give g to the  answer  of g(4),  it ended up  trying  to calculate g(None), which  made  it try to evaluate None + 1, which  made  it complain that  it did  not know  how  to add  something of type NoneType and something of type int.

 

Posted Date: 8/9/2012 7:00:30 AM | Location : United States







Related Discussions:- Procedures, Assignment Help, Ask Question on Procedures, Get Answer, Expert's Help, Procedures Discussions

Write discussion on Procedures
Your posts are moderated
Related Questions
Write a Python program to accomplish the following. Use modular design. Include at least 3 functions: one that returns zero values, one that returns one value, and one that retu

Assume you have a truck which has to travel across a desert from the base camp at position 0 (left) to the target camp at position 4 (right). The intermediate positions 1,2, and 3

Your task is to construct a translator from the subset of Python in Project 3 intocontinuation-passing style (CPS). Of course, the expected way of accomplishing this is to trans

There are three seating categories at a stadium. For a softball game, Class A seats cost $15, Class B seats cost $12, and Class C seats cost $9. Write a program that asks how many

how to calculate angle between 2 moving line in 2d space

protocol rdt2.2 considers there is a bit errors between sender to receiver and also from receiver to sender. So now we have to consider checking bit errors introduced in reply from

De?nition   A method de?nition has the abstract form: def ( , ..., ): ...   There are essentially three parts: is a name for

how to make a python programme for van der waals equation of state with surface discontinouty

Worked example 1   Let's examine what  happens when  we compute the following Python code:   def p(x, y): z = x*x - y return z + 1/z   >>> p(1.0, 2.0) -2.0

Non-local references in procedures There  is an important subtlety in the  way  names  are handled in the  environment created by a function call. When  a value  that  is not