Execute Program

Python for Programmers: Basic Data Types

Welcome to the Basic Data Types lesson!

This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!

  • Python's basic data types include integers, floating point numbers, strings, and booleans. These mostly work in familiar ways, so we'll only discuss them briefly.

  • Python's Boolean values are True and False. Note that they're not true and false! Normally, UpperCamelCased names indicate classes in Python, but this is an exception. It's an inconsistency on Python's part.

  • >
    1 == 1
    Result:
    TruePass Icon
  • >
    2 > 3
    Result:
    FalsePass Icon
  • Many other languages, most notably JavaScript, use C-style logical operators. In those languages, && means "and", || means "or", and ! means "not". Python's operators are different: it uses the words and, or, and not.

  • >
    True and True
    Result:
    TruePass Icon
  • >
    1 + 1 == 2 and 2 + 2 == 3
    Result:
    FalsePass Icon
  • >
    False or True
    Result:
    TruePass Icon
  • >
    1 > 2 or 3 == 4
    Result:
    FalsePass Icon
  • >
    not True
    Result:
    FalsePass Icon
  • >
    not not True
    Result:
    TruePass Icon
  • Although Python doesn't use the more common && operator style, its precedence rules match most other languages. As an example, not x or y is equivalent to (not x) or y.

  • >
    not True or True
    Result:
    TruePass Icon
  • >
    (not True) or True
    Result:
    TruePass Icon
  • >
    not (True or True)
    Result:
    FalsePass Icon
  • We can use the bool built-in function to convert other types into booleans. Any zero number (like 0 or 0.0) converts to False, and any non-zero number converts to True.

  • >
    bool(1.5)
    Result:
    TruePass Icon
  • >
    bool(0.0)
    Result:
    FalsePass Icon
  • >
    bool(37)
    Result:
    TruePass Icon
  • >
    bool(0)
    Result:
    FalsePass Icon
  • Strings work similarly. Empty strings convert to False and non-empty strings convert to True.

  • >
    bool("abc def")
    Result:
    TruePass Icon
  • >
    bool("")
    Result:
    FalsePass Icon
  • Note that whitespace characters are still characters. A string with only whitespace converts to True.

  • >
    bool(" ")
    Result:
    TruePass Icon
  • We can use any value in boolean expressions. For example, not value works even if value isn't a boolean. In that case, Python uses the bool built-in to automatically convert value into a boolean. It's as if we did not bool(value). (In a future lesson, we'll come back to this idea of "truthy" and "falsey" values in more detail.)

  • >
    not " "
    Result:
    FalsePass Icon
  • Python distinguishes between integers and floating point numbers. Integers are whole numbers: positive, negative, and 0. Floats contain decimal points, like "real numbers" in a math class. However, floats don't guarantee precision. A future lesson will cover floating point precision in more detail.

  • Any operation involving a float returns a float.

  • >
    2.0 * 3.0
    Result:
    6.0Pass Icon
  • Combining integers and floats gives us a float, even if the result is a round number.

  • >
    3 * 1.5
    Result:
    4.5Pass Icon
  • >
    5 * 2.0
    Result:
    10.0Pass Icon
  • >
    10 + 3
    Result:
    13Pass Icon
  • Python has the usual operators like +, *, etc. It also has a convenient exponentiation operator, **, which many languages lack.

  • >
    3**2
    Result:
  • >
    2**3
    Result:
    8Pass Icon
  • We can compare integers against floats, like some_int == some_float. However, floating point imprecision means that this is much more subtle than it seems. We'll see more details of floating point imprecision in a future lesson.

  • >
    2 == 2.0
    Result:
    TruePass Icon
  • Python has special syntax for scientific notation with an explicit exponent. For example, 2.1e3 is equivalent to 2.1 * (10**3), or 2.1 * 1000.

  • >
    2.1e3
    Result:
    2100.0Pass Icon
  • This syntax always gives us floats. The 2e2 in the next example doesn't include a decimal point, but it's equivalent to 2.0 * 100.

  • >
    2e2
    Result:
    200.0Pass Icon
  • Scientific notation also allows negative exponents. 1.2e-1 gives the equivalent of 1.2 * (10**-1), or 1.2 / 10

  • >
    1.2e-1
    Result:
    0.12Pass Icon
  • 0.12 and .12 are equivalent in Python, so either one is a correct answer for that code example.

  • >
    0.12 == .12
    Result:
  • Strings go inside single or double quotes. (Though Python also has some additional string syntax that we'll see later.) We can get the length of a string with the len function.

  • >
    message = "Hello"
    len(message)
    Result:
    5Pass Icon
  • We can combine strings with +.

  • >
    name = "Amir"
    greeting = "Hello, " + name + "!"
    greeting
    Result:
    'Hello, Amir!'Pass Icon
  • We can escape characters inside of strings with \, which makes it clear that we're including a special character. For example. "\"" is a string containing a single character, a double quote.

  • >
    len("\"")
    Result:
    1Pass Icon
  • In this case, it'd be better to use a single-quoted string so we don't have to escape the " character at all.

  • >
    '"' == "\""
    Result:
  • Those are the basic types! Now we can move on to the deeper details that make Python Python.