Department of Mathematics, Computer Science, and
                Statistics
St.
                Lawrence University
Zonohedron
 

What is this object?

...

Links
Overview
Schedule
Grading Info
Class Notes

 

CS 140: Introduction to Computer Programming


Variables and Data Types

There are four data types that we will encounter in our course: integers, strings, floating point numbers, and lists.
  • An integer is a number with no digits past the decimal point; in other words, numbers like 17, 41, 0, or –3.  (Thus –2.7 and 3.1416 are not integers.)
  • A string is a sequence of characters typed from the keyboard.  Such a sequence might contain spaces, lower and upper case letters, or even digits.  Place characters inside quotes to let Python know to treat them as a string.  Examples of strings include "fantabulous", "agent 007", and even "90210".
  • A floating point number (or float for short) is a real number with digits before and after the decimal point, such as 3.1416, –2.7, and even 5.0.  Note that Python will treat 5 as an integer but will treat 5.0 as a float, even though they are the same number.  This will make a difference when we discuss arithmetic.
  • A list is simply an ordered list of items, placed between square brackets and separated by commas.  Two examples of lists are ["a", "b", "c", "d"] and [17, "twix", 2.7].  Lists usually contain items of the same type, such as in the first example, although it is not required.  Later on we will devote an entire set of class notes to lists later on, so we won't say more about them here.
A fundamental task in almost any program is to perform actions or make decisions based on the values of quantities, either numerical or string type.  Such a quantity can vary in the course of the program execution, so we call its name a variable.  Here is a short program that involves two variables.

name = "Peter"
age = 8
print "Next year", name, "will be", age+1, "years old."


If you were to run this program the output would be

Next year Peter will be 9 years old.


Notice that the program treated age as a number by performing arithmetic with it.  (We will discuss how the print statement works in the notes on Input and Output, and in again in more detail when we reach Formatting.)

Variable names may consist of any sequence of lower or upper case letters, digits, or the underscore character.  However, a variable name cannot start with a digit and cannot contain any spaces.  Thus number, num3, and number_one are all valid variable names, but 7up and seven up are not.  Be aware that Python distinguishes between lower and upper case letters!  This means that name, Name, and NAME are all interpreted as different variables.

A variable changes value during program execution whenever that variable name is set equal to a new value.  For instance, the program

age = 8
age = age+1
print "Next year Peter will be", age, "years old."


will have the output

Next year Peter will be 9 years old.

same as before.  The second line effectively increases the value of the variable by one.  Similarly, we could write

age = 8
age = age*2
print "Twice Peter's age is", age, "years old."


to obtain

Twice Peter's age is 16 years old.