File "<ipython-input-28-672ad970f03b>", line 1
1variable = 2
^
SyntaxError: invalid syntax
The if conditional
An if is a keyword used to define a block of code that can be executed or not if the statement after the if is a true boolean
so we can use operators to get facts about our variables like the equalty operator
Sometimes, we would like check the trueness of various statements. for that we can use the operator and
or maybe if one of the statements is true
That's the basis of the if construction but there is 2 adjacent keywords that can be used for the next purpouses:
if we want run code if a if block code isn't run, then we can use else
note that as we're using two different blocks of code, the else token has the left space remmoved as represents the beginning of another block of code
sometimes, we need execute only one block of a serie of conditional blocks, that why we have else if, in python called elif
Note that it could have run the block, less than 50 but didn't because, it found first the elif block of a > 1 and a < 10
, then executed it and due the other blocks being elif, ignored the rest
While construction
Programs keep being useless if they can't repeat a block of code as many times we want because, programs exist for do the work that people doesn't want to do
a while is a keyword that defines a block executed as many times as the expression after while is True
As happened with if we can apply all features of a statement into that sentence after while
Probably you guessed that if == is equalty, then != is the inequalty or not equal. In this example, we find what number divided by 2 results into 20 and the answer is 50 as you can see. Notice that normally facts in a while are the opposite of what normally we will say "a / 2 == 20" because we need that result of that expression being True for make while to run the block code again
Loops have 2 keywords that can be used inside their block, we will show it implementing a search on a list
ok, we have the item that we searched in the list but what if we want stop it if we find 30?
for that we didn't have to introduce any new keyword Kappa
But sometimes, conditions can be really long or even uncalculable until we do some operations. Let's find a number that contains a 3
We see new things in these examples like the operator not that reverses the result of a boolean statement (What is True becomes False and opposite). The other thing that we see is that a string is indexable, you can access to a letter of it using [] and the position.
oh, wait that problem didn't use any new keyword... but how the fuck can we do the same without grouping a piece of code in a function?
Yep, we're looking what seems a nested loop while. In these ocassion, exit the loop when we want using the condition of the first while can be tricky. As you can see, we had to put the increment of i at the beginning because we don't want increment when we detect the number with 3 and due that, we had to start with i being -1
What about i < 0? Well, you can guess that if we ask the member -1 of a list will complain, so we need to protect that incorrect access making loop before that sentence with the a[i] is evaluated. That's why i < 0 is there.
There is two quirks about the evaluation of conditionals. First is that languages will try anything to save run code. If the first condition in that sentence is True, then doesn't matter what says the other part of or and will not evaluate it (Making not evaluating that a[-1]). The second thing is that we group sentences in () to dictate the order of evaluation, first we want evaluate i < 0
and after that, what is inside ()
break to the rescue
Last example was not an easy example of how make nested loop that need exit in some point that isn't evaluable in the conditional. Fortunately, we have the keyword break which simply breaks the loop
That's way better :p
The other keyword is continue and what it does is end the code block of the loop and make another iteration (Basically, it's like it jumped to the end of the loop code)
Exercise
Find all numbers up to 1000 that the division between the number and 10, contains 0 and write it on a file
For that, you should have enough with all the code posted in this notebook and you only need to know that the string "\n" is used to indicate when there is a new line