Lab 03 Pre Lab



Variables types

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

Using variables in Python is easy! There are just three important rules we have to remember:

  1. You must follow Python's rules for naming variables. That means no special characters (other than underscores), no digits at the start of a variable name, and no variables with the same name as keywords. You can find more details about Python's naming rules in the slides for Lecture 03.

  2. Use meaningful variable names! For example, numberOfBooks is a much better variable name than NoB or numb or x. Something like numBooks would also work, if you want to keep your variable names a bit shorter.

  3. Before we can use a variable, it must be initialized. In other words, we have to put a value into the "box" before we can start using the variable. We do this using the assignment operator, the equals sign (=).
    For example: We had to initialize the value of the variables booksPerShelf and numberOfShelves before we could use them to calculate the size of the library.

Here are some more examples of variables:





Constants

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.

For example:

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.





Expressions

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:

Our expression is  15 + 42, which evaluates to 57. This result is then stored in the variable sum1. In other words, the value inside the box labeled "sum1" is now 57.

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

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.





Casting

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: