How do you show Python output

We can show Python output in many ways. In other words, there are many ways that we can adopt to show the output in Python.

In this section, we will see all the alleys and backstreets to display the output.

Firstly, let us clear a basic point in mathematics and programming. 

A function primarily handles two things.

Input and output. Right?

We have already seen that Python gives us ample opportunities to handle many problems.

There are many built-in functions available.

Just like other built-in functions, print() is a function and that gives us output. 

On the other hand, we use the input() function to take the inputs. Although we are not going to discuss input() here.


If you are a complete beginner your journey to learn TensorFlow might start from here.

For the TensorFlow beginners we have a dedicated category – TensorFlow for Beginners.

But besides that, you may need to learn several other machine learning and data science libraries.

As a result, you may check these categories as well – NumPy, Pandas, Matplotlib.

However, without learning Python, you cannot learn the usages of these libraries. Why? Because they all use Python as the Programming language.

Therefore please learn Python at the very beginning and start learning TensorFlow.

And, finally please check our Mathematics, Discrete Mathematics and Data Structures categories specially. We have tried to discuss from basic to intermediate level so that you can pick up the core ideas of TensorFlow.


Let’s concentrate on the output section. There are many actions which are waiting for us.

By the way, in this section, we are going to discuss only the output on the screen, right? 

Certainly we can give the output data to a file. 

We will discuss it later.

Here goes the first example.

name = 'John'
print('The name is', name)

# output
The name is John

In this print() statement, we noticed one thing that we should understand.

Python by default added a space between the string and the value.

If we don’t like a separator like comma (,), we can write this way.

print(f'The name is {name}')

# output
The name is John

We can also use the format() function. But it looks clumsy. 

Because in the previous example we have used the ‘f’ letter to do the same thing in a better way.

print('The name is {}'.format(name))

# output
The name is John

As we saw, the curly braces ‘{ }’ acts as the placeholder.

Now we can pass the variable name as parameter.

print('The name is {name} '.format(name = 'Json'))

# output
The name is Json 

Python’s ancestor the C programming language formats the string with the sprintf() method.

We can emulate that in Python too.

Let’s show some examples where we can give output to several data types in a different format.

name = 'John'
print('The name is %s' %name) # string
# output
The name is John

a_number = 20364
print('The name is %2d' %a_number) # decimal number
# output
The name is 20364

a_float = 200.36987
print('The number is %2.1f' %a_float) # flaoting point number with length value 2.1 f
print('The number is %.3f' %a_float) # flaoting point number with length value 2.3 f
# output
The number is 200.4
The number is 200.370

We have used the ‘%’ operator to accomplish the mission.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

Flutter, Dart, Python and TensorFlow

C, C++ Java, and Game Development

Twitter


Posted

in

,

by

Comments

Leave a Reply