print() Function

an illustration of the print() function which takes inputs, formats them, and displays them

In this post, I will give you a brief introduction on built-in functions in Python. Then, we will get into more details about a very useful function, the print() function. The notebook for this post is available here.

Programming functions

First, recall from middle school or high school, a mathematical function is like

f(x) = x + 1

and we can use it for different inputs to get different outputs. The processing of inputs is always exactly the same: any input is added with 1 when given to the function

f(1) = 1 + 1 = 2
f(5) = 5 + 1 =6

A Python function is somewhat similar. In Python, a function is pre-written codes that do a particular task on any input given to it. Function has some conceptual similarity to variables in that they also store something for reusing. The difference is that variables store contents, and functions store processes. In a programming, functions that come with the base installation of a language is called built-in functions.

The print() function

We use a function by referring to its names. Functions’ names must be followed by a pair of open-close parentheses (). This action is named “calling a function“, or a “function call“. A function may or may not have inputs. Inputs must be inside the parentheses. Functions without input parameters still need the parentheses however there is nothing in between them. A function that we will use a lot is print() which will display the values or contents of the inputs we give it. A call to print() without input parameters will output an empty line.

The print function can surely work with inputs being variables. In fact, that is one of its most common use cases. You can try changing the values or the variables in the codes below and observe the result.

Strings in Python

So far in all examples, I been assigning numbers to variables, which are one of the data types in Python. Besides numeric, we have a second data type which is just as important, the string type. In short, strings are texts. Whenever you see something wrapped in between a pairs of quotes, either single quotes '...' or double quotes "...", you are looking at a string. For example, 'hello', 'this is python', 'cvx)(U*)(&', are all strings in Python.

Strings can also be assigned to variables to use multiple times. In the example below, I assigned "hello data science" to a variable called a_string. So, whenever I refer to a_string, I get "hello data science".

More input with the print() function

The print() function can take more than one inputs. To provide more inputs to print(), simply include all inside the function parentheses and separate them by commas ,. A common use of this feature is to output a combination of static text and dynamic contents from variables. For example, as you change name to other values, the name part in the output of print() updates accordingly. However, the 'hello' part stay constant.

In data analytics, there are a lot of use cases for print(). For example, when you have trained multiple predictive models and look at their accuracy without confusions, you can use print() and give it both the models’ names and their accuracy values. You can also display information of a data set in a nice way. There are really countless applications of this simple but useful function.

Conclusion

Print() is a very useful function that you can use to display different information of an analysis to yourself and others. Learning to use print() effectively is highly important, as it will also get you used to the concept of functions in programming. I will see you in the next post about the if-else structure in Python.

1 Comment

Comments are closed