Python, like other computer languages, uses variables to store data values in memory. In storing these variables in memory, we are also assigning a data type to the value held by the variable so that the processor knows how to treat the value. This is important because integer numbers have different properties than floats (decimals) and dates. The beauty of Python is that data types for variables do not have to be explicitly declared as they do in statically typed languages such as Java or C. Instead, the Python interpreter dynamically infers the data type, therefore, allowing any kind of data to be assigned to any variable without setting memory allocations. For example, in C we would type:
int value = 0;
for(int 1=0; i<1000; i++){
value +=I;
}
The same operation in Python would look like this:
value = 0
for i in range(1000):
value +=1
Python will recognize and treat value as an integer whereas in the C example we had to declare value as an integer. Now, if we want to change things up in Python, we can type:
X = “one-hundred”
But in C that cannot be done.....
int x = 100;
x = “one-hundred”; //ERROR
We told the computer x would be an integer and then we changed it to a string.
Back to basics, if you consider the types of data you encounter on a daily basis you will likely find that you deal with floating decimal places (floats) and integers (counts) when dealing with numbers, word (string), and dates (datetime), plus various others. We have general rules as to how these data types can interact with one another:
As we can see, the computer will not combine two different data types by addition. It is important that we understand the different data types and how they can be used and manipulated.
Python Standard Data Types
Python has five standard data types:
- Number
- String
- Boolean
- List
- Tuple
- Dictionary
Numbers
Python will interpret any number you enter as a number. If the number has no decimals, Python will see it as an integer and if it does have decimals Python will see it as a float. For example, 155 is an integer and 155.0 is a float.
Integers
Integers, also known as int, are whole numbers that can be positive, negative, or 0. An example of integers would be …….-2, -1, 0, 1, 2…… In Python, we can print an integer like so
Print(1500)
1500
Notice that commas are not used in numbers greater than 999 like you would use if you were writing.
It is also possible to assign a value to a variable. An important concept is that a variable holds a value and does not equal that value. For example:
We can also assign expressions to variables:
My_expression = 1500 + 500
Print(my_expression)
2000
Floating Point Numbers
Floating point numbers, or floats, are real numbers that can be rational or irrational. These are pretty common; think about numbers with decimals, such as money, and p. As with integers, floats can be printed with Python’s print function
Can be assigned to variables
And we can assign expressions with floats to a variable
One thing to keep in mind is that while we may see 5.0 and 5 as being the same, the computer does not, 5.0 is a float and 5 is an integer. Performing a mathematical operation with a float and and integer will convert the integer to a float
Strings
The string data type comprises letters, numbers, and symbols contained within either single quotes ‘ or double quotes ‘. The choice is yours to make when choosing double or single quotes, but it is important to stay consistent.
Some examples of strings are:
‘5’
‘Hey diddle, diddle. The cat and the fiddle.’
‘My cat likes single quotes.’
“My dog likes double quotes.”
The classic first program is the “Hello World” statement.
Print(“Hello World!”)
There are numerous string methods and functions we can employ within Python to manipulate string data. These are worth exploring to make your data munging tasks easier as well as exchange information back and forth with the computer.
Boolean
Boolean data types will be one of two values; True or False. These can also be represented as T or F, and 1 or 0, respectively. Some examples of Boolean logic are:
10>9 True
8<4 False
550<600 True
Notice double equals are used here == to test for equivalency. A single equals sign = is only used to assign value to a variable.
As with numbers and strings, Boolean values can be assigned to variables and printed with the print() function:
Lists
A list in Python is a mutable ordered sequence of items. Anything can be included in a list and lists can also be assigned to variables like the other data types. Lists are always contained within two brackets [ ]. Some examples are:
Integers [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Floats [2.2, 3.2, 4.2, 5.8, 9.2, 9.8, 10.0]
Strings [“Beagle”, “Jack Russel”, “Pudle Pointer”]
Lists are often used in list comprehension, where an operation is performed on each member of a list. These are very flexible data types as they can be changed and appended to.
Tuples
Tuples are immutable, ordered objects used to group data. Tuples are contained within parentheses ( ). An example of a tuple is:
(‘red’, ‘orange’, ‘yellow’, ‘green’, ‘blue’, ‘indigo’, ‘violet’).
Tuples can also be assigned to a variable:
Dictionaries
Dictionaries in Python are used to map key:value pairs to hold data and are constructed with curly braces { }. An example of a dictionary, or dict, is:
{‘name’: ‘Millie’, ‘animal’:’dog’, ‘breed’:’Pudle Pointer’, ‘color’:’brown’, ‘home’:’Reno’}
In this example, they keys are the words to the left of the colon: ‘name’, ‘animal’, ‘breed’, ‘color’, ‘home’. These can be creaed using any immutable data type. The associated values are ‘Millie’, ‘dog’, ‘Pudle Pointer’, ‘brown’, ‘Reno’ and can consist of any data type.
As with all Python data types, dicts can be assigned to a variable and printed:
To select specific information we can call the variable and one of its keys. If we want to know my dog’s name, we can call my_dog[‘name’]. We can also print this value
Final Thoughts
This brief overview of Python’s primary data types and data structures should help you get started with applying them to your own Python programming. As your skills grow, you will learn and use list comprehensions to iterate operations, will leverage indexing functions to add and subtract from dictionaries, and will learn to use list functions like append to add values to lists once you have performed operations. If this has been informative and helpful, please comment below or contact me at Twitter.