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 that receives as input from the user, Write an algorithm for a pro...

Write an algorithm for a program that receives, as input from the user, 2 string variables and 2 integer variables; then joins together and displays the combined strings; and final

Interaction and debugging, We encourage you to adopt an interactive style o...

We encourage you to adopt an interactive style of debugging and programming. Use the Python shell a lot. Write short pieces of code and check them.  It is much  simpler to test the

Program to solve word search puzzles, This assignment involves writing a pr...

This assignment involves writing a program to solve word search puzzles. For example in the following word grid it is possible to find the words active, stock, ethernet and java. N

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

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

Indentation and line breaks, Every programming language has to have  some m...

Every programming language has to have  some method for indicating grouping of operations. Here is how you execute an if-then-else structure in Java:   if (s == 1){ s = s

Cps translator for python, Your task is to construct a translator from the ...

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

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

Algorithm, given number is prime number or not

given number is prime number or not

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