0% found this document useful (0 votes)
6 views

Lab Lectures

Uploaded by

Fai Sal Khanzada
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lab Lectures

Uploaded by

Fai Sal Khanzada
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 101

Introduction to PYTHON

 As of June 2017, the course is taught using


Python 3. The Python community has been
moving from Python 2 to Python 3 for a long
time

 The differences between the two versions of


Python are pretty minimal.
 The print statement has become the print
function. What this means is that this line in
Python 2:
◦ print "hello world” is as follows in Python 3:
◦ print("hello world")

The raw_input function has also changed to input, so


instead of:
◦ raw_input('What is your name?')
◦ input('What is your name?')
 You can write program on:
◦ https://www.programming-hero.com/code-
playground/python/index.html

 You can install Python


◦ https://www.python.org/downloads/

 Text Editor
◦ https://atom.io/
 if you just run Python, it will take, interactively,
 commands that you can type.

 And you have to type "python" at this chevron


prompt, is what we call it,
 if you just run Python, it will take, interactively,
commands that you can type.

 And you have to type "python" at this chevron prompt, is


what we call it,

So Python is like "Okay. I'm here.


• I can handle any Python statement you can send me.
• I'm ready to do whatever it is that you want to do.
• I don't know what to do.
• I need you to tell me what to do."
you can type a series of statements in Python.
The first statement that you might type is an assignment statement; x equals 1.
And so what is going on here?

This assignment statement is something that often confuses people when they
move from math to programming.

An equal sign sort of has a direction to it; it's an arrow.


 It really is saying "Dear Python,
 you've got a lot of memory.
 Take a little tiny piece of that memory and remember it and name it
x.
 I might use that - I'm going to use that later - and stick a 1 in it."
 So this is sort of like stick 1 in a spare place in memory and name it
x.
 print (x) says, go take whatever that spare bit of memory was and
bring it back out and tell me what I put in it.
 Now what this is doing is this is an expression and that says take
 whatever's in x, which is a 1, and then add 1 to it,
 which becomes 2, and then stick it back in x.
 So that adds 1 to x and then we print that out and it's a 2 and then
we quit.
 Now if you type wrong things here, you're going to get syntax
errors.
 So what do we say to Python?
 You can almost think of this as like writing an
essay where you start - and if you think
back, you started learning an alphabet and then
you used that alphabet to produce words and
then used the words to produce sentences
and then you combined sentences to make
paragraphs,
 which then make a story.

 And we're going to do the same kind of thing.


 if we start at vocabulary,

 the first thing we have in every programming language is what's


called reserved words.

 Now, what do we mean by reserved words?

 Well, these are words that if we use these words, we must use
them to mean the thing that Python expects them to mean.

 Another way to put that is we can't use them elsewhere.

 We can't make up a variable named import.

 We can't make a variable named assert;


 when we write a program, we're writing a text file and we put a line and
another line and another line. Each one of these is like a separate line and
we've got to get them right.

 And then we construct a paragraph out of a series of lines. And so in this


particular example, we have another assignment statement that's sticking the
number 2 into the variable x, retrieving that 2 back in,
 adding 2 to it and sticking that sum back into x.

 Print is a function and there is a parameter x and that's going to cause 4 to be


printed out. So we're using operators, this plus is what's called an operator,
 the equal sign is what's called an operator.

 This print is actually a function.

 This is a parameter being passed into the function.

 So there's a whole series of different kind of syntaxes that we use to produce


the lines that we have to produce.
 There are a couple of basic patterns that we use and we compose
them.
 The most basic pattern is what's called sequential.
 We do one thing, then we do the next thing, then we do the next
thing.

 Conditional is sort of intelligent where you're doing something and
then may or may not be doing something.

 Do, do, do, something,

 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.

 Those are called conditional


 The real power of computers come when we
tell it to do something over and over and over
again a million times, if necessary.
 And that's when we have a series of steps
that need to be repeated.
 A big part of any programming language is the syntax for constants.
 The nice thing about this it's kind of instinctive.
 So, like a 123 or 98.6, numbers both integer and floating point
numbers.
 We've been using this on calculators.
 The kind of constants that are a little bit different are things like
string constants.
 So "Hello world" is a string constant.
 We use that so that our program can say nice things to people that
are using our program.
 And so, we call them constants because they don't change

 Variables and reserved words.


 So the other thing other than constants are reserved words.
 these are words that when you say them to Python,
 Python expects them to mean exactly what Python wants you to mean
 variable names are places where you're
asking Python to allocate a bit of memory and
stick something in it and you're choosing the
label.

 When we have this assignment


statement, and remember that assignment
statements always have a direction, right?

 You can start variable names with letters or
underscores, although we avoid
 underscores because Python tends to use
underscores for its own internal purposes.
 rest of it can be letters, numbers, and
underscores.
 it's case sensitive.
 So, spam all lowercase, Spam with one
uppercase, and SPAM these all are different
variable names
 We have a technique called mnemonic.

 The idea is that when you choose a variable


name, you should choose a variable name to be
sensible and Python doesn't care whether you
choose mnemonic variable names or not.

 And the name that you choose for a variable does


not communicate any additional information to
Python, okay?


 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

 We talked about operators, operator precedence,


focused a little bit on division where we talked
about Python 2 versus Python 3.

 We talked about type conversion and comments


and then writing an entire program.
 Indentation is important in Python.

 It's more important in Python than literally almost any other programming
language.
 We always use indentation in other programming languages to mentally keep track
of blocks of text, but it's not where the programming language will complain if
you get it wrong.

 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.

 The except is kind of like an else, an if-then-else, but what it really is


code that Python will execute if something goes wrong.

 So this is either going to run and work and skip this.

 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.

 Now, we'll run this code again.


 Okay, we run it a second time and now we enter something, it says forty-two but it's like f-o-r-
t-y. And so forty-two is what goesin here.

 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:

◦ comparison operators, logical questions.


◦ indentation and how important indentation is.
◦ One-way decisions with if,
◦ two-way decisions with if-then-else.
◦ Nested decisions where you have an if inside of an
if that moves on in.
◦ Else-if, and then try and except to catch errors that
you want to catch, okay?
 The new keyword that we've got is def, d-e-f,
which stands for start the definition of a
function, define the function.

 def is the keyword, it ends in a colon,

 just like lots of things that start an indented


block that starts an indented block.
 The naming conventions for function names is the
same as for variables
 It's also another one of Python's built-in functions and this is an
example of an argument.

 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.

 Definite loops are finite.

 Definite loops are for lists or lines in a file or characters in a string,


and, you know, they iterate through members of a set.

 And for this, we use the "for" construct.

 And it's more predictable;

 and it's easier even to validate them

 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?

 What if we're looking for the largest value or checking to see if 42 is a


member of a set or something?

 Or looking for the largest letter, like the max function?

 And so we're going to construct loops sort of with an idea of doing


something to each value in the set that we're iterating through.

 And then coming up with some kind of result.

 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

You might also like