Chapter 2, Lesson 1 Text
Chapter Two: Working with Data
Lesson One: Data Types and Variables
Most programs require data to do useful work. For example, a calculator application needs to add numbers and a word processor needs to manage text. In this lesson, we introduce the Python data types that identify specific kinds of data and teach you how to name or identify data values within your program.
As you can imagine, there are different kinds of data. A number and a paragraph of text, for example, would likely be treated differently within a computer program. Your program might reasonably want to do some math with your numeric values, and those same math operations like add, subtract and multiply wouldn't make any sense for a paragraph of text. Similarly, you might want to convert a line of text from upper-case to lower-case, and that operation wouldn't work on a simple number.
In Python, like many programming languages, each value stored within your program has a specific data type. This is important because the data type controls what the value can hold and the kinds of operations that can be done on the data. The amount of computer memory it takes to hold one data value also depends on the data type.
There are a variety types of data in the Python language. We are going to introduce some basic data types now, and you will learn others later in the course. The table below shows some of the most useful Python data types.
|integer||An integer value can store positive or negative whole numbers like 0, 1, 42, or -347.|
|floating point||A floating point value can store a decimal number with a fractional part like 3.14159 or -1.732.|
|string||A string value can store a line of text like "May I take your order?" Individual characters are simply stored as short strings with a length of one, as in "X".|
|Boolean||A Boolean value can hold only two possible values - true or false. Boolean values are often used when making logical decisions.|
Now that we've identified the different kinds of data, how do individual data values actually get stored in a program? You might want to store the numbers 42 and 12, so you will need two different places to store "integer" values.
A program will store specific data values in holding areas called variables. A variable has a name and represents a storage area in computer memory. You can picture a set of variables as a group of lockers in a school, where each locker has the name of a student. The contents of each locker can be different.
Let's say you want to add, remove, or peek inside the locker for one particular student such as Sue. You would use the name to find the right locker, and then open that locker to manage the data held inside. In Python, each locker can only hold one particular type of data at a time. One locker might hold a string, while another might hold an integer or Boolean value, and so on.
However, you can change the data type in any variable by simply placing a different value inside. So, you could replace "Hello" in the Sue variable with a number like 43, and Sue would then automatically become a numeric variable. You can also simply update the contents in a variable with another value of the same type. The original "Hello" in the Sue variable could be replaced with "Goodbye", for example.
Creating, Naming and Assigning Variables
When programmers create variables to hold data, each variable will be assigned one kind of data type. In some languages, it is the programmer's responsibility to select the data type like integer or floating point when the variable is created. However, in Python, the variable's data type is set automatically each time you store data in it.
In addition to a data type, each variable must have a name that you will use to manage that specific storage area. In Python, you can create a variable just by typing in a name and then assigning a value. The example below creates a variable named myAge and stores the integer value 17 in that variable.
myAge = 17
The next example creates a string variable named message and stores the value "hello" in that variable.
message = "hello"
These two Python lines of code are examples of assignment statements. An assignment statement has a variable name on the left, then an equals sign (=) and then the value to store in that variable. Any time you want to store a new value in a variable, use an assignment statement. We will explore assignment statements in more detail in the coming lessons.
Rules for Naming a Variable
The Python language has specific rules about the names you give to your variables. Your variable names may contain any mixture of upper and lowercase letters (including non-English characters).
CAPITAL = "Happy" # variable name in all capital letters lowercase = "Birthday" # variable name in all lowercase letters MixedCase = "to me!" # variable name in mixed case letters
It's important to understand that the names of variables are case-sensitive. This means that names with the same letters in different upper or lower-case mixtures are treated as different variables.
ShirtCost = 5 # There are three different shirtCost = 6 # variables here, because shirtcost = 7 # the names are not exactly the same
Many programmers will use capitalization to help visually identify different words in a variable name. In the example above, we combined the words "shirt" and "cost" to make a variable name. The first example capitalizes the first letter of each word, while the second example leaves the first letter of the first word in lower case. The third example doesn't use any capitalization at all.
Which pattern do you find easiest to read? Everyone has their own personal opinion. The second style (shirtCost) is used by many programmers and is the format we will use in this course.
The actual capitalization style you use doesn't change your program logic at all. However, you must understand that variable names are case-sensitive, so "ShirtCost", "shirtCost" and "shirtcost" are three different variables. Beginning students will often mix up capitalization styles and accidentally create new variables when they intended to re-use an existing variable. Be careful to always write your variable names exactly the same way!
You can also use the underscore character (_) in your variable name. Often this is used to visually separate words but can also be used as leading or trailing characters.
shirt_cost = 5 # embedded underscores are ok _pants = 3 # leading or trailing underscores are ok
Numbers are also acceptable in variable names if they are not at the beginning. They can be either embedded in the middle or at the end of the name.
area51 = "spooky" # embedded or trailing numbers are fine 42aliens = "aliens" # ERROR: can't begin with a number!
All variables must have names that are one continuous phrase of letters and symbols. Variable names cannot contain spaces.
no spaces = "false" # ERROR: can't contain spaces or other whitespace
The Python language has a list of key words that have specific meaning within the language; these are called "reserved" words. You cannot use any reserved words as a variable name.
true = "value" # ERROR: true is a keyword and can't be used as a variable name
The following table lists the reserved Python words to avoid in your variable names.
|Keywords in Python|
Best Practices for Naming Variables
Python doesn't care what names you give to your variables, so long as they follow the syntax rules. So, variable names like i42 or YUA13Z_22 are perfectly valid. However, you should normally create variable names that have some useful meaning to a human that reads your code later.
- Try to create variable names that describe what they contain
- Be careful to avoid misleading names
- Don't make your names so long that they are hard to read or type
- Don't make your names so short that they become mysterious
Which of the following variable names do you think would work the best for storing the name of a player in a game program?
The last choice (playerName) most accurately describes the variable contents and is not too short or too long.
We might use general variable names like myNumber or myString in examples where the overall meaning is not that important. In your own code, it's best to be more specific and create variables with names that match their purpose in your program.
Work with Me: Naming and Typing Variables
Using the naming rules listed above, come up with your own variable names to hold the following kinds of data. In addition, describe the data type (integer, floating point, string, Boolean) that would likely be used by each variable.
- A player's current score in a game
- Your pet's name
- The number of books in a library
- The answer to the question "Do you want to continue?"
- The result you get when calculating the area of a triangle
Discuss your chosen names and selected data types with your class or with your teacher. Do your names make sense to them, or do they have suggestions for improvement? Does your class or teacher agree with the data type you believe will work the best in each case?
End of Lesson