Concepts / Expressions and Operations

Expressions and Operations

A value is one of the basic things a program works with, like a letter or a number.

  • 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.

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.

belongs tobelongs tobelongs to42intintwhole number"42"strstrtext42.0floatfloatdecimal number
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.

containsends before'string beginsHellotext content'string ends
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.

inputreturns42valuetype()checks the valueintdetected type
How does a value move into the type() function, and how does the function identify int, str, or float?

Classifying three values

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

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

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

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

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

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

Reading Values Correctly

  • Treating '42' as the integer 42

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

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

  • Treating 5.0 as the integer 5

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

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

  • Forgetting quotation marks around text

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

    Fix: Enclose every string in quotation marks.

  • Guessing a type instead of checking it

    Visual similarity does not make two values the same type.

    Fix: Use type() whenever you are uncertain.

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. A value is one of the basic things a program works with, such as a letter or a number.
  2. Integers are whole numbers, strings are quoted text, and floats are decimal numbers.
  3. Quotation marks distinguish text from numbers and other kinds of values.
  4. The values 42, '42', and 42.0 look related but belong to int, str, and float respectively.
  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.