The "print" function is a Python instruction that will output variable values and results to the computer screen. This instruction will allow us to automatically output data results to the screen when running a program. This section reviews what we've covered so far and introduces examples of the print function. Try executing these instructions in the command line window to make sure you understand how to use the print function:
z=1.45 print(z)
Notice how the print function outputs the value of the variable z
to the screen.
It is possible to use the print command in a slightly more sophisticated way. Type this set of commands in the command window:
temperature=40 print('Temperature outside = ', temperature)
Observe, it is possible to "dress up" the screen output by adding some extra descriptive text. We will see more examples of this later on as we introduce and develop expertise with the string data type. But, as a preview of using the print function with text, try typing this command:
print('Hello world!')
Congratulations! You have now executed what is probably the most-used example in just about every introductory programming course.
Read this page to see more examples of using the print function. Try typing some of those examples in the Repl.it IDE to be sure you are comfortable with the print function. Consider executing these instructions one after the other (that is, sequentially):
var1=22 var1=-35 var1=308
What value will the variable var1
contain after these instructions are executed? You can check your answer by using the print function.
Variables
Any Python interpreter can be used as a calculator:
Python |
---|
|
Output |
---|
|
This is great but not very interesting. To do anything useful with data, we need to assign its value to a variable. In Python, we can assign a value to a variable, using the equals sign =
.
For example, to assign value 60
to a variable weight_kg
, we would execute:
Python |
---|
|
From now on, whenever we use weight_kg
, Python will substitute the value we assigned to it. In layman's terms, a variable is a name for a value.
In Python, variable names:
- can include letters, digits, and underscores
- cannot start with a digit
- are case sensitive.
This means that, for example:
weight0
is a valid variable name, whereas0weight
is notweight
andWeight
are different variables
Types of data
Python knows various types of data. Three common ones are:
- integer numbers
- floating point numbers, and
- strings.
In the example above, variable weight_kg
has an integer value of 60
. To create a variable with a floating point
value, we can execute:
Python |
---|
|
Python |
---|
|
Using Variables in Python
To display the value of a variable to the screen in Python, we can use the print
function:
Python |
---|
|
Output |
---|
|
We can display multiple things at once using only one print
command:
Python |
---|
|
Output |
---|
|
Moreover, we can do arithmetic with variables right inside the print
function:
Python |
---|
|
Output |
---|
|
The above command, however, did not change the value of weight_kg
:
Python |
---|
|
Output |
---|
|
To change the value of the weight_kg
variable, we have to assign weight_kg
a new value using the equals
=
sign:
Python |
---|
|
Output |
---|
|
Variables as Sticky Notes
A variable is analogous to a sticky note with a name written on it: assigning a value to a variable is like putting that sticky note on a particular value.
This means that assigning a value to one variable does not change values of other variables. For example, let's store the subject's weight in pounds in its own variable:
Python |
---|
|
Output |
---|
|
Python |
---|
|
Output |
---|
|
Since weight_lb
doesn't "remember" where its value comes from, it is not updated when we change weight_kg
.
Source: The Carpentries, https://swcarpentry.github.io/python-novice-inflammation/01-intro/index.html
This work is licensed under a Creative Commons Attribution 4.0 License.