Variables
0 / 7
Variables allow you to store
a piece of information that can be used later in your program.
The example below shows how we store the value 5 in the variable x.
The example below shows how we store the value 5 in the variable x.
We say that we have
assigned
the variable
x
a value of
5.
Notice that when we print the variable x it outputs the value 5
Notice that when we print the variable x it outputs the value 5
Rules for naming variables
You can name variables whatever you want as long as you follow these rules:
1. The first letter in a variable name must be a letter a-z or A-Z or an underscore _
2. Subsequent letters can be letters a-z or A-Z, underscores _, or numbers 0-9
1. The first letter in a variable name must be a letter a-z or A-Z or an underscore _
2. Subsequent letters can be letters a-z or A-Z, underscores _, or numbers 0-9
Here are some examples of good variable names:
Although short variables names like just the letter
x
are technically valid, as a
best practice
you should
give your variables names that describe the data they hold.
Giving your variables descriptive names makes your code easier to reason about and debug.
Giving your variables descriptive names makes your code easier to reason about and debug.
Here are some examples of variables names that are technically correct, but they are bad because they are not descriptive.
In Python it is a best practice to use a naming convention called snake_case when naming variables.
a variable name is in snake_case if all the letters are lower case and mulitple words are joined together with an underscore.
These variables use snake_case:
number_of_students = 33
max_speed = 80
minutes_remaining = 100
total_cost = 50.95
These variables do not use snake_case:
numberofstudents = 33
MAX_speed = 80
MinutesRemaining = 100
TOTAL_COST = 50.95
Working with variables
A variable can be assigned a value from a different variable:
We first declared a variable called
x
and assigned it a value of
10.
Then we declared another variable called y and assigned it a value of whatever was in x. This essentially copies the value of x into y.
Then we declared another variable called y and assigned it a value of whatever was in x. This essentially copies the value of x into y.
You can do math with variables and then print the results.
Read the code below and try to determine what the output will be before you click run.
Example program
Here is a program that you can use to calculate how much an order of apples would cost.
The program includes a sales tax calculation.
The program includes a sales tax calculation.
Be sure to read through each line of the code above. A big part of learning to code
is learning to carefully read and understand programs.
Debugging exercise
Imagine you are a software developer responsible for adding features and fixing bugs
in an online shopping cart application.
Your manager comes to you and says that customers are reporting that when they order apples, the total price after tax is several times more expensive than it should be.
The piece of code you need to debug is the same as the example program above, but a bug has been introduced that is causing the program to output a final price that is much too big.
First, run the code below to see what the current code outputs. Then, find and fix the bug so that the code output matches the expected output.
Your manager comes to you and says that customers are reporting that when they order apples, the total price after tax is several times more expensive than it should be.
The piece of code you need to debug is the same as the example program above, but a bug has been introduced that is causing the program to output a final price that is much too big.
First, run the code below to see what the current code outputs. Then, find and fix the bug so that the code output matches the expected output.
Expected Output
33.4375
Your Output