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

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

Create a program to produce a business speak phrase, The goal of this exerc...

The goal of this exercise is to write a \business phrase" generator, which each time it is called produces lines of business speak, such as: It's time that we became uber-effici

Three layer neural network to classify the two sets of 3-D d, 1. Use a thre...

1. Use a three layer neural network to classify the two sets of 3-D data set. You should generate 100 samples for each data set based on the following criteria: Data set 1: generat

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

Bank transfer, Bank transfer What  if we  have  two  values,  represen...

Bank transfer What  if we  have  two  values,  representing bank  accounts, and  need  to transfer an  amount of money  amt between them?  Consider that a bank account is show

Lab programming, protocol rdt2.2 considers there is a bit errors between se...

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

Top-down approach, what are the issues assciated with top-down analysis

what are the issues assciated with top-down analysis

Programming embedded systems- interact with the environment, Interacting wi...

Interacting with the environment Computer systems have  to communicate with  the world around them,  getting information about  the external world, and  taking  actions  to cha

Non-local references, Non-local references So far, whenever we needed ...

Non-local references So far, whenever we needed to compute a variable, there  was  a binding for that  variable in the 'local' environment (the environment in which  we were e

Python program, Write a program that asks the user to enter a number of sec...

Write a program that asks the user to enter a number of seconds. The responses of the program will vary depending on the length of seconds: • If the number of seconds is under 60

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