Values, Types, and Comments

Values

What are Values?

A Note on Values in Code

The representation of a bare value in code is called a literal.

Data Types

Values can be Classified by Data Type

A data type is a set of values. The type of a value determines how it can be used in expressions. Sometimes data type is also referred to as just type… or class.

Today, we'll go over the following 4 types

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

The last 3 are numeric types.

Strings

What's a String Again?

A string is a sequence of characters.

Unbalanced Quotes

What do you think will happen if you have an extra quote?

Let's give it a try: →

>>> "oops " I quoted again"
SyntaxError: invalid syntax

If you don't have matching start and end quotes (unbalanced quotes) you'll get a syntax error.

Alternate String Syntax

You can use three different types of quotation marks:

"double quoted string"
'single quoted string'
"""more than
one line.  omg!"""

Multiline Strings?

What do you think will happen if you try spanning multiple lines with single or double quotes?

>>> "spanning multiple
SyntaxError: EOL while scanning string literal

A Quick Aside on Comments

A comment is text in a program that is meant for the human reader; it isn't used by the interpreter. A comment can be:

These are both comments: →

#   this is a comment
"""
so
is
this
"""

A Hasty Escape

What if I want to put in a character that has special meaning in a string? Say, for example… a single or double quote?

print("escaping using \"backslash\"")
print("single  quotes ''''") 
print("""some "double quotes"""")

I Heard You Like Backslashes

What if you want an actual backslash in your string?

You can use backslashes to escape backslashes

print("I heard you like \\'s, So I put a \\ before your \\")

New Lines (Again)

You can actually represent a new line as a character by using:

\n
print('two\nlines\n')

On a Tropical Island

jake

On a Tropical Island Lyrics

These are lyrics to a song called "On a Tropical Island"… from a fine cartoon named Adventure Time. It has a bunch of new lines in it, as well as single quotes.

How would you get Python to print out these lyrics? Let's find 2 different ways to print this out.

On a tropical island,
Underneath a molten lava moon.
Hangin' with the hula dancers,
Askin' questions cause' they got all the answers.

Numeric Types

Three Numeric Types

The following types are all closely related; most of the same operations can be applied to all of them:

  1. int (integer)
  2. float (floating point)
  3. complex (complex numbers)

int (integer)

What's an integer?

float (floating point)

What's a floating point number?

A floating point number represents real numbers

…but what's a real number?

Really Big or Really Small Numbers

Very large or small floating point numbers are expressed in scientific notation

Uh-oh - That's Too Much

>>> 5555**55555.0
Traceback (most recent call last):
  File "<pyshell#93>", line 1, in <module>
    5555**55555.0
OverflowError: (34, 'Result too large')
>>> 

Complex Numbers

For completeness… Python supports complex numbers

Don't, Use, That, Comma

Clearly, symbols have special meanings in numeric types

However - Don't use commas! They don't mean what you expect.

>>> 3,000
(3, 0)
>>> # huh???
>>> # it's something called a tuple

So You Don't Know What Type You Have

type(1.0)

A Guessing Game

  1. 1
  2. 1.0
  3. "1"
  4. """1.0"""
  5. 1.111
  6. '1,111'

1 is an int. 2 and 5 are floats. Everything else is a string.

Reeeeeewind

Next: Operations and Variables