Python - Variables and Data Types
Python - Variables and Data Types
Variables and Data Types are two important concepts in the Python Programming Language.
“Variables” are terms that hold a given piece of data, whether from the user or hard coded in
the program. A “Data Type” refers to the category the programmer intends to assign to a
particular piece of data. This handout will further explain what each of them are, how they
work, and when to use them.
You can navigate to specific sections of this handout by clicking the links below.
Variable Guidelines/Rules: pg. 1
Data Types: pg. 2
Different Data Types and What They are Used For: pg. 2
Variable Guidelines/Rules
Example:
In the example code, there are three variables. Two of them are assigned values directly, and
the third is assigned the value of the first two multiplied together. If the programmer decided
to print the third variable, Python would display 28.
Data Types
Data Types are very specific. For example, there is a difference between “Seven,” 7, and 7.0,
which are the data types str (String), int (integer), and float (floating-point number)
respectively. Data types are used with variables to let the computer know how to process given
data. If the programmer wanted to request a number from the user, they would use either the
int or float data types, but if they wanted a sentence, or another series of characters, then they
would use the str data type.
A table of different data types is listed below including examples of how they are used. A list of
the common data types and their definitions are shown on the next page.
Example Data Type
x = “Hello World” str
x = 20 int
x = 20.5 float
x = range(6) range
int (Integer) Data Type is used when the programmer wants the data to be represented by a
whole numerical number.
float Data Type is used similarly to the int data type, except instead of being represented as a
whole number, the data would be represented by a decimal or floating-point number.
list Data Type is used to represent and organize a series of data, whether it be numbers, letters,
or words, and assign them to a single variable. This series of data can be changed later, but they
must always be enclosed in brackets [ ].
tuple Data Type is like the list data type in the sense that it stores multiple values in a single
variable. Unlike a list, however, once tuples are created, neither the order nor the contents of it
can be changed. It is also enclosed in parentheses rather than brackets.
range Data Type is most often used in loops to represent a series of numbers that start with 0
(by default) and increase by one (by default) until it reaches a given number (“Python range()
Function”).
dict (Dictionary) Data Type is similar to both the tuple and list data types, except rather than
holding one value in each part of it (like 1, 2, 3, 4, 5), dictionaries hold a value pair, one acts as a
key, and the other the value associated with it (like animal: cat, name: fluffy). These value pairs
are changeable, so the dict data type acts like an actual dictionary, except instead of pages, it is
enclosed in curly brackets { }.
Example:
Works Cited