In this section we will learn how to assign values to a variable in Python and display results.
Objectives
Create a variable and assign it a value in Python using =
Display the value of a variable in Python using print()
Subsection
A variable is like a container that can be used to store information or data. In Python you assign a value to a variable using = .
Run the Python code below by clicking the Evaluate (Python) button.
Note that the function print() was needed to display the value of the variable.
You Try1.1.
Change the value of the variable a by adding 5 after the 2*3 above and run the code again to see how the output changes.
SubsectionNaming Variables
Variables can be named using letters, underscores, numbers, etc. Just be sure to use the exact same name to call your variable.
For example, the following code has a syntax error. Run the code as is and see what happens.
You Try1.2.
Can you fix the bug in the above code so it runs correctly?
Hint.
Variables are case sensitive and must be exactly the same for Python to recognize them.
SubsectionArithmetic and Comments in Python
Python recognizes simple arithmetic symbols like + -, *, /.
Meanwhile a hashtag # can be used to tell Python to ignore code, reading what follows as a comment by the author rather than a Python command.
Comments can help anyone reading your code understand what that section of the code is doing. You can also comment out portions of code to isolate a bug.
You Try1.3.
In the box below, change the value of the variable arithmetic to try out some basic calculations. Add a comment using # to explain what your code is doing.
Hint.
You can use the standard order of operations and parentheses (PEMDAS).
SubsectionMore about Variables
Just like in math, variables can be helpful when the information will change. Recall the formula for the area of a circle \(A=\pi r^2\text{.}\)
In the code below,
fill in the formula for the area of the circle using the appropriate variable names and arithmetic operations.
Use the code to approximately compute the area of a circle with radius 5 and a circle with radius 13.
Note that print will display comma separted items one right after the other. In the code above we first displayed the string “The area of a circle is” followed by the current value of our variable circle_area.
SubsectionSyntax and Semantic errors
One error we can make when creating and editing code is to mis-type a command causing Python to not understand the syntax, that is the chosen command, in our program. Another error is when we make our meaning unclear, more of a semantic issue. For example you can add numbers to numbers or strings of letters to strings of letters in Python but if we try to add a number to a string of letters, Python will not know what is meant by adding.
Summary.
A variable can be used as a container to store data or information.
Use print(variable_name) to display the information stored in the variable.
Use # to comment and make your code more readable.