A variable is how we store information in our Python programs. In simple terms, you can think of a variable as a "box" that you can put stuff in. The "stuff" we place in the box is also known as the value of the variable.
For example, we can create a variable called classSize, and set its value to be 20. To express this in Python code, we would write classSize = 20 .
We've discussed many different types of variables in class, but today we'll focus on three of them: strings, ints, and floats.
Using variables in Python is easy! There are just three important rules we have to remember:
Here are some more examples of variables:
Often, our programs will have data that isn't generated by the user or by the code, but that is used a great number of times in the program. Examples include things like tax percentages for a shopping program, the command a user needs to give to quit the program, numbers or letters to represent different menu options, etc.
These pieces of data don't change as the program is run (e.g., the user can't change the sales tax), and for this reason we call them constants. Constants are a "special" type of variable that we use in Python to store data that doesn't change. When naming a constant variable, the name is written in all capital letters, and underscores are used to separate the words.
When we don't use constants, our program instead has meaningless data (like 100, or "new", or 17.3, or "B") as part of the code. We call this type of meaningless data magic numbers (or magic strings). When we look back at our code a few days later, or if we are looking at someone else's code, these "magic" numbers (or strings) are meaningless. Only a magician could easily figure out what they are supposed to mean.
An expression is code that calculates or produces new data and data values. Expressions are what allow us to create interesting Python programs. The word "expression" is really just a fancy name for something that can be evaluated to a single value.
Remember that expressions must always be on the right hand side of the assignment operator!
For now, we will use expressions in Python mostly for mathematical equations. For example, if we want to find the sum of two numbers in Python, we could write code like this:
We could have achieved the same thing with the following code:
In this second example, we used the variables num1 and num2 to hold the values 15 and 42. However, the expression still evaluates to 57, and that value is still stored in the variable sum1.
Here are a few more examples of expressions:
User input is a way to get information from the user while your program is running. Much like expressions, user input is an important step in creating Python programs that do interesting things.
The Python code you will use to get input from the user will look something like this:
When your program is run, this code will print out the message "Enter your name please: " to the screen. After the user enters their answer and presses enter, the text that they entered will be stored as the value of the variable userName.
However, even if we ask for an integer (and the user enters one), the value will be stored as a string instead. We can't do addition or multiplication like we might want to with a string. Python treats integers and strings very differently!
We can fix this by telling the program that the input should be stored as an integer. Doing this is called casting, a process that changes a variable from one type to another. For example, if we want to convert the user's age to an integer, we could write something like this:
If we wanted their GPA (which would likely be a float, and not an integer) we could write something like this: