knowledge-kitchen / course-notes

Data Types (in Python)

The simplest primitive data types

Every high-level computer programming language has a built-in understanding of a few different types of data. The differentiation in how a computer "understands" data is, of course, different from how humans think about data. Some common data types are outlined below.

Integers

Simple numbers with no decimal place.

Examples:

Floating point numbers

Numbers with decimal points.

Examples:

Strings

Regular text is usually represented by programming languages as a "string of characters" or "string" for short.

Most programming languages require that quotation marks be placed around string literals to differentiate them from other keywords in the language. Whether to use double or single quotes for this is dependent on which language you are using.

Examples:

See more on Strings

Booleans

Boolean values are those propositions which the programming languages understands to be either true or false.

Examples of Boolean values in Python:

Examples of Boolean values in Java:

See more on Boolean logic

Aggregate data structures

Some data structures allow you to group together a bunch of different pieces of data into a single aggregate data structure. These are a bit more “high-level”.

Lists and arrays

Lists, also known as arrays, are groups of single values.

Dictionaries, associative arrays, and hash maps

Dictionaries, associative arrays, and hash tables, are all data structures that hold a group of key/value pairs.

Incompatibilities among data types

When trying to perform operations on values of multiple data types, it is not uncommon to find incompatibilities among the data types. Each high-level programming language has its own limitations on how different data types can be mixed and matched in operations.

Python examples

Example of an error:

x = "my favorite number " #a string
y = 4 #an int
z = x + y #an error! you cannot add a string to an int

Example of a potential solution

x = "my favorite number " #a string
y = 4 #an int
z = x + str(y) #fine! the int was converted to a string before adding

Converting among data types

Often it is useful to translate a value from one data type into another data type. Each high-level programming language has its own techniques for how to convert data of one type to data of another type.

Python examples

Imagine you have a variable x

x = "4"

Python built-in functions:

Code examples

#input
x = input("What's your lucky number today?")

#processing
y = int(x) * 10

#output
msg = "Your lucky number times ten is " + str(y)
print(msg)

Introspection

If you're not sure what data type a given value is, find out. Each high-level programming language has its own techniques for introspection, or discovering information about the system and the data stored in the system.

Python examples

Code examples

if type("a haircut") == str:
  print("Hoorah!")