Just like us, programs need to keep track of information as they are solving problems or making decisions. They do this by storing data inside of variables. Last week we saw how to create variables and also how to read them from the user and print them to the screen.
We actually saw how to do this with only one type of data. However, Python actually has several different types of data. To be able to use a variable, you have to know what type of data it contains.
Today we will look at some of Python's data types and what we can do with them.
The first type that we saw was the string type. A string is some text inside of a program. For instance a message, name, address, email, password, URL etc. are all textual and would be stored as strings in a Python program.
As we saw, strings can be created by enclosing text inside of double quotes:
address = "1301 College Ave" print(address)
Strings can actually be enclosed inside of single quotes instead:
city = 'Fredericksburg, VA' print(city)
There is usually no difference between the two ways of making strings. The exception is when a string contains a quote. What is wrong with this program:
answer = input("Answer "yes" or "no" here ") print(answer)
A string using double quotes can't contain double quotes because Python will think it's the end of the string! To fix this, we can use single quotes instead:
answer = input('Answer "yes" or "no" here ') print(answer)
This goes the other way too. For example this program has an error:
print('You can't enter a negative number!')
But this one is fine:
print("You can't enter a negative number!")
In addition to making strings inside of quotes, or using
to read a string from the user, we can also get strings by joining other
strings together. This is done by putting two existing strings together
with a + in between them.
For example, to join someone's first and last name together:
first = input("What is your first name? ") last = input("What is your last name? ") # join them together in a new variable fullName = first + last print(fullName)
We can add as many strings together as we want. How can we make the program above put a space between the first and last names?
# join them together in a new variable with a space fullName = first + " " + last
Many programs compute with numerical data, so numbers are another important type of data. Python actually has multiple types of number for different situations.
One is the integer, or int type. Integers are numbers that have no fractional component. They can be positive or negative, and are stored exactly inside of the computer.
We can create an integer just by putting a number into a program. We can also assign it to a variable to keep track of it:
amount = 15
We can ask Python what type of data something is by calling the
>>> a = "hello" >>> type(a) <class 'str'>
>>> b = 56 >>> type(b) <class 'int'>
Integers cannot have any fractional component. So if we make a number that has a fraction in it, Python gives it a different type, called a float:
>>> cost = 4.99 >>> type(cost) <class 'float'>
A float is a number which can have a fractional part. You may wonder why not just have all numbers be floats, since they can store numbers where the fractional part is 0.
The reason is that floats don't store numbers exactly, they only store approximations of them. The reason is that some numbers, like Pi, would require infinite memory to store exactly.
We can see this effect by giving Python certain computations like the following:
>>> 0.1 + 0.2 0.30000000000000004
To read in a float from the user, we can't just use the
command. This always gives a string, even if the user types in a number:
>>> num = input("Enter a number ") Enter a number 6 >>> type(num) <class 'str'>
Because it's a string, we can't really use it like a number. For example, if we try to add "1" + "2", we'll end up with "12" instead of 3.
In order to actually get a number from the user, we need to convert it into either an int or float first. This can be done by putting int() or float() around the input line, like this:
num1 = int(input("Enter an integer ")) num2 = float(input("Enter a float "))
What happens if we put in something that is not a number?
Once we have numbers inside a Python program, there are lots of things we can do with them. We saw that + can be used to join together strings. With numbers it adds them together.
Python uses these operators for common math calculations:
|%||Modulus (remainder division)|
The % operator is the most unusual in this list. It is what you get
as a remainder when doing division. For example, if you divide 5 by 2,
you get 2 with 1 left over. So
5 % 2 is equal to 1.
Notice that exponents are done with
**. A common mistake
is to use ^ instead.
Python follows the standard mathematical rules of precedence which can be overridden with parenthesis.
Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.