Working with variables

  • 1/19/2018

What you have learned

In this chapter, I’ve shown you how to create variables in Python programs. You’ve learned that a variable is a named location in memory and that there are rules concerning what a valid name can be. Essentially, a Python name must start with a letter or underscore (_) character and contain letters, numbers, or an underscore after the first character. You’ve seen how Python determines the type of variable to create from the type of value being assigned. Assigning an integer value to a variable creates an integer variable, and so on.

You’ve also discovered that there are two fundamental kinds of data in programs: text and numbers. Python provides the string type to hold text, and a program can use the input function to read a line of text from a user at the Python Shell and store that text in a string variable (although you also noticed that this behavior differs between the two major versions of Python). You’ve seen how Python provides the int function to convert a string that holds a number into the value that string represents, and that programs can use a combination of input and int to read numbers from program uses.

Considering numeric values, you’ve seen the fundamental difference between an integer value with no fractional part (for example, 1) and a real-number value that contains a fractional element (for example, 1.5). We also explored how a floating-point value stored in the computer can approximate the actual value, which might lead to problems when performing calculations on real numbers.

Finally, you looked at how calculations are performed in programs, how to convert between floating-point values and integer values, and why a program might need to do this.

To reinforce your understanding of the content, consider the following “profound questions” about variables and values.

What happens if I “overwrite” a variable of one type with a value of another type?

You might think that this would cause an error. For example, you might expect Python to give you an error along the lines of “Last time you used this variable you put a number in it, and now you’re putting a string in it.” However, this is not what happens. Every time you make an assignment, Python creates a brand-new variable with the appropriate type and discards any existing variable with the same name. So, a program never “overwrites” a variable; it simply makes a new one with the same name and different contents.

Does using long variable names slow down a program?

You might think using a variable called “sales_total_for_July” would be more difficult than using one called “sj.” And to an extent, you might be right. However, the effect on a Python program’s speed is so small that it’s insignificant. I’d much rather use longer variable names that make a program easy to understand.

Can we write all our programs using only floating-point variables?

You might think that using floating-point variables would make our programs simpler. However, you can still make a compelling case for using integers where appropriate. We’ve seen that the results of some calculations might not be what we would like them to be. It is perfectly possible that a calculation that should produce the value 1 can produce values such as 1.00000000004. If a program compares the values 1 and 1.00000000004, it would decide that they are different and perhaps cause a program to behave incorrectly.

For this reason, I try to make sure that when I create variables, I use values that will create an appropriate type. When counting things, I’ll use integers; if I need to perform calculations, I’ll use floating-point values.

Can I stop my program from crashing if someone types in an invalid number?

Yes. You haven’t yet learned how to do it, but there is a mechanism in Python that lets a program take control when Python encounters an error. You can make a program that displays an error message when a user makes a mistake and gives the user another chance to enter the value. We’ll explore this in the section “Detect invalid number entry using exceptions” in Chapter 6.