Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only T&Cs apply

Find out more

Chapter 1.4 – Writing Paragraphs of Code

Writing Paragraphs of Code
So welcome back. Now we’re gonna start learning about the Python language. And you can think of this as talking to Python itself. And it turns out that there is a way on most computers, whether it’s a Windows computer in command line, or a Macintosh, or Linux box, to get Python started. And 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. But this also sort of fits in the what next, 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 wanna do.
I don’t know what to do, I need you to tell me what to do. And so you can type a series of statements in Python. And so the first statement that you might type is an assignment statement, x = 1. And so what is going on here? Now, this assignment statement is something that often confuses people when they move from math to programming. An = 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 gonna 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, it’s kinda redundant, but usually, you’re doing something more complex than this. Put something in memory and then take it back out, that’s the first thing. Now, what this is doing is this is an expression, and that says take whatever is 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 gonna get syntax errors. And Python is just gonna tell you syntax error, syntax error, syntax error, and away you go. But this is us talking to Python. So what do we say to Python? You can almost think of this is like writing an essay. Where you start, and if you think back you started learning an alphabet, and then use that alphabet to produce words, and then use the words to produce sentences, and then you combine sentences to make paragraphs, which then make a story. And we’re gonna do the same kind of thing.
This is the program that we’re ultimately gonna write in, don’t worry about understanding what this story does, we’ll talk about this later. We’re gonna come back to this many times throughout the course. This is a Python story about how to count words in a file in Python. This solves that problem that I asked you to solve a little while back of what the most common word was and how many there are. And we’ll come back to this several times in this course. So 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, because if Python sees assert, it means something very specific to Python. If it sees if, it means something. If it sees for, or pass, or while, where’s while, there’s while, these mean things. The best way to think about this is Python is kinda like a dog. And if you’re talking to a dog and you say, hey dog, do you like Beethoven or Bach better?
The dog is hearing is like blah, blah, blah, blah, blah, it doesn’t care anything. And then you say, hey there Spot, what do you think of the weather today? Do you really like sunsets or do you like rain better? And the dog is like, blah, blah, blah, blah. Then you say something like, do you think it’s time to go for a walk? And the dog goes like, walk, got it. And the dog has been listening to you all along. Most of it is blah, blah, blah, but walk is a reserved work for a dog. Food, treat, those are reserved words for dogs. Most of stuff you can say anything you want for a dog and they’re very good listeners, right?
But when you say a reserved word for a dog and you say walk and you don’t deliver then that dog’s gonna bug you for awhile. So you can think of that as Python. When Python sees global, or from, or for, it’s like, whoa. That’s a word that means a lot to Python, so you better use it the way Python expects you to use it. Sentences are lines, and so 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 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 gonna cause 4 to be printed out. So we’re using operators. This plus is what’s called an operator, the equals 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. Now we start come combining these lines into paragraphs and make some sense.
And so that interactive Python were you talking with that the three chevron prompt, that’s fun and it’s fun to get started and type x equals and x equals x plus one and whatever. But don’t do too much. And I see students all the time, they try to write whole programs in that and it gets a little frustrating cuz you have to type it perfectly from beginning to end. It’s much more common to type something in a file. Once we get maybe beyond three lines of Python we tend to just use a programming text editor, like Atom or something.
And we put them all in file and then we tell Python start at the beginning of this file and then read through the file. We call this a script or a Python program. And in Python, we tend to call that file .py. And some text editors, like the Atom text editor, which is one of those that I recommend, when you have a file that ends in .py, it syntax highlights it and it and gives you colors, it makes things pretty. And it helps you understand and sometimes even leads you towards syntax errors and mistakes that you have. And so scripts are stored sets of instruction in text files that you can then hand to python to run them.
And I have a whole bunch of videos that sorta get you to learning Python, getting Python installed, doing something interactively. And then writing a script and then running from the script. So like I said, you could do interactive in Python or you can run a script. And a script is much more common. After about the first 15 minutes we pretty much do everything with a script. So what do you put in that script? Well it’s a series of steps. Now 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 steps. And we’ll show you in a sec how those look. Then the real power of computers come when we tell it to do something over and over again, a million times if necessary. And that’s when we have a series of steps that need to be repeated. And then the fourth pattern is the store and retrieve pattern, which we will visit in chapter four.
So this is an example of a four line Python program that has four basic steps. As if you had typed these four lines into a file and then told Python to execute them. Python will stick 2 in the variable x then it’ll print that out. Our output will be this, it’ll add 2 to it and then print it out again, and it’ll be 4. This over here is called a flow chart, I don’t really make you write flow charts, in the old days they made us write flow charts. I’m not sure that’s all that valuable. All I’m trying to show you is the sequence of things.
It runs this, does something, then it moves on to the next one, does this, then it moves on to the next one, does this, then moves on to the next one. Meaning that when you’re done with the statement, where are you gonna go? Well, in this case, it’s the simplest of all possible things, where we’re just going from one thing to the next thing. Now, it gets a little more interesting when we do conditional steps, and the thing that makes conditional happen is this if. Right, the if statement is a reserved word, okay?
And so you’ll see it starts at the beginning, and the if has embedded in it a question that leads to a true or a false, ends in a colon, and then there’s an indented block. And what happens is this is the conditional statement, print(‘Smaller’) is the conditional statement. Meaning that If x is less than 10 evaluates to true, then it’s gonna execute, otherwise it’s gonna be skipped. So it can either execute it and continue, or it can skip right over. And so it’s probably easiest to show this on this diagram, where you start with x = 5, then you hit the if statement, and the if statement is asking a question.
If the question is true, you go down one road. And if it’s false, you’re go down the other road. In this case, x is 5. So this is true and so we go down this path and print out smaller and then we rejoin. And the next thing we encounter is another if statement, is x greater than 20? Well in that case, it’s no because it’s not, because x is 5, so we’re asking the question. We’re not changing x, we’re just asking a question about x, so we skip right over this. So the way this code ultimately runs is it comes in, runs this, sequentially comes to here.
Conditionally does run this line of code and then skips completely over it, so this code never ran. And that’s a super simple example, so it’s that the output is Smaller and then Finis. And it’s a super simple one and what you’ll find is we compose these things to make more complex programs. So conditional is the second of the three patterns we are gonna see. And the most exciting pattern is the repeat. It looks a little more busy cuz we can do more stuff with it. So the basic idea is there are a couple of different looping key words, reserved words, the while and the for.
The while loop basically says, it functions kind of like an if statement in that there’s a question in it. Doesn’t hurt n and doesn’t get changed by this, it just looks at n and answer the question is n greater than zero? If it is true, it runs this code, so just like an if. So up to here, it looks like an if. If n is true, n greater than 0 is true, it runs this code. Now this is a little sequential bit. When you are done with this, what do you do? We just follow on to the next one.
And you’ll notice that this is indented at the same level as the print statement, and that’s how we have repeating or even ifs with more than one statement in it. And then, when it de-indents, that’s the end of the loop. And what happens is it runs this and runs that, but then, what does it do? Well, it’s at the end of the loop, so it actually goes back up at the top of the loop. So it goes back up to the while statement, and we actually printed n out which is 5, then n became 4. And then it reevaluates this question, is 4 greater than 0?
Yes it is, so it prints 4 out and then it subtracts 1 and it becomes 3, now is 3 greater than 0? Yes it is, yes it is, and so it goes 4, 3, 2, 1. Then it comes through and it prints 1, and then it subtracts 1 from it so it becomes 0. Now it comes up and asks this question, is 0 greater than 0? Because n has become 0 by the successive iterations through this loop. So n is not greater than 0, so this switches from a true to a false. And false it leaves, goes out, and exits the loop. So over here, it goes 5, 4, 3, 2, 1, [SOUND] and then out she comes, okay?
And as soon as this becomes false, then this loop exits. And so that’s the way we tell a program, we tell the computer that we want it to keep going until something has happened, we’ve achieved something. Who knows what it is we’re doing. None of these programs make any sense particularly, we’re just sort of learning baby steps, okay? So loops have this notion of iteration variables to make sure that they’re not infinite loops. And this variable n is carefully constructed to start and be checked and then be changed each time through the loop. And that’s how we make sure this loop only runs five times and not forever.
Because you can construct infinite loops that run forever, but its not too practical. You just run out of battery or whatever after a while. You generally construct loops to finish, so that they have to check to make sure that they’re finished, okay? So if we take a look back at that story, we see that in that story that is how you count the most common word in a file and print what the word is and how many there are, you find that there’s some sequential code. And in Python the way you can tell that it’s sequential code is when it’s not being indented, it just goes down, down, down, down, down.
Now the for is another of the reserved words that a loop. So this has a colon and it says that’s an indented block and there turns out to be a for within a for. This is called nesting, we’ll get to that. And then this loop ends and it runs for a while. And then there is more sequential stuff. There is a for, which is repeated code. And then there’s an if nested within that for, that’s conditional code. This will run for a while and then it comes out and does some sequential code. So there is in this sequential code, repeated code and conditional code. So the three of the four basic patterns of programming we see in this file.
And so if you look at this from an outlying perspective, this is sort of our story. These are the paragraphs of our story. This top bit here says, read the name of a file from the user and open that file so we can read it. This second paragraph says, as we read through the file, create a histogram that maps the number of words to the frequency of the words. And each time you see a word update the histogram so that we have a running total of all of the words, using a thing that we’ll learn later, like in chapter ten or nine, called dictionaries. And that’s cool, that’s cool. But don’t worry about it, don’t worry about it.
Don’t worry about it now. So this is making a histogram. Then once we have the histogram and we’ve read all the words, then we read through the histogram to find the largest. And then we print out the largest word and the largest count. And so this is a little short story with some sentences and paragraphs, and alphabet, and reserved words, and vocabulary, and all that stuff. And we’re kind of learning how to do it. Don’t try to get this all right now. We’re gonna touch on this. We’ve got a whole chapter on variables and a whole chapter on conditional, a whole chapter on loops, right?
I just am trying to give you this big picture, so that when we get there, you can start putting those things together. And like I said, it’s a little confusing. Generally, it really starts to clear up in chapter six and chapter seven. So just understand that we learn little bits and little pieces and then it gets better towards the end. So thanks for listening. Chapter one was really tried to get you an overview of the kind of stuff that we’re gonna learn throughout the course. And lay down a little bit of vocabulary, so I can talk about things like microprocessors, and RAM, and memory, and stuff like that.
So I can communicate, say hey, we’re gonna do this thing that’s gonna do something with the CPU, so you can use that. So I hope you find this of value and see you in the next chapter.
This article is from the free online

Programming for Everybody (Getting Started with Python)

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now