Print vs return, Python Programming

Print vs Return

Here are two different method declarations:

def f1(x):

print x + 1 def f2(x):

return x + 1

What happens when  we call them?

>>> f1(3)

4

>>> f2(3)

4

 

It looks like they behave in exactly the similar way.  But they don't, exactly.  Look at this example:

 

>>> print(f1(3))

4

None

>>> print(f2(3))

4

 

In the case of f1, the function, when  calculated, prints 4; then  it gives the value  None, which  is given by the Python file. In the case of f2, it does not give  anything, but it gives 4, which  is printed by the Python core. Finally, we may look the difference here:

 

>>> f1(3) + 1

4

Traceback (most recent call last):

File "", line 1, in ?

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

>>> f2(3) + 1

5

 

In the ?rst case, the function does not give a value,  so there  is nothing to include  to 1, and an error is created. In the second  type, the function gives the value  4, which  is included to 1, and  the result,  5, is output by the Python read-eval-print loop.

Print  is very needful  for debugging. It is very important to know  that you may print  out as many  values as you want  in one line:

 

>>> x = 100

>>> print 'x', x, 'x squared', x*x, 'xiv', 14

x 100 x squared 10000 xiv 14

 

We have also snuck in another data type on you: strings. A string  is a sequence of words. You can build a string  using  single or double quotes;  and access individual elements of strings using indexing.

 

>>> s1 = 'hello world'

>>> s2 = "hello world"

>>> s1 == s2

True

>>> s1[3]

'l'

 

As you  may  see,  indexing indicates  to the  extraction of a particular element of a string,  by using square brackets [i] where i is a number that  identi?es the  location  of the  character that  you wish to extract (note that the indexing starts  with 0 ).

 

Posted Date: 8/9/2012 7:01:08 AM | Location : United States







Related Discussions:- Print vs return, Assignment Help, Ask Question on Print vs return, Get Answer, Expert's Help, Print vs return Discussions

Write discussion on Print vs return
Your posts are moderated
Related Questions
Write an algorithm for a program that shows the use of all 6 math functions. Write, test, and debug the program using Python. SAMPLE OUTPUT (not including author/program infor

Simple expressions A cornerstone of a programming language is the  ability  to compute expressions.  We will start here  with  arithmetic expressions, just to take the  concep

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

Finally! After years of adding unimportant sports, the IOC has finally added Shoe Tying to the Olympics. And you’ve been selected to write the software to handle the judging of t

Range Very frequently, we will want to iterate through a list of integer values, often as indices.  Python gives a useful function, range, which gives lists of integers. It ma

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

Perimeter of a polygon Now, let's consider the problem of computing the length  of the perimeter of a polygon. The input is a structure of vertices,  encoded as a list of list

Common Errors and Messages Here are some common Python errors  and error  messages to look  out for. Please let us give if you have any favorite additions for this list.

how to calculate standard deviations in programming

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