for now, a value is a number or a string (we'll see more types of values later)
it can be stored in a variable
it can be part of an expression (a combination of values, operators, functions, etc … that can be evaluated)
but it can't be evaluated any further on its own (2 + 3 is not a value, because it can be evaluated further)
some examples of values:
-123456
"a string is a value"
A Note on Values in Code
The representation of a bare value in code is called a literal.
"a string " a string literal
254 - an integer literal
we'll go over these types in the next few slides
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
str (string)
int (integer)
float (floating point)
complex (complex numbers)
The last 3 are numeric types.
Strings
What's a String Again?
A string is a sequence of characters.
any characters
including spaces and punctuation
must start and end with quotes!
Unbalanced Quotes
What do you think will happen if you have an extra quote?
Let's give it a try: →
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 quotes
single quotes
triple double quotes for multiline strings
Multiline Strings?
triple double quotes allow you to span multiple lines
single or double quotes do not
What do you think will happen if you try spanning multiple lines with single or double quotes? →
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:
prefixed with the # token
or surrounded by triple double quotes as a bare string literal
These are both comments: →
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?
you can use the backslash character before the special character
for quotes, you can use mixed quotes (embed single quotes in a double quoted string)!
let's give it a try… →
I Heard You Like Backslashes
What if you want an actual backslash in your string? →
You can use backslashes to escape backslashes
New Lines (Again)
You can actually represent a new line as a character by using:
that's backslash, then n, or the newline character
so… a multiline string can also be created by using newline characters:
On a Tropical Island
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.→
Numeric Types
Three Numeric Types
The following types are all closely related; most of the same operations can be applied to all of them:
int (integer)
float (floating point)
complex (complex numbers)
int (integer)
What's an integer?
integer - whole number, can be negative
int is the actual name of the integer type
24, -25 →
no size limit (well, as much as your computer can handle)!
for example: 1337 ** 20 →
float (floating point)
What's a floating point number?
A floating point number represents real numbers
…but what's a real number? →
a quantity across a continuous line, like fractions or irrational numbers like pi or the square root of two
floating point numbers are indicated by a decimal point: 5.55555, 5.0 →
Really Big or Really Small Numbers
Very large or small floating point numbers are expressed in scientific notation
5555 ** 5.0 gives back 5.289569361832972e+18 (that is 5.2389 … times 10 to the 18th power) →
the exponent can be + or -
a few more examples:
52e+3 = 52 * 10 ^ 3 = 52000
52e-3 = 52 * 10 ^ -3 = 0.052
What is the result of 2e+2? →
200.0
Uh-oh - That's Too Much
unlike integers, floats have min and max values… if you have a value that's too big or too small
sys.float_info.max →
5555**55555.0 →
Complex Numbers
For completeness… Python supports complex numbers
numbers with square root of -1 / imaginary numbers
1j * 1j →
Don't, Use, That, Comma
Clearly, symbols have special meanings in numeric types
a decimal point signifies a floating point number
scientific notation is represented by e and a plus or minus
j is the imaginary part of a complex number
However - Don't use commas! They don't mean what you expect. →
So You Don't Know What Type You Have
There's a function called type
Let's look at it in the interactive shell →
Notice the arrow… it says it returns the "type" of the parameter passed in…
Here's how you would use it: →
A Guessing Game
1
1.0
"1"
"""1.0"""
1.111
'1,111'
1 is an int. 2 and 5 are floats. Everything else is a string.
Reeeeeewind
What do we call a bare value in code… like "foo" or 5?
Name three ways to represent a syntactically correct string in Python
Name two ways to put a double quote in a string
How about a backslash, or newline?
Which type can be affected by really large or small numbers - float or int?
How can I tell if a number is a float?
What function could I use to determine the type of a value or variable?