Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Python Expert

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

 

Python, Programming

  • Category:- Python
  • Reference No.:- M9505998

Have any Question?


Related Questions in Python

Tasksdemonstrate data scraping of a social network of

Tasks Demonstrate data scraping of a social network of choice. Develop technical documentation, including the development of the code & detailing the results. Provide a report on the findings, that includes research into ...

Question write a simple python program that takes use

Question: Write a simple python program that takes use inputs as non-zero digits and converts them into binary form. The response must be typed, single spaced, must be in times new roman font (size 12) and must follow th ...

A software company sells a package that retails for 99

A software company sells a package that retails for $99. Quantity discounts are given according to the following table: Quantity Discount 10 - 19 20% 20 - 49 30% 50 - 99 40% 100 or more 50% Write a program using python t ...

Project reconnaissance and attack on ics

Project: Reconnaissance and Attack on ICS NetworksEnvironment Setup The second mini project will be based on Industrial Network Protocols, specifically the Modbus protocol. Please follow the instructions carefully to set ...

The second task in this assignment is to create a python

The second task in this assignment is to create a Python program called pancakes.py that will determine the final order of a stack of pancakes after a series of flips.(PYTHON 3) Problem Task In this problem, your input w ...

Question why is software configuration management

Question : Why is software configuration management considered an umbrella activity in software engineering? Please include examples and supporting discussion. The response must be typed, single spaced, must be in times ...

Architecture and system integrationcase study queensland

Architecture and System Integration Case Study: Queensland Health - eHealth Investment Strategy After evaluating various platforms, Queensland Health finally decided to adopt a Service Oriented Architecture (SOA) for its ...

Below zero - ice cream storethe local ice-cream store needs

Below Zero - ice cream store The local ice-cream store needs a new ordering system to improve customer service by streamlining the ordering process. The manager of the store has found that many orders are incorrect and s ...

Question write a python program with a graphical user

Question: Write a python program with a graphical user interface that will allow a user to create a custom pizza which they wish to order. At minimum, the user should be able to choose the size of the pizza, the type of ...

Python programming assignment -you first need an abstract

Python Programming Assignment - You first need an abstract base class, called, Account which has the following attributes and methods: accountID: This attribute holds the ID assigned the account , if not provided set to ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As