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.

Data Types
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.

Data Type Description
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.
List A List value can hold multiple values. We will discuss Lists in more detail in a future chapter.


Variable Concepts
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 is an abstraction inside a program. Variables have a name and represent 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.

School lockers holding variable data

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
Copy
The next example creates a string variable named message and stores the value "hello" in that variable.

message = "hello"
Copy
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.

Changing Variables Over Time
We call our named storage areas "variables" because they can vary or change over time! Each assignment statement will update the variable contents to a new value.

myAge = 17 # the myAge variable now contains 17
myAge = 18 # the myAge variable now contains 18
myAge = 19 # the myAge variable now contains 19
Copy
You can copy the contents of one variable to another by placing a variable name on the right side of the assignment statement. The example below demonstrates how the contents of two variables (myAge and yourAge) can change over time as a program executes assignment statements.

myAge = 17 # the myAge variable now contains 17
yourAge = myAge # the yourAge variable now contains 17 (copied from myAge)
myAge = 19 # the myAge variable now contains 19 (yourAge is not changed)
Copy
Notice that updating one variable does not change the value in another variable.

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
Copy
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
Copy
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 lowercase. 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
Copy
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!
Copy
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
Copy
The Python language has a list of keywords 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
Copy
The following table lists the reserved Python words to avoid in your variable names.

Keywords in Python
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
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.

Writing clear and easy to ready program code is important! Other programmers may need to understand your code later, so be sure to choose meaningful variable names and add comments to describe what the code is doing. You may even return to your own code months or years later and not have a clear memory of how it works, so you will appreciate clarity as well.
To make your programs clear and easy to read, follow these guidelines:

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?

pn
playerData
ThisIsTheNameOfThePlayerThatWeWillBeUsing
playerName
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.

1. Why is data important in a program?
2. What are some kinds of data that a Python program might use?
3, What is a variable? How is a variable data type selected?
4, How do you assign values to a variable?
5, What are the naming rules for Python variables?
6, Are variable names case-sensitive? What does that mean?
7. What are "keywords" and where should they be avoided?
8. What are some best practices for naming variables?

1. Data is important in a program because it provides the necessary information or input for the program to perform useful work or calculations.

2. Some kinds of data that a Python program might use include numbers, text, Boolean values, and lists.
3. A variable is an abstraction inside a program that represents a storage area in computer memory. The data type of a variable in Python is set automatically each time a value is assigned to it.
4. Values are assigned to a variable using an assignment statement, which consists of the variable name on the left side of the equals sign (=) and the value to store in the variable on the right side.
5. The naming rules for Python variables are: variable names may contain a mixture of uppercase and lowercase letters, numbers (except at the beginning), and underscores, but cannot contain spaces or special characters. Variable names are case-sensitive.
6. Yes, variable names in Python are case-sensitive. This means that variables with the same letters in different uppercase or lowercase mixtures are treated as different variables.
7. Keywords are reserved words in the Python language that have specific meanings and functionalities. They should be avoided as variable names because they cannot be used as variable names.
8. Some best practices for naming variables include: creating variable names that describe what they contain, avoiding misleading names, not making names too long or too short, and making names that are clear and easy to read for other programmers who may need to understand the code later. It is also recommended to add comments to describe the code's functionality and purpose.