Subtitles section Play video
-
In this lecture, you will see values act as the most basic (or primitive) data elements
-
necessary to form not only variables, but any expressions.
-
A prominent example of conditional statements in Python is the “If” statement.
-
What you can use it for is intuitive, but it is very important to learn the syntax.
-
Think of the following: if 5 equals 15 divided by 3, then print “Hooray!”
-
First, don’t forget we should use the double equality sign here, because we are checking
-
whether 5 is equal to 15 divided by 3, and we are not assigning the value of 15 divided
-
by 3 to be 5.
-
5 is not a variable name; it is a number.
-
Good.
-
Now it is crucial to place a colon.
-
The colon will tell the computer what to do if the condition we just wrote has been satisfied.
-
For achieving good legibility, we advise you to write the print statement on a new line.
-
Please, remember it should be indented; otherwise, you will run into an error.
-
All right, now this should work correctly.
-
Yes, 5 is equal to 15 divided by 3.
-
Hooray!
-
Will it work if we check for 5 being equal
-
to 18 divided by 3?
-
It is not supposed to, since 5 differs from 6.
-
We got nothing, because we have not told the machine what to do, if the provided condition
-
is not satisfied.
-
So, there is no reason for the machine to print out “Hooray!”
-
The graph could help you imagine the process of the conditionals.
-
Before it displays the outcome of the operation, the machine follows these logical steps.
-
If the conditional code is not to be executed because the if-condition is not true, our
-
program will directly lead us to some other output or, as it is in our case, to nothing.
-
After any of the two situations, the machine will go to the next black point and will progress
-
from there on.
-
Let’s try with an inequality sign, which can be written with an exclamation mark and
-
an equals sign.
-
Isn’t 5 different from 3 times 6?
-
Yes, it is.
-
It is True that it is different.
-
Hence, we have “Hooray!” as an output.
-
This was a brief introduction to if-statements.
-
This logic will help you proceed to the next lecture, where we will complicate things a
-
little bit.
-
But not too much
-
Let’s assign the value of 1 to x.
-
We can ask the computer to display “Case 1” if x is greater than 3, and “Case 2”
-
if it is less than or equal to 3.
-
Ok, we could write two if-statements one after the other.
-
Let’s see if we get the correct outcome.
-
Yes, we are in case 2.
-
There is a shorter and better way to express ourselves here.
-
In the second part, instead of saying “If x is smaller than or equal to 3”, we could
-
write directly “else” and insert a colon.
-
“Else” will tell the computer to execute the successive command in all other cases.
-
In our little program, that would mean “in all cases when x is not greater than 3”.
-
That translates into “when x is less than or equal to 3”.
-
Let’s verify if what we did was correct.
-
Bingo!
-
Case number 2!
-
This picture adds up to the one we saw in the previous lesson.
-
Instead of leading to no output, if the condition is false, we will get to an else code.
-
In our case, this is the command “print “case 2”.
-
Regardless whether the initial condition is satisfied, we will get to the end point, so
-
the computer has concluded the entire operation and is ready to execute a new one.
-
Wonderful!
-
Now, please allow me to share two notes on the subject.
-
Don’t fall into the trap of organizing your code on a whim.
-
There is a strict manner to do that, and indentation plays a key role again.
-
Should you put the “else” keyword, just underneath the first print word, nothing will
-
happen.
-
Remember to place the if and the else keywords on the same vertical line!
-
This is the right moment to introduce you to the notion of blocks of code.
-
The if-statement, meaning the condition plus the relevant “print” command, form the
-
first block of code.
-
The entire else-statement forms another block of code on its own.
-
In a long sheet with much code, you’ll have a whole lot of blocks.
-
And larger programs are constructed on a block by block basis.
-
In this lesson, we’ll learn an elegant way of adding a second “if” statement to one
-
of our expressions.
-
This is done with the help of the elif keyword, as shown in this example.
-
If y is not greater than 5, the computer will think: “else if y is less than 5”, written
-
“elif y is less than 5”, then I will print out “Less”.
-
And the else-statement follows as a tail with the respective block that says “return “Equal”.
-
Let’s confirm we wrote the code correctly.
-
We can print out the “compare to five” function with a value of y equal to 10 in
-
the following way... then we’ll expect to see a statement that says “Greater” because
-
10 is greater than 5.
-
Correct?
-
Ok.
-
Perfect.
-
What if we carry out this operation for the number 2?
-
The machine tells us that 2 is less than 5.
-
And that’s what we expected.
-
To obtain the third outcome, we must compare the number 5 with a number that is not greater
-
or smaller than 5.
-
This will happen only if the argument of the function is 5, right?
-
Shall we try this one?
-
Great!
-
We obtained “Equal”, as expected.
-
Know that you can add as many elif- statements as you need.
-
Let’s provide an example.
-
If y is less than 0, the string “Negative” should be displayed.
-
I will place this block between the if and the other elif statement.
-
Let’s see what happens.
-
The function with an argument of minus 3 shows “negative”, just as it should.
-
Let me just control whether our little program will run properly if I asked it to “compare
-
to five” a value that lies in the range between 0 and 5, say 3.
-
Yes, we see “Less”, so everything is ok.
-
A very important detail you should try to remember is the computer always reads your
-
commands from top to bottom.
-
Regardless of the speed at which it works, it executes only one command at a time.
-
Scientifically speaking, the instructions we give to the machine are part of a control
-
flow.
-
This is something like the flow of the logical thought of the computer, the way the computer
-
thinks – step by step, executing the steps in a rigid order.
-
When it works with a conditional statement, the computer’s task will be to execute a
-
specific command once a certain condition has been satisfied.
-
It will read your commands from the if- statement at the top, through the elif-statements in
-
the middle, to the else- statement at the end.
-
The first moment the machine finds a satisfied condition, it will print the respective output
-
and will execute no other part of the code from this conditional.
-
In our example, if the first statement is correct, we will see the corresponding output
-
number 1, which is printing the string "Greater”.
-
The computer will disregard the elif and the else statements, and will proceed with the
-
rest of the code.
-
If the first statement is not correct, we will move forward, and the computer will check
-
whether our second statement is true.
-
If yes, we will see output number 2, which is printing the string “Negative”.
-
If not, we will get to statement number 3 and so on until the computer finds a satisfactory
-
outcome to print out.
-
Now, I will switch the order of the two elif statements to prove that the order of instructions
-
matters.
-
Ok?
-
Let me print “compare to five” of minus 3.
-
Ha!
-
Instead of “Negative” we obtained “Less”.
-
This is how the computer reasons: assume y equals -3.
-
Print out “Greater” if y is greater than 5.
-
Is it greater than 5?
-
No, so the computer continues and checks if there are any other statements in our code.
-
Given we have other statements, it moves forward.
-
So, is y less than 5?
-
Yes, it is.
-
At this moment, the computer thinks, “Lovely, I got it!
-
My number is less than 5, I satisfy what my programmer asked me to do, I print out “Less”
-
and I am fine”.
-
And the machine stops there and does not execute a single letter of the code that follows in
-
this block.
-
The fact that you examined the cases when y is less than 0 or equal precisely to 5 have
-
no application.
-
They become useless.
-
Whether you ask for the output of minus 3 or 3, you will still have to be satisfied
-
with the “Less” label.
-
You found this interesting, didn’t you?
-
Stay focused for the next lecture, when we will share something more about computational
-
logic.
-
You probably noticed we talked about Boolean values a few times.
-
Here, we would like to provide a short video that aims to explain their application.
-
Let x be equal to 2.
-
What you see next is the following if-else construction: – if the value of the x variable
-
is greater than 4, print out “Correct”.
-
In all other cases, print “Incorrect”.
-
So, which is the Boolean element we have in this computational logic?
-
Basically, after you insert your if-statement, the computer will attach a Boolean value to
-
it.
-
Depending on the value of its outcome, “True” or “False”, it will produce one of the
-
suggested outputs, “Correct” or “Incorrect”.
-
If the first statement is True, that is, if x is greater than 4, the machine will print
-
the corresponding statement “Correct”.
-
Else, which means if the statement “x greater than 4” is untrue, or more precisely “False”,
-
the statement “Incorrect” will be printed.
-
From a certain perspective, everything in a computer system is Boolean, comprising sequences
-
of 0s and 1s, “False” and “True”.
-
This is why we are paying attention to the Boolean value.
-
It helps us understand general computational logic and the way conditionals work in Python.
-
Ok.
-
Excellent.
-
Now, you know more about conditionals in Python, and you understand the control flow of if-,
-
elif-, and else- statements.
-
In addition, you saw, once more, complying with the Pythonic syntax is crucial for the
-
execution of your code.
-
Where you type the colon sign and indentation matters.
-
Last, you saw the order in which you declare your commands leads to a specific outcome.
-
If you change the order of your commands, the outcome could change, and this may take
-
you to undesired results.
-
Wonderful!
-
Great!
-
Let’s step it up a notch.
-
Starting from this lesson, we’ll deal with Python’s functions - an invaluable tool
-
for programmers.
-
The best way of learning is by doing, so let’s create a function and see how it can be applied.
-
To tell the computer you are about to create a function, just write def at the beginning
-
of the line.
-
Def is neither a command nor a function.
-
It is a keyword.
-
To indicate this, Jupyter will automatically change its font color to green.
-
Then, you can type the name of the function you will use.
-
For instance, simple, as we will create a very simple function.
-
Then we can add a pair of parentheses.
-
Technically, within these parentheses, you could place the parameters of the function
-
if it requires you to have any.
-
It is no problem to have a function with zero parameters.
-
This is the case with the function we are creating right now.
-
To proceed, don’t miss to put a colon after the name of the function.
-
Since it is inconvenient to continue on the same line when the function becomes longer,
-
it is much better to build the habit of laying the instructions on a new line, with an indent
-
again.
-
Good legibility counts for а good style of coding!
-
All right, let’s see what will happen when we ask the machine to print a sentence.
-
Not much, at least for now.
-
The computer created the function “simple” that can print out “My first function”,
-
but that was all.
-
To apply the function, we must call it.
-
We must ask the function to do its job.
-
So, we will obtain its result once we type its name, “simple”, and parentheses.
-
See?
-
Great!
-
Ok, good.
-
Our next task will be to create a function with a parameter.
-
Let it be “plus ten” with a parameter ”a”, that gives us the sum of “a”
-
and 10 as a result…
-
Always begin with the “def” keyword.
-
Then, type the name of the function, “plus ten”, and in parentheses, designate the
-
parameter “a”.
-
The last thing to write on this line would be the colon sign.
-
Good.
-
What comes next is very important.
-
Don’t forget to return a value from the function.
-
If we look at the function we wrote in the previous lesson, there was no value to return;
-
it printed a certain statement.
-
Things are different here.
-
We will need this function to do a specific calculation for us and not just print something.
-
Type “return “a” plus 10”.
-
This will be the body of this function.
-
Now, let’s call “plus ten” with an argument 2 specified in parentheses.
-
Amazing!
-
It works.
-
Once we’ve created a function, we can run it repeatedly, changing its argument.
-
I could run “plus ten” with an argument of 5, and this time, the answer will be 15.
-
Great!
-
Pay attention to the following.
-
When we define a function, we specify in parentheses a parameter.
-
In the “plus ten” function, “a” is a parameter.
-
Later, when we call this function, it is correct to say we provide an argument, not a parameter.
-
So we can say “call plus ten with an argument of 2, call plus ten with an argument of 5”.
-
People often confuse print and