Variables
Variables hold data. Data could be a string, multiple strings, numbers, and more.
Learning Objectives
You should be able to:
- Describe the purpose of using variables
- Create variables in Python
Video Walkthrough
Use this video to follow along with the steps in this lab.
Why Variables Are Useful
Variables help organize the data that an application uses. For example, an online banking website will store your first name as a variable. When you log in, the variable might be displayed with a welcome message. If you order checks, the variable with your first name will be printed on the checks.
There are two kinds of variables: mutable and immutable. Mutable objects can have their values changed. Immutable variables cannot change. The only way to change the value of an immutable variable is to overwrite the old variable. Strings and numbers are immutable.
Naming Variables
In Python, variables must start with a letter or an underscore. Only use a-z and 0-9, and _. So, no special characters. Variable names are case-sensitive. This should be a couple of pages of text and screenshots.
Good variable names:
- age
- first_name
- firstName
- compay1
- spent_money
Illegal variable names:
- 9_squares (starts with a number)
- first-name (has a dash)
- spent$ (has a special character)
Make variable names unambiguous if possible. "ASDF" is a perfectly valid variable name, but nobody reading your code will understand what that variable is meant to represent.
Create Variables
- In a terminal, run
pythonto start an interactive Python shell.
python
- Run the following code to create a variable named
first_namewith a value ofAlice.
first_name = "Alice"
- Use the
print()function to output the variable's value. (In the shell, it's possible to just write the variable name and press enter, but this does not work in .py files.)
print(first_name)
- Run the following code to make "Alice" lowercase.
first_name.lower()
- The code will automatically print
alicein the shell. - Print the value of first_name again.
print(first_name)
- The value of first_name is still
Alicewith a capital A. Thelower()method did not change the value of the first_name variable. - Update the first_name variable with the lower-case version of the variable's value.
first_name = first_name.lower()
- No code will be output in the shell.
- Print the value of first_name again.
print(first_name)
- Now, the value should be lowercase. In this instance, Python overwrote the old variable with the new value.
- Create an integer variable named
age.
age = 25
- Notice that you did not have to declare a data type for first_name or age.
- Print the value of the age variable.
print(age)
- Add 5 to age.
age + 5
- The value 30 will be output in the shell.
- Predict what the value of age will be before printing it.
print(age)
- Update the age variable by adding 2.
age = age + 2
- Check that the value has been updated.
print(age)
The value should be 27.
Challenge
What variables would you create to store data about a single person? define the variables and print them in the shell.
Reflection
- Why is it important to use descriptive variable names?
- What other data types besides numbers and strings would you want to describe a person?
Key Terms
-
Variable: A named storage location in a program that holds a value. Variables allow programmers to store, retrieve, and manipulate data. The value of a variable can change during the execution of a program. Variables are fundamental to programming and are used to represent data that can be used and modified by the program.
-
Mutability: The ability of an object to be changed after it has been created. In programming, mutable objects can have their state or contents modified, while immutable objects cannot be altered once created. For example, in Python, lists are mutable (their elements can be changed), whereas strings and tuples are immutable (their contents cannot be changed after creation).