Data: Values, types and expressions and Variables

Values and Types

Every value has a type

  • Value (data) types in python:
    - Integers (type int)
    - Floating-point numbers (type float) - Strings (type str)
    - Truth values (type bool)
    - …and so on.
  • Types determine what we can do with values
  • The type() function tells us the type of a value:

Numeric types
int

  • Integers (type int) represent positive and negative whole numbers (0, 1, 2, −1, −17, 4096, . . .).
  • Values of type int have no inherent size limit.
  • Note: Can’t use commas to “format” integers (must write 1282736, not 1,282,736).

float

  • Floating-point numbers (type float) represent decimal numbers.
  • Values of type float have limited range and limited precision.
    - Min/max value: ±1.79e308.
    - Smallest non-zero value: 2.22e−308.
    - Smallest value > 1: 1 + 2.22e−16.
  • Type float also has special values Data: Values, types and expressions and Variables and Data: Values, types and expressions and Variables (not a number).
  • Every decimal number is a float:
    >>> type(1.5 - 0.5)
    <class ’float’>
  • The result of division is always a float:
    >>> type(4 / 2)
    <class ’float’>

Strings

  • Strings (type str) represent text.
  • A string literal is enclosed in single or double quote marks:
    >>> "Hello world"
    ’Hello world’
    - A string can contain other types of quote mark, but not the one used to delimit it.
    >>> ’4" long’
    ’4" long’

Type conversion

  • Explicit conversions use the type name like a function:
    >>> int(2.0)
    >>> float(" -1.05")
    >>> str(0.75 * 1.75)
  • Conversion from str to number only works if the string contains (only) a numeric literal.
    >>> float("12s")
    ValueError: could not convert string to float: '12s'
  • Conversion from int to float is automatic.
    - E.g., int times float becomes a float.

Numeric operators in python
Data: Values, types and expressions and Variables

Precedence

  • There is an order of precedence on operators, that determines how an expression is read:
    - -1 ** 5 means -(1 ** 5), not (-1) ** 5.

Math functions

  • The math module provides standard math functions, such as square root, logarithm, trigonometric functions, etc.
  • Almost all math functions take and return values of type float.

Comparison operators
Data: Values, types and expressions and Variables

  • Can compare two values of the same type (for almost any type).
  • Comparisons return a truth value (type bool), which is either True or False.
  • Conversion from any type to type bool happens automatically, but the result may not be what you expect.

Expressions

  • ((523 - 485) / 485) * 100 is an expression

Variables

  • A variable is a name that is associated with a value in the program.
    - The python interpreter stores name–value associations in a namespace.

Valid names in python (reminder)

  • A (function or variable) name in python may contain letters, numbers and underscores ( ), but must begin with a letter or undescore.
  • Reserved words cannot be used as names.
  • Names are case sensitive: upper and lower case letters are not the same.
    - Length_Of_Ropeand length_of_rope are different names.