Lab Lectures
Lab Lectures
Text Editor
◦ https://atom.io/
if you just run Python, it will take, interactively,
commands that you can type.
This assignment statement is something that often confuses people when they
move from math to programming.
Well, these are words that if we use these words, we must use
them to mean the thing that Python expects them to mean.
something, something, maybe not, maybe - and you have this "if".
If this is true, do this statement; if it's false, do some other
statement.
Python loves this code.
Python thinks great. I don't know why you picked those
variables, I do not care. All I care is that they're unique.
Python really has no opinion about these variables.
Python carefully tracks not only what the value in a
variable is but what kind of a value it is.
So is it a string, is it an integer, is it a floating point?
We talked about constants, variables, reserved
words, type, mnemonic variable names
So you have to sort of think why did you increase the indent, which is like if, after
an if or a for or a while statement, which we'll see in a bit. You maintain the
indent, which means you stay the same, or then you de-indent or you reduce the
indent back, and that is the way to end a block.
The problem is is that Python can get very confused if you sometimes
are using four spaces, space, space, space, space, and sometimes you use tab.
And the danger is it can look right on your screen, but Python will still complain to
you.
And so if you're using a text editor that might be putting tabs into your document
you can see where the if starts and then it's the line that lines back up with the
for, that is the scope of the entire for loop. Same here with the if. The thing that
lines up, that is the scope of that if block.
So mentally, you have to realize that the de-indenting and indenting adds
meaning to your program.
Try ends in a colon, which means it's an indented block of code.
And then we put the dangerous line in there, and then we put except.
Or if it goes bad, it's going to run, blow up, and then run this stuff and
then continue on.
So it's kind of like if things work out, do this,
if things don't work out, do this other thing.
.
We're going to read a number from the user and print out either Nice work or Not a number.
And so, we take an input statement, which stops and waits for us to type, and then we type 42.
And 42 goes into rawstr and then we know that this int is dangerous.
Right?
And this rawstr came from the user, whatever the user typed.
And so we put it in the try block, and if it's 42, it converts and it says ival is greater than 0, we
print it out, so it says Nice work.
We as the programmer had no control over what our crazy user was typing, right?
You're starting to be a programmer and crazy users do crazy things to your poor programs,
even if they're only like seven lines long. So we got forty-two coming in here,
we know this is going to blow up, this int is going to blow up with a traceback.
what we talked about is:
We're passing in a string and we're asking max finds the largest of
something and in this case it's going to scan through this string
and find the largest letter.
The basic idea is that at some point in the
code, you go back up,
The idea is that you've done something, let's
go do that thing again.
And the way we express that in Python is with
a couple of keywords.
One of the keywords is the while keyword and
the for keyword.
Now, we have some statements that we can
use to get out of a loop.
One of them is the break statement.
And it's an executable statement.
When it runs, it basically breaks out of the
loop,
moves to the line beyond the end of the loop.
Break statement take out of the loop, the
statement next to the loop
Continue statement take control top of the loop.
So up to now we've been talking about indefinite loops, and that's the use of
the while keyword that just runs until some logical condition is false or you hit
a break.
because we have a set of things that we're going to go through, and it might
be a lot, but we're going to go through all the things in the set.
While was the keyword for indefinite loops, and for is the key word for
definite loops.
So the first thing we see in a for loop is we see the iteration variable is
explicitly just part of the syntax.
The for is the keyword, in is another Python reserved word.
We have various forms of collections and we'll find that files are
collections, lists are collections.
So, that's very definite.
Python
And this takes
is wherecare
friend and friends,
of
you've everything
got for
to be careful us.
because I named it
We don't
which have
makes to have
perfect sense,a logical condition to stop
or anything
but likefor
don't believe that.
a moment that Python understands what
plurals are.
This doesn't have to be a list of numbers.
So, friends is a list of three strings,
Here we have
and friend is an list of strings.
iteration We put
variable that's goingthis
to gointhrough
a it.
variable.
So, I stick this list into friends.
So square brackets, list of strings: Joseph, Glenn, and Sally.
Now we're going to talk about what we're trying to accomplish with a
loop
We're trying to, we know how they mechanically work but what are we
looking for?
And the pattern that we're going to do is we're going to write a for loop.
If we have a loop and in loop we want to keep track of how many we've
seen.
Well, you do this counting thing
So the way it works is you set a variable to 0.
This is the top part, normally this would be called count
Initially we seen 0.
Each time through the loop, we're going to add 1 to zork,
sometimes we just want a variable that tells us whether something was found or
not. (Bolean variable)…..
So boolean is another type of a variable as we have integer variables, string
variables, floating point variables.
And so False is a constant in Python.
found is a mnemonic variable. I use the word found for this all the time,
but don't get stuck on it.
It just happens to be a variable with the value False.
THE END