Python Programming Language - Conditions Up until this point, the translator has carried out one command at a time. Conditions affect how this changes. IF, ELSE, IF In order for our script to execute the code in its block, a condition essentially needs to return True. If, elif, and else are the three crucial keywords in this sentence. The user enters a number that this script converts to an integer. When this number is less than ten, our first if statement determines if it is. Always keep in mind that comparisons only yield True or False. The code that is indented here is run if the return value is True. Python code blocks are identified with colons and indentations. If this condition returns False, the elif-block is reached and this condition is checked there. Here, the identical process takes place. Elif blocks can be as numerous as you like. If neither criterion is true, we enter the else-block. You can see how these fundamental if, elif, and else trees function in this flowchart. Of course, an elif or else block is not necessary. Simply create an if-statement and the rest of the script will run without the code if the condition is not met. NESTED IF-STATEMENTS
You can also put if-blocks into if-blocks. These are called nested if-statements.i f number % 2 == 0: if number == 0: print("Your number is even but zero") else: print("Your number is even") else: print("Your number is odd") So, here we have the first condition, which checks if the number is even. When it’s even it then checks if it’s a zero or not. That’s a trivial example but you get the concept.
0 Comments
Leave a Reply. |
AuthorAnything you need to know about computer science Archives
May 2023
Categories
All
|