Lesson 3. Data Types

Python is different from practically every other programming language that I have worked with in that you do not explicitly define data types. When you create a variable, you have to assign it a value. How you assign it a value will define the data type of the variable.

If you are coming from another programming language, or are familiar with OO programming in general, then you may be familiar with the idea that class and data type are synonymous. Classes are objects and everything in Python is an object. That makes discussions about data types in Python simpler than it would be in other languages.

This lesson goes over what I like to call the fundamental data types. These are the data types that you would find in all programming languages. Data types unique to Python like list and tuples are discussed in their own lessons.

String

Python stores textual data using the str object. Python has a lot of power to manipulate strings which we will be learning about in later lessons. Once you figure out how easy it is to work with string data in Python, you probably won’t want to learn any other programming language ever again.

Boolean

Every computer language gives you the power to represent true and false values. Python does it with the keywords True and False. The T and the F are capitalized and yes it matters.

Integers

Technically, Python supports every integer value between positive and negative infinity. In practice, you will be limited by your machine’s memory.

Float

Floats are how Python represents decimal values in a non-precise way. What that means is, when you are working with floats, you have to look out for things like rounding errors.

Floats have their place in computer science, but, if you are going to work in analytics precision will be required.

Decimal

This is my recommended way to deal with real numbers. You have to be careful when you create your decimal value by passing the constructor either a string or integer value. If you pass the constructor a float value, the result will still suffer from approximation issues.

Examples

Creating Strings

String variables are created by wrapping the value in quotes. You can use single or double quotes.

foo = 'This is a string value.'
foo2 = "This is also a string value."

print(foo)
print(foo2)

Creating Booleans

Booleans are both a data type and a value, so you don't have to explicitly assign a variable as a Boolean. As you can see below, when we cast these values to integers, they come out 0 and 1 just like we would expect from basic computer science.

print(int(True))
print(int(False))

Creating Integers

Creating integers is straight forward. You assign a variable a value that is a number but not a decimal and do not surround the value with quotes.

x = 1
y = 2

print(x+y)

Creating Floats

You create floats by using floating point numbers when you assign a variable a value. Below are some examples. There is also an example of how the imprecision of float can cause problems.

If you do the last calculation by hand, you should get an answer of 0. I even plugged it into my TI-84 and got 0. Because it is a float the value of b gets printed out as a non-zero number.

x = 1
y = 2.5
z = x + y

print(z)

a = 10e-2

print(a)

b = .3 - (.1 * 3)

print(b) #What is this nonsense?

Creating Decimals

An unfortunate thing about creating decimals is that you have to import a module. We will talk about modules in a later lesson, so I am going to hand wave them for now.

We are going to fix the issue of the calculation of b from above by passing the Decimal constructor strings in place of float values.

from decimal import *

b = Decimal('.3') - (Decimal('.1') * 3)

print(b) #That's more like it!

Now you try it!

Don't copy and past. Type the code yourself!

Last updated