Values, Types, Variables Review

Previously on …

Values

What's a value? →

A value is just data!

  1. it can be stored in a variable
  2. it can be used an an expression
  3. it can't be evaluated further (2 + 3 is not a value; it can be evaluated further)

Types

What's a type? →

A type is just a kind or category of values. Name the types that we learned about. →

  1. str (string)
  2. int (integer)
  3. float (floating point number)
  4. complex

Basic Data Types - Strings

str - strings

What's a string? How do we know a literal value is a string? →

String Operators

Name two operators that can be used with strings

String Operators Continued

What do the following lines of code output (error is a possible answer)?

print("three" + " blind" + " mice")
print(3 + " strikes")
print(3 * "ring ")
print("hey" * "three")
three blind mice

TypeError: unsupported operand type(s) for +: 'int' and 'str'

ring ring ring

TypeError: can't multiply sequence by non-int of type 'str'

Basic Data Types - Integers

int - integers

What's an int? →

Basic Data Types - Floating Point Numbers

float - floating point numbers

What's a float? How do we know a literal value is a float? →

Operators for Numeric Types

Name 7 operators that can be used on floats and/or ints. →

  1. addition: +
  2. multiplication: *
  3. subtraction: -
  4. division: / (results in a float even if two integers - ex 10 / 2 → 5.0)
  5. integer division: // (closest integer to the left - ex -23 // 3 → -8)
  6. modulo: % (ex 10 % 7 → 3)
  7. exponentiation: ** (ex 2 ** 3 → 8)

Operators for Numeric Types Continued

The following block of code will cause a run time error. Why?

a = "20"
print(a + 5)

A Type Error will occur… the operator, plus, doesn't support int and str. What can be done to fix this?

One fix is to convert the variable, a, into an integer using the int function:

a = "20"
print(int(a) + 5)

Type Conversions

Name three functions that can be used to convert a value from one type to another.

  1. int
  2. float
  3. str

Type Conversions: a Closer Look

The following functions will attempt to convert the parameter(s) passed in into the type that the function is named after:

Type Conversions Continued

Variables

Variables allow us to bind names to values so that the variable name can be used in place of the literal value. In code, how would we assign the value 12 to a variable called n?

n = 12

Valid Variable Names

and       del       from      not       while
as        elif      global    or        with
assert    else      if        pass      yield
break     except    import    print
class     exec      in        raise
continue  finally   is        return 
def       for       lambda    try

And on to Built-In Functions…