In this video, I introduce a category of types called sequence types,
this category includes the string type and
other types which will be introduced in future lessons.
I also introduce the subscription expression,
which is a mechanism for referring to elements of a sequence.
A sequence is a finite ordered group of elements where each object has a numerical
index between 0 and 1 less than the number of elements in the sequence.
There is no sequence type in Python.
Instead, sequences are category that include several different Python types.
The Python STR type is one kind of sequence.
Each string is a finite ordered group of indexed characters.
For example, the string object hello is a sequence of characters.
Where h is element 0, e is element 1,
l is both element 2 and element 3, and o is element 4.
Remember that Python always starts it's indexes at 0 instead of 1.
I can use a new kind of Python expression called a subscription
expression to reference an element of a sequence.
Subscription uses 2 delimiters.
Left bracket and right bracket.
Python subscription is analogous to using subscripts in mathematics.
For example, I can reference the string element of hello at index one,
using this expression which is pronounced hello of one.
In many programming languages,
the elements of a string are objects whose type is char which is short for character.
However, there's no chart type in Python.
Let's find out what type of elements Python uses in a string.
The type str was displayed.
It appears that elements in a Python string are one element string objects.
However, Python string elements are characters that have an internal
representation that do not have a Python type.
When you use subscription to reference one of these characters,
the interpreter returns a one element string object.
This is a simplified syntax diagram for the subscription expression,
and here is a generalized syntax diagram for expression that includes subscription.
The simplified semantics rule for subscription is
evaluate the left expression to get the primary object.
Evaluate the bracketed expression to get the index object.
If the primary object is not a sequence, report a subscription error.
If the index object is not a non-negative integer less than
the sequence length report an indexing error.
Reference the primary object's element at the index.
The expression hello of 1 is lexically and syntactically correct.
I will apply the semantic rule for subscription.
In step 1,
the left expression is evaluated to obtain the primary object, hello.
In step 2, the bracketed expression evaluates to the object 1.
In step 3, the primary object has type str which is a sequence,
so no subscription error is reported.
In step 4, the int object 1 is a non-negative integer that is less
than the string length 5, so no indexing error is reported.
In step 5, the primary object is hello so
the interpreter returns the element at index one.
This is the single element string e not each which is at index 0.
This expression is also lexically and syntactically correct.
I will apply the semantic rule for subscription.
In step 1, the left expression is evaluated to obtain the primary object 27.
In step 2, the bracketed expression is evaluated to obtain the index object 1.
In step 3, the primary object has type int, which is not a sequence.
So a semantic type error reports that the integer is not
subcscriptable and evaluation stops.
Here is another lexically and
syntactically correct subscription expression.
I will apply this semantic rule for subscription.
In step 1, the left expression is evaluated to obtain the primary object,
hello.
In step 2, the bracketed expression is evaluated to obtain the index object, 1.
In step 3, the primary object is type STR which is a sequence.
So no subscription error is report.
In step 4, the string object 1 is not a non-negative integer.
So a semantic type error reports that the index must be an integer and
evaluation stops.
Here is yet another lexically and
syntactically correct subscription expression.
I'll apply the semantic rule for subscription.
In step 1,
the left expression is evaluated to obtain the primary object, hello.
In step 2, the bracketed expression is evaluated to obtain the index object, 5.
In step 3, the primary object has type STR which is a sequence.
So no new subscription error is reported.
In step 4, the INT object five Is a non-negative integer,
but it is not less than the string length which is also 5.
So a semantic indexing error reports that the index
is out of range and evaluation stops.
The reason the largest index must be strictly less than the length
instead of less than or equal to the length is that sequences are indexed
from 0 to 1 less than the sequence length.
I know I have discussed indexing from 0 instead of 1 several times,
but you will probably make indexing errors anyway.
I still make indexing errors.
In the subscription semantic, step four 4 indicates that
the interpreter reports an indexing error for all negative integers,
however, the beltway sequence types can handle some negative index.
In this expression, the last element o is returned,
this is done by adding the index -1 to the strength length
5 to obtain the index 4 and returning the element o at this index.
Using an index of- n yields the nth last element.
This can sometimes be useful.
However, if you use a negative index whose magnitude is too large,
you will still get an indexing error.
For example, hello[-6] is an error because there is no sixth
last element of hello, which has only 5 characters.
In this video, I introduced a category of types called sequence types.
This category includes the string type and
other types that will be introduced in future lessons.
I also introduce the subscription expression which
you can use to refer to elements of a sequence.