2.1. Values and types

A value is one of the basic things a program works with, like a letter or a number. You can print values in Python. See what happens when you run the following code.

These values belong to different types: 17 is an integer, and “Hello World!” is a string, so called because it contains a “string” of letters. You can identify strings because they are enclosed in quotation marks.

If you are not sure what type a value has, use the type function to find out.

Not surprisingly, strings belong to the type str and integers belong to the type int. Less obviously, numbers with a decimal point belong to a type called float, because these numbers are represented in a format called floating point.

What about values like “17” and “3.2”? They look like numbers, but they are in quotation marks like strings.

They’re strings. We can check this using the active codeblock below.

When you type a large integer, you might be tempted to use commas between groups of three digits, as in 1,000,000. This is not a legal integer in Python, but it is legal:

Well, that’s probably not what you expected! Python interprets 1,000,000 as a comma-separated sequence of integers, which it prints with spaces between.

This is the first example we have seen of a semantic error: the code runs without producing an error message, but it doesn’t do the “right” thing.

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

You have attempted 1 of 8 activities on this page