Introduction
If you’re
new here, this is the second part of an article where I show the basics of
programming languages with Python, one of the most famous and beginner-friendly
one. You can find the first part of the article here.
Presented
problem:
Solution:
Python outputs 23 as the solution.
Let’s say
you want to output the input value only if
it’s greater than 10, otherwise (else) just output 0.
By doing the same thing in Python:
Things to
notice:
-
to
define the code that goes inside if
and else we use a colon, go to a new
line and add some indentation: this tells Python that code is not something
after the if/else, but part of it: you can indent using tabs or spaces, your
choice!
-
“else”
specifies the code executed only when all the conditions encountered before (in
this case just an “if”) aren’t met
- I defined the y variable only inside the if statement and this is ok for this example for the sake of simplification (and for showcasing the “else” condition), but on real life this should never happen, because by missing (for some reason) the definition of the y variable somewhere it would have led to crash the program when executing the last line “print(y)”, where Python wouldn’t have known what to print: a “safer” implementation would’ve been be the following…
For now we’ve only seen the example with two conditions, but how can you add more? Differently from what one could think, you cannot repeat the word “if” because that’s only for the first condition, and you cannot repeat the word “else”, because that’s only for the last, where all the previous conditions aren’t met. The solution is using another word, “elif”, which stands for “else if”: it’s called so practically because it’s something which only runs if the first condition isn’t met (else), but also only if it’s condition is met (if).
Anyway, talking about it is much more complicated than what the actual thing is, so here is an explanatory example (please remember I’m not writing “safe” code here):
The code I wrote above does the exact same thing than before, with a difference: when x is greater than 100, it becomes 100 (in simple words, 100 is the max value). A “small” but important thing to notice is that I put “x > 100” before “x > 10”: inverting the two conditions makes “x > 100” unreachable, as “x > 10” has “priority”.
In the
previous paragraph, I said greater than
to indicate the symbol >. This seems
obvious, but some symbols (ex. “greater than or equal to”, represented with ≥)
cannot be used in programming languages, as they’re not (simplifying)
standard/plain characters.
-
greater than: >
-
less than: <
-
greater
than or equal to: >=
-
less
than or equal to: <=
-
equal
to: ==
(notice there is a double equal, as the single equal is used for assignment *)
-
not equal to: !=
* a = 1 (a is equal to 1)
(assigning 1 to a)
is different from
saying
a == 1 (is it a equal to 1?) (comparing
a with 1)
Get back at
work here.
This time I left the solution below because the school ending is “approaching” and I’m not sure there’ll be another part anytime soon.
Hey! I’m
watching you… cheater! ;-) Don’t scroll
down before trying to find a solution to the problem above!
Here’s the solution to the problem:
Hi, I'm
Giorgio Bellisario, a Liceo Scientifico student. My passion revolves around
tech and everything computer-related. Coding is my favorite hobby, and I
primarily focus on web development, crafting websites from scratch. If you're
intrigued by all things tech and ethical "hacking", you've come to
the right place.
Comments
Post a Comment