Viewing Data and Variables
In this lesson we will go over how the print
function works and
how we can use it to print text or a dataframe.
In programming, a simple yet fundamental function is one that displays the content
of a variable. If we want to display the text "Hello World!" we pass this text
as an argument to the print
function.
Let's consider another situation.
Imagine a loading screen during a game's installation process.
This screen might show a percentage to indicate progress.
It comprises static text and a dynamic number such as "Progress: 16%".
The print
function we just learned can do this in
a couple of different ways. The way we will learn in this lesson is by
passing these in as additional arguments, as can be seen in this
code block.
Sometimes we would like to add a little bit of space between
text. To do so, we need to embed within the text the
\n
character, which is a special character sequence,
known as an escape sequence, that tells Python to add a new line.
In the next code block, try running it first, then try running it again after deleting the escape sequence and see what happens!
Printing dataframes
The simplest way to print a dataframe is to include it inside a print
statement. For instance, after creating the iris dataset,
we can use print(iris)
. However, as this dataset has 150 rows,
this will display the entire dataset, which may be a tad overwhelming.
Instead, we can use other methods called head
or
tail
to view the first and last five rows,
respectively. The number of displayed rows can be adjusted
with an optional input. We will learn methods in
detail in the Classes lesson.
In short, a method is a function associated with
a class object, such as the dataframe class object
iris.
There was something else we snuck in there and that is a comment.
A comment begins a line with the pound symbol #
,
which tells Python to ignore anything found after it.
What might happen if we have a Python program that
contains only comments? Well, nothing will happen! So why are they
important? Comments are added to code to help fellow data scientists,
developers (or yourself a year from now) have a better understanding for
why a line of code is written the way it is. This is incredibly useful in
maintaining a code base as sometimes we need to understand the
history / reason / purpose of a code chunk to really understand it.
We can further access a single column of data from the dataframe by
writing the dataframe name and the column name in square brackets
and in quotations [" "]
. For instance, to access the
Sepal.Length
column, we write iris["Sepal.Length"]
.
Practice exercise
Assign to a variable a the string Hello World.
This will be printed out with the provided print
function.