CSci 150: Foundations of computer science
Home Syllabus Readings Projects Tests

Types & I/O

Types

A type is a class of values that a Python program can work with. We've worked with strings, which constitute one type, and numbers, which constitute… well, actually they constitute two types, integers and floating-point numbers.

The difference between integers and floating-point numbers is in how they are represented internally, which has an effect on their behavior. Integers are whole numbers like 4359, 0, and −10, with no decimal point or fractional value. Floating-point numbers can have a decimal point, but unlike integers they are approximations (though correct to fifteen decimal places). Note that there is a floating-point number 2.0 — it has no fractional part, but the floating-point representation can work with it.

How does Python decide whether a number is an integer or floating-point value? First, in how you type it: If you type it with a decimal point (like 2.0), it is a floating-point value, but a non-decimal number (like 2) is an integer. When you perform arithmetic on two integers, you get an integer as a result — unless you do regular division using ‘/’, in which case you get a floating-point result. If you perform arithmetic where one or the other side is floating-point, you get a floating-point result.

On the whole, you don't have to worry about this distinction between integers and floating-points, but every once in a while the difference will rear its ugly head. For example, “2 ** 2000” and “2.0 ** 2000” work very differently: The first gives you the number you expect, while the second results in an overflow error, since the floating-point representation isn't built for numbers beyond approximately 1.8 × 10308.

Conversion functions

Sometimes you have values of one type that you want to convert into a value of another type. For that, Python has several “conversion functions”.

coderesult
float(x) the conversion of x to a floating-point number
int(x) the conversion of x to an integer
str(x) the conversion of x to a string

Some examples of using these conversion functions.

digits = '234'
num = int(digits)        # num is now 234, digits is still string '234'
digits2 = 2 * digits     # digits2 is now the string '234234'
num2 = 2 * num           # num2 is now the integer 468
num2f = 2 * float(digits# num2f is the floating-point number 468.0

result = 'Doubled: ' + str(2 * num)  # result is 'Doubled: 468'

That last line is notable: Python will not allow you to add a string and a number, so “result = 'Doubled: ' + (2 * num)” will simply lead Python to reject the program with a run-time error. To get the number 468 treated as a string that can be added to another, we need to use the str function.

I/O

Sometimes we want to interact with the user. The easiest approach to this is a typewriter-like interface, used by old-style computers: Sometimes the program displays new text for the user to see, and at other times the program reads a line typed from the user. Python supports this type of interface using two functions.

coderesult
input() returns the next line of user input, as a string
print(x) displays x on the screen (no return value)

Here is a very simple program using these.

print('What is your name?')
name = input()
print('Hi, ' + name + '!')

When executed, this is what appears (user input in boldface):

What is your name?
Somebody
Hi, Somebody!

Since we'll often want to read a number from the user, it can be helpful to use the int or float function in conjunction with input.

print('How old are you?')
age = int(input())
print('Next year you will be ' + str(age + 1))

Here is a sample run.

How old are you?
20
Next year you will be 21

In execution, the input function returns the string typed by the user — the digits 2 and 0 — which the program passes as an argument to int, which returns the number 20. In the third line, we compute 20 + 1, then pass the result of 21 to str to get the string with the digits 2 then 1, which is then added to another string. The usage of str is important: Python will not allow us to add a string and an integer together.