Procedures, Python Programming

Assignment Help:

 

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.

 


Related Discussions:- Procedures

Coding examples of python, Coding examples Following are some  attempts...

Coding examples Following are some  attempts at defining a function isSubset,  which  takes  two  arguments, a and  b, and  returns True if a is a subset  of b, assuming that

#title.display of vanderwaals isotherm, draw the variation of pressure with...

draw the variation of pressure with volume of a real gas at temperatures lower than its critical temperature.also draw its surface of discontinuity

Basics of python-introduction, Basics of python-introduction Python is...

Basics of python-introduction Python is designed for easy interaction between a user and the system. It goes with  an inter­ active function  known as  a shell or listener.  T

Program to calculate area function, Rewrite the area.py program (shown bel...

Rewrite the area.py program (shown below, or in the Creating Functions section of the tutorial) so that it has separate functions for the perimeter and area of a square, a rectangl

File handling , A program to count how many files are on the file system wh...

A program to count how many files are on the file system which displays summary information regarding the total number of bytes used by all files and a breakdown of the number of b

Stuck on homework, There are three seating categories at a stadium. For a s...

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

Procedures in python, Procedures in python Procedures are computer pro...

Procedures in python Procedures are computer program creates that let us capture common patterns of computation by: Grouping together sequences of statements

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd