Values and Types

Introduction to CMPUT 175 Python Review

  • Overview of the review material focusing on basic values and types in Python.

Basic Data Types in Python

Integers (int)

  • Definition: Integers are whole numbers that can be positive or negative without a decimal point.

  • Storage: Integers can have as many digits as the memory allows.

  • Basic Operations on ints:

    • Addition: 3 + 4

    • Subtraction: 9 - 5

    • Multiplication: -34 * 3

    • Division (floor division): 7 // 2

    • Exponentiation: 2 ** 128

    • Modulo operation: 32 % 3

      • Explanation of Modulo:

        • Returns the remainder after integer division.

        • Results always between 0 and one less than the divisor (right value):

          • Example: 1 % 3 = 1, 2 % 3 = 2, 3 % 3 = 0, etc.

        • Useful in data structures and algorithms for wrapping behavior.

Floating-Point Numbers (float)

  • Definition: Floats are finite approximations of real numbers that include decimal points.

  • Basic Operations on floats:

    • Addition: 5.0 + -2.4

    • Multiplication: 4.9 * 2

    • Subtraction: 0.2 - 0.5

    • Division: 7.0 / 2.0 (note automatic conversion of ints to floats when using / operator).

  • Type Determination:

    • Use type() function to determine data types:

      • type(3) gives int

      • type(3.0) gives float

      • For division, type(7 // 2) is int but type(7.0 / 2) is float.

Text Representation

Strings (str)

  • Definition: Strings are sequences of characters enclosed in quotes (single or double).

  • Accessing Characters:

    • Indexing starts from zero, e.g., for string "Eddie", use:"Eddie"[0] to access the first letter.

  • String Length: Use the len() function:

    • Example: len("Eddie") returns 5.

  • Indexing: The last character is accessed with index one less than the length, e.g., "Eddie"[4].

String Operations

  • Membership Testing: Check if a substring exists within a string using in operator.

    • Example: "Ed" in "Eddie" is True, while "ed" in "Eddie" is False.

  • Case Sensitivity: Strings are case sensitive. Use .lower() to convert a string to lowercase and .upper() for uppercase.

  • String Concatenation: Use the plus operator to join strings, e.g., "CMPUT" + "175".

Special Value: None

  • Definition: None represents the absence of a value.

    • Recognizable by its capitalization.

    • Returns no output when evaluated in Python's interactive console.

  • Future Relevance: None will be useful for representing nothingness in computations as discussed in future lessons.

Summary of Concepts

  • Integer and floating-point representations in Python using int and float types.

  • String manipulation using str type.

  • Importance of None for value representation in applications.