Building Blocks of Python - Data Types and Variables

How do we represent data in Python?

Building Blocks of Python

Every language has building blocks such as words and letters that, when combined in a certain order, will make sense to people. In our case, there are four building blocks to Python that, when combined in a certain order, will make sense to your computer.

The most important part of the Python language is data. There are different categories of data in Python; some are basic and straightforward types, others are more complex and may be made of different combinations of the simple data types. The most basic data types are called primitive data types. Primitive data types, or primitives, fall into the following categories:

  1. Numbers, such as 1, -15, 3.414, etc.

  2. Letters & words

  3. Values that we will use in our calculations, i.e., letters that we can combine in certain ways to get meaningful or desired results.

Later, we will talk more about how we can combine primitive data types when we learn about functions!

The primitives in Python are: integers, floats, strings, and booleans.

Integers are whole numbers on the scale from -∞ to ∞. There's another kind of number in Python too, called floats. Floats are numbers that include decimal points to divide the integer and fractional parts--e.g. 1.2where 1 is the integer part of the float and 0.2 is the fractional part. They are real numbers.

>>> 2  # Integer
2

>>> 1000.019   # Float
1000.019

Letters, numbers, and symbols are known as characters in Python. Combinations of characters are called strings. In Python, an interpreter is the program that reads your code and tells the computer to carry out the commands. You can make the interpreter understand that an object is a string by putting it in either single or double quotes: 'pies' and "pies" are both the same thing. If you want either single quotes or double quotes to be part of your string, you can do so by using the other to depict the string. For example, to store Amanda's in a string, you'd set it to "Amanda's". To store "Yes," she said in a string, you could use '"Yes," she said.' (This is not true for every programming language, though!)

>>> "abcdef"
'abcdef'

>>> 'Hello'
'Hello'

>>> "I <3 Data Science!"
'I <3 Data Science!'

The last primitive is booleans. Booleans are true or false values. In Python, statements can evaluate to True or False, and booleans represent that value. True will always evaluate to True and False will always evaluate to False. A special fact about Python is that it treatsTrue as being equal to 1, and False as being equal to 0, when used in arithmetic.

>>> 1 + True     # They must be capitalized!
2

>>> 2 > 3    
False

These are the four primitive data types that will constantly appear. As we go on, we will develop new ways to combine these pieces of data.

If you are ever unsure what the data type of a value is, you can use the function type(...) to check what the data type is. Functions are pieces of code that take in values, run calculations or manipulate them in certain ways, and either use the result in further calculations or return the answer to you. We'll be discussing them in detail later.

>>> type('4') # tells you what data type something is
<type 'str'>

>>> type(4)
<type 'int'>

>>> type(4.0)
<type 'float'>

You can also convert to and from integers, floats, and strings easily using functions that Python has built-in. Built-in functions are functions you can always use after you have started Python in your command line. Some common functions include: type(...), str(...), float(...). We have already discussed what type(...) does. str(...) converts the value you give the function to a string. float(...) does the exact same thing, except it converts numbers or strings to a float data type. If you need to convert a value from one data type to another in order to use certain functions that only allow a specific data type, you can use the functions str(...), int(...), float(...).

>>> str(4) # converting an integer to a string
'4'
>>> int('4') # converting a string to an integer
4
>>> float(4) # converting an integer to a float
4.0
>>> float('4') # converting a string to a float
4.0
>>> int(4.6) # converting a float to an int rounds down to the nearest whole number
4

Another useful function is the len(...) function, which takes in a string and tells you how many characters it has.

>>> len('a')
1
>>> len('ab')
2
>>> len('Mary went.')
10

Another built-in feature of Python is that we can use operators. Operators are symbols that represent a calculations. The most common operations are: +, -, *, /, which will allow you to add, subtract, multiply, and divide numbers as you would in regular math.

Here are some examples:

>>> 2 + 24
26

>>> 130 - 90
40

>>> 4 * 5
20

>>> 80 / 4     # When you divide integers, they become floats
20.0

>>> 10.0 / 2.0
5.0

Simple, right? To try this out, open up your command prompt and type in python. You should see something that looks like this:

This is an interactive session of Python that allows you to see the results of code you run in real time. You can try using the +, -, *, / operators to add, subtract, divide, and multiply numbers.

By using these operators, Python is giving your computer instructions and telling it what to do. When you type in 2 + 2, for example, your computer is getting instructions to add two numbers. By using the Python interpreter, you're telling your computer what language your instructions are written in so that it can evaluate things accordingly.

There are several other operators that we will use often. They are //, %, **, <, >, ==.

>>> 10 / 3      # Normal division returns a float
3.3333333

>>> 10 // 3     # Double division gets rid of everything to the 
3               # right of the decimal, and will return an integer

>>> 100 % 2     # The `%` operator is the remainder. 
0               # If they are divisible the remainder is zero

>>> 100 % 3     # This equals 1, because the closest number that 
1               # is a factor of 3 is 99 (3 * 33), and 100 - 99 is 1.

>>> 2 ** 3      # This is the same as 2 to the power of 3. It is 2 cubed.
8

>>> 10 ** 2     # 10 * 10 is 10^2 which is 100
100

We can compare values using the operators: <, >, ==, and they return True or False.

>>> 10 > 0     # is 10 greater than 0? Yes!
True

>>> 10 < 0     # is 10 less than 0? No!
False

>>> 10 == 10   # is 10 equal to 10? Yes!
True

The best way to familiarize yourself with these operators is to try them out by opening up your command prompt and typing python.

Variable Assignment

Python can do more than just act as a basic calculator though, and that's what makes it useful.

In algebra, you can assign a number to a letter -- say, x -- and use that variable to represent a value. Similarly, you can represent any value as a variable in Python.

So, for example, if you ran the following:

>>> x = 2
>>> x * 2
4

You would get an output of 4. Likewise, we can also assign x * 2 to another variable. Let's call it y.

>>> x = 2
>>> y = x * 2
>>> y
4

Here, we have assigned a variable to an arithmetic expression, but it's true value is still just a number. This is because, when the interpreter assigns the variable to something, it evaluates it first. Evaluating x * 2 gives us 4, so y is assigned to 4.

You can use pretty much anything as a variable name, although for style purposes, it's preferred that you use lowercase words and/or letters, separated by underscores. You can also reassign variables as you go through code.

>>> my_number = 3
>>> your_number = 6
>>> my_number - your_number
-3
>>> my_number * your_number
18
>>> my_number = 4
>>> my_number * your_number
24 # this is equivalent to 4 * 6 since we changed the value of my_number

Once you re-assign a variable, you lose the value you once had. Think of it as a garbage collector for your memory in your computer. Once you reassign the variable, there is nothing pointing to the old value, so it gets collected by the garbage collector. To go back to that old value, you can do another reassignment.

>>> a_number = 6   #you can use any other variable name too!
>>> a_number * 2 
12
>>> a_number = 4
>>> a_number * 2 
8
>>> a_number = 6
>>> a_number * 2
12

We can also use variables to represent all of the other data types we learned earlier.

>>> a_string = "This is my string"
>>> a_string
'This is my string'
>>> a_boolean = True
>>> a_boolean
True
>>> a_float = 3.14
>>> a_float
3.14

You can also "add" strings together. This is called string concatenation. It works just like if you glued two words together. Be careful about the spacing though!

>>> start = "I like " # notice the extra space after the word 'like'
>>> pie = "pie."
>>> cake = "cake."
>>> start + pie
'I like pie.'
>>> start + cake
'I like cake.'

Last updated