Placeholder Image

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

  • 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 printHooray!”

  • 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 outHooray!”

  • 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 haveHooray!” 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 displayCase 1” if x is greater than 3, andCase 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 sayingIf x is smaller than or equal to 3”, we could

  • write directlyelseand insert a colon.

  • Elsewill tell the computer to execute the successive command in all other cases.

  • In our little program, that would meanin all cases when x is not greater than 3”.

  • That translates intowhen 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 commandprintcase 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 theelsekeyword, 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 relevantprintcommand, 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, youll have a whole lot of blocks.

  • And larger programs are constructed on a block by block basis.

  • In this lesson, well learn an elegant way of adding a secondifstatement 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 outLess”.

  • And the else-statement follows as a tail with the respective block that saysreturnEqual”.

  • Let’s confirm we wrote the code correctly.

  • We can print out thecompare to fivefunction with a value of y equal to 10 in

  • the following way... then well expect to see a statement that saysGreaterbecause

  • 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 obtainedEqual”, 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 stringNegativeshould 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 showsnegative”, just as it should.

  • Let me just control whether our little program will run properly if I asked it tocompare

  • to five” a value that lies in the range between 0 and 5, say 3.

  • Yes, we seeLess”, 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

  • thinksstep 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 stringNegative”.

  • 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 printcompare to fiveof minus 3.

  • Ha!

  • Instead ofNegativewe obtainedLess”.

  • This is how the computer reasons: assume y equals -3.

  • Print outGreaterif 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 outLess

  • 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 theLesslabel.

  • 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 outCorrect”.

  • In all other cases, printIncorrect”.

  • 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, “TrueorFalse”, it will produce one of the

  • suggested outputs, “CorrectorIncorrect”.

  • If the first statement is True, that is, if x is greater than 4, the machine will print

  • the corresponding statementCorrect”.

  • Else, which means if the statement “x greater than 4” is untrue, or more preciselyFalse”,

  • the statementIncorrectwill be printed.

  • From a certain perspective, everything in a computer system is Boolean, comprising sequences

  • of 0s and 1s, “FalseandTrue”.

  • 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, well 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 functionsimplethat can print outMy 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 beplus tenwith a parameter ”a”, that gives us the sum of “a”

  • and 10 as a result

  • Always begin with thedefkeyword.

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

  • Typereturn “a” plus 10”.

  • This will be the body of this function.

  • Now, let’s callplus tenwith an argument 2 specified in parentheses.

  • Amazing!

  • It works.

  • Once weve created a function, we can run it repeatedly, changing its argument.

  • I could runplus tenwith 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 theplus tenfunction, “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 saycall plus ten with an argument of 2, call plus ten with an argument of 5”.

  • People often confuse print and