Print vs return, Python Programming

Assignment Help:

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 ).

 


Related Discussions:- Print vs return

Re-writing a C++ code in Python with the help of ctypes, I have a C++ code ...

I have a C++ code (10-15 line) which i need to re-write in python with the help of ctypes library. Is it possible i can get some help with it? Thanks & regards Tanmoy

Non-local references in procedures, Non-local references in procedures ...

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

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

While loop, You should use for whenever you can, because  it creates  the s...

You should use for whenever you can, because  it creates  the structure of your  loops clear. Sometimes, however, you require to do an operation various times, but you don't want t

Angle between 2 moving line , how to calculate angle between 2 moving line ...

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

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

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

Euler method, python program of motion of a particle in viscus medium

python program of motion of a particle in viscus medium

Perimeter of a polygon, Perimeter of a polygon Now, let's consider the...

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

Environments in python, Environments in Python Generally, Python estab...

Environments in Python Generally, Python establishes the following binding environments: 1.  builtin     : the mother of all environments: it contains the de?nitions of

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