0%

Chapter 2

Values and Variables

Meet Python's Values

In the opening chapter, you used print() to show messages. Those messages were made from values.

A is one piece of information that a program can work with. Python programs can work with words, numbers, dates, pictures, and much more. We will begin with three kinds of value that appear in everyday programs.

Text, whole numbers, and decimal numbers

Run this example and look at the three values Python prints:

Try it

Python puts each result on its own line, but the values are not all the same kind.

Python nameWhat it representsExamples
strText, also called a "Hello", "A cup of tea", "42"
intA whole number, also called an integer3, 0, -12
floatA number with a decimal point2.5, 0.0, -1.25

The quotation marks around a string are part of the Python code. They tell Python where the text begins and ends. Python does not show those quotation marks in the output.

Numbers are written without quotation marks when you want Python to treat them as numbers.

Quotation marks change the meaning

These two values look related, but Python treats them differently:

42
"42"

The first is the whole number forty-two. The second is text containing the characters 4 and 2.

That difference becomes visible when Python sees a plus sign:

Try it

In the first instruction, Python adds two numbers. In the second, everything between the quotation marks is text, so Python prints it literally.

Which instruction gives Python the text value 42?

Python calls these categories types

The kind of a value is called its type. A value’s type helps Python decide what it can do with that value.

You can ask Python for the type of a value with type():

Try it

Python reports str, int, and float. The word class in that output is part of Python’s technical description. You do not need to understand classes yet.

Deep dive

str is short for string. int is short for integer. The name float comes from “floating-point number,” a common way computers store decimal values.

Decimal numbers inside a computer have a few surprising limits. We will deal with those only when they become relevant. For now, float is the useful name to recognize.

One useful habit

When code behaves differently from what you expected, ask yourself: “What value is Python working with, and what type is it?”

You do not need to memorize every Python type. For now, make sure you can recognize text in quotation marks, whole numbers, and decimal numbers. In the next lesson, you will learn how to give a value a name so a program can use it again.

A quick note about Monty

The free Monty preview covered the opening chapter. If your plan includes Guided, Monty remains available for hints, explanations, and code reviews. If not, you can complete every exercise with the lesson, runnable examples, and focused submission feedback.

Task

Run the examples and make sure you can identify a , an integer, and a floating-point number. Complete the quiz, then press Next to continue.