Concepts / Expressions and Operations

Expressions and Operations

Values are not all the same. Python needs to know whether a value is a whole number, text, or a decimal number so it can apply the appropriate rules.

  • Programming

Values at the Starting Line

A value is one of the basic things a program works with, such as a letter or a number. In a simple Python program, the values 1, 2, and 'Hello, World!' are already enough to show the raw material that programs can manipulate, store, and display. Before working with larger programs, learn to recognize what a value is and how Python classifies it.

Key point

Values are not all the same. Python needs to know whether a value is a whole number, text, or a decimal number so it can apply the appropriate rules.

Three Ways to Represent Data

Python organizes values into types. The three types in this lesson are integers, strings, and floats. An integer is a whole number, including negative whole numbers and zero. A string is text enclosed in quotation marks. A float is a decimal number, identified by the presence of a decimal point.

42intintwhole number"42"strstrtext42.0floatfloatdecimal numberbelongs tobelongs tobelongs to
What is the difference between 42, "42", and 42.0, and how does each value belong to a different Python type?

Quotation Marks as Boundaries

Quotation marks tell Python where a string begins and ends. They also tell Python that the enclosed content is text, not a variable or a number. This is why '42' and 42 are different values: the first is text because it is enclosed in quotation marks, while the second is an integer.

'string beginsHellotext content'string endscontainsends before
How do quotation marks show where a string begins and ends, and what changes when they are removed?

Checking a Value with type()

When you are unsure about a value's type, use the type() function. It takes a value as input and returns the type that value belongs to. For example, type(42) identifies an integer, type('42') identifies a string, and type(42.0) identifies a float.

42valuetype()checks the valueintdetected typeinputreturns
How does a value move into the type() function, and how does the function identify int, str, or float?
Worked example

Classifying three values

Identify the type of 17, '17', and 17.0, then verify each classification with type().

  1. 1
    First value

    17 has no decimal point and no quotation marks, so it is an integer. Python identifies it as int.

  2. 2
    Second value

    '17' is enclosed in quotation marks, so it is a string. Python identifies it as str.

  3. 3
    Third value

    17.0 contains a decimal point, so it is a float. Python identifies it as float.

  4. 4
    Verification

    Apply type() to each value: type(17), type('17'), and type(17.0). The function verifies the three classifications.

Result

17 is an integer, '17' is a string, and 17.0 is a float.

Reading Values Correctly

  • Common mistake

    Treating '42' as the integer 42

    What it looks like

    '42' has quotation marks, while 42 does not.

    Why it fails

    Quotation marks make the first value a string. Python treats strings and integers as different types.

    Do this instead

    Check whether quotation marks are present before deciding that a value is a number.

  • Common mistake

    Treating 5.0 as the integer 5

    What it looks like

    5.0 and 5 look similar, but only 5 has no decimal point.

    Why it fails

    A decimal point makes 5.0 a float, even when its decimal part is zero.

    Do this instead

    Look for the decimal point. No decimal point means integer; a decimal point means float.

  • Common mistake

    Forgetting quotation marks around text

    What it looks like

    Writing Hello instead of 'Hello'.

    Why it fails

    Quotation marks tell Python that the content is text rather than a number or variable.

    Do this instead

    Enclose every string in quotation marks.

  • Common mistake

    Guessing a type instead of checking it

    What it looks like

    A value may look like a number but still be text, as in '42'.

    Why it fails

    Visual similarity does not make two values the same type.

    Do this instead

    Use type() whenever you are uncertain.

Best practice

When reviewing a value, use a three-part check: look for quotation marks, look for a decimal point, and use type() to verify your conclusion. This habit helps you find incorrectly represented values before they cause unexpected program behavior.

Type Identification Practice

EASY

For each value, decide whether it is an integer, string, or float. Then verify your answers with type(): -5, 'abc123', 0.5, 0, 'Hello!', and 99.99.

Hints
  • A whole number with no decimal point is an integer.
  • Quotation marks identify a string.
  • A decimal point identifies a float.

A Reliable Foundation

  1. 1
    A value is one of the basic things a program works with, such as a letter or a number.
  2. 2
    Integers are whole numbers, strings are quoted text, and floats are decimal numbers.
  3. 3
    Quotation marks distinguish text from numbers and other kinds of values.
  4. 4
    The values 42, '42', and 42.0 look related but belong to int, str, and float respectively.
  5. 5
    The type() function verifies the type of a value when you are uncertain.

Key Takeaways

  • Values are the basic pieces of data that programs work with.
  • Python distinguishes integers, strings, and floats.
  • Quotation marks identify strings and prevent text from being treated as a number or variable.
  • A decimal point makes a value a float, even when the decimal part is zero.
  • Use type() to verify the type of any value.

Related concepts

More concepts starting with E