How to Write Fibonacci Code in Python
if-condition in Python
In Python there is the possibility to check conditions and to react accordingly in the program flow. Here, depending on the time, we could greet the user accordingly. But first of all the general structure of -queries and how will they be used.
Structure of the query
If we were to formulate a condition in German, it would look like this
if value is less than 5 then do everything colon
And now the whole thing as a Python program, in which we first define the variable:
The important thing is that after the query, the other things that belong to the query, is indented! These indentations are the linchpin in Python and guarantee a clean source code. If you mess with the indentations, the program will not work as expected.
In our inquiry, everything below that is indented belongs to the condition. And that can be more than one line!
If we let the program run, we get all 3 lines as the result. The 2 lines of text that are "within" the query (i.e. that are indented) and the last line of text that comes after the query and is not indented.
If we now change our variable at the beginning to, when the program is executed we only get the last line that comes after the query. The query is not true because the value of 9 is already greater than the condition and the indented query is not executed.
When making changes to the program, it is important not to forget to save!
What exactly happens at? Python checks whether the result is true or false. "True" or "false" can also be presented directly to the query and it reacts accordingly:
It appears as a result:
Of course, we could also type and nothing would be displayed since our query is not true.
Of course, nobody would create such a query, because the query would always lead to exactly the same result. However, we can also pass variables that have this value.
Our variable "true or false" has now been assigned the value "true" and the query reacts accordingly.
These variables are of the Boolean type - there are only the values or in the case of Boolean. These variable types are named after the inventor George Boole.
Not the same in Python
Often you just want to know whether a certain condition does not apply, i.e. whether it is unequal.
The operator (not equal) can be used for this.
As an example we want to know from a number of hours whether it is NOT 12 o'clock. For this our code:
Alternative if query does not apply
Now it is often a good idea to be able to react immediately if the query does not apply. Without this knowledge we would have to build such angular constructions as follows:
We want this to be shorter because the second query is just the opposite of the first. Everything that is not smaller than 5 is definitely larger than 4. And this is where the nice word comes in:
We will replace our second part with. If our query does not apply, i.e. if it is not true, but false, then the block is output under:
The output we get when executing the program is:
Value is greater than 4
All comparison operators
Use the appropriate comparison depending on the task at hand!
|<=||Smaller or equal|
|>=||greater than or equal to|
Check further conditions within the condition - elif
Our previous program is not really sexy. There is the output “Value is less than 5” or “Value is greater than 4”. Actually, the following 3 possible results would be much nicer.
- Value is less than 5
- Value is exactly 5
- Value is greater than 5
To do this, we need a query within the query. And Python knows the command for that. In most other programming languages this is known as "elseif" but in Python it is.
Our program from above now looks like this.
The check of the condition behind is only checked if the first condition does not apply. If our condition does not apply either, then the alternative comes into effect.
The indentations are important!
Small nasty source of error - double equal sign
Here is our code in which an error has crept:
We check whether this corresponds exactly to 5. Here the standard error is accidentally just making an equal sign (instead of 2). But this is not possible and thus an error message comes from Python in the form of "SyntaxError: invalid syntax"
any number of elif
We're not limited to one thing. Any number of these further queries can be made. It makes sense depending on the task at hand.
Recommend • Social Bookmarks • Thank you
- Math joke number 38 what an octopus is
- Baruchowo Maps Google Earth
- How to translate the browser language into English
- Noscript Chrome Lifehacker how I work
- How to keep drawn butter warmer
- How to apply reflective tape on trailers
- As they say stubbornly in Tagalog
- Chicken Chow Mein cook with Faiza recipes
- Howard Shaw Mazars Greenview
- Dr. Howard Brown DVM
- Qbittorrent rss feed setup how to
- How to open catia drawing sample
- Casbia men's shoes wholesale
- WhatsApp Marathi Status Community
- Gry leniuchowo graj teraz gotowanie
- How to feed on Skyrim vampire cattle
- How to loose your headphones for television
- What is the sticker for output 32
- Howls moving castle screenwriters
- Dj sbu remember when it rained waptrick
- How to say fake yeezy foamposite
- How to reuse gel hand warmer
- How to restart the firewall configuration
- What is the abbreviation for association