.. include:: ../../include/global.inc
Now you can output things with print() and you can do math. The next step is to learn about variables. In programming, a variable is nothing more than a name for something and helps making the code easier to read. In |Python| the equal sign (=) is used to assign a value to a variable.
1 2 3 4 5 6 7 8 9 10 11 12 | width = 10
depth = 5
height = 20
area_size = width * depth
volume = area_size * height
# Variables can be output with print()
print(area_size)
# The output of strings and variables can be combined
print("Volume:", volume)
|
50
Volume: 1000
A value can be assigned to several variables simultaneously as seen below. Additionally, you will learn how to make strings that have variables embedded in them. You embed variables inside a string by using specialized format sequences (in this case %d) and then putting the variables at the end with a special syntax.
1 2 3 4 | width = depth = height = 10
print("The edge lengths of the cube are %d, %d and %d, respectively." % (width, depth, height))
print("Still the same volume: %d" % (width * depth * height))
|
The edge lengths of the cube are 10, 10 and 10, respectively.
Still the same volume: 1000
Note
Remember to put # -- coding: utf-8 -- at the top of your script file if you use non-ASCII characters and get an encoding error.