Skip to main content Link Menu Expand (external link) Left Arrow Right Arrow Document Search Copy Copied

Lesson 2 - Storing and Displaying Data

You may have noticed that Python will only output the last expression in the program. In this lesson, you will learn how to display more data to the screen as well as how to store data in variables.

Table of Contents

Lesson Objectives

  • Use variables to store data.
  • Print data to the console.
  • Use comments to make notes in our program.

Variables

Rather than just using numbers in our calculator, we can initialize and store the number in a variable. With variables, we’ll be able to store our data, but we can also re-use it and even modify that data later on.

Identifier Rules

All variables need identifying names, more formally called identifiers.

Python has some rules and restrictions for identifiers.

  • Identifiers can consist of uppercase letters, lowercase letters, digits, and underscores.
  • Identifiers cannot start with a digit.
  • There are some identifiers reserved for Python’s use only; they’re usually common keywords like “and”, “if”, “or”, etc.
  • Identifiers are case-sensitive.
    • myVariable and MYVARIABLE are considered two different variables.

Assigning Values to Variables

The format to create a variable is:

identifier = value

Suppose we wanted to assign myVariable the value of 10. We would do

myVariable = 10

The line above is read as “myVariable is assigned the value of 10.”

If we decide later on that we want to reassign the value of myVariable to something else, we would again do

myVariable = <new value>

and that would set the value of myVariable to our new value.

Using Variables in Math

Perfect, now we have a variable with some value set to it. How do we use it in our math? Let’s say, for some reason, we wanted to find out the value of myVariable if we add 100 to it. We can just do

100 + myVariable

and it outputs the answer.

Adding to a Variable

When updating the value of a variable, you can use the existing value as part of the expression.

Input

a = 5

a = a + 1
a

Output

6

Another way to do the same step is the following:

Input

a = 5

a += 1
a

Output

6

This works for +=, -=, *=, and /=.

Displaying Data to the Console

Take a look at the code block below.

50 * 0.9
50 * 0.5

What do you think will be shown if we run both lines in the same code block? After trying it out, you’ll discover that it only outputs the answer of one of the lines. That’s because Python will only show you the last line that returns data.

If we want to display more data to the screen, we have to use the print() function to “print” it to the console.

Input

print(50 * 0.9)
print(50 * 0.5)

Output

45
25

Writing Comments in your Code

Sometimes, it’s useful to write yourself (or others) comments about your code. They can be used to explain pieces of code, and generally, they make the code more readable.

Python considers everything after a # symbol a comment and ignores it when executing the code.

Input

# print() displays information to the screen.
print(50 * 0.9)
print(50 * 0.5)

Output

45
25

Since Python ignores comments, you can also use it to prevent lines of code from being executed. This is particularly useful when troubleshooting issues with your code.

Input

# print() displays information to the screen.
# print(50 * 0.9)
print(50 * 0.5)

Output

25

Key Points / Summary

  • You can use variables to store data.
  • The print() function can print data to the console.
  • You can use comments to document and explain your code.