Multi-Line Statements in Python Language
Statements in this language typically end with the start of new line. However Python allows the use of line continuation character with a back slash (\) to denote that a line must be continued.
In most of the programming languages that we use, there are various ways to control the flow of program. Such control of statements in a program allows certain parts of code to execute if particular condition goes right. This flow control process is done by using some statements and blocks of code. The most common and majorly used statement is if statement. This will compute if a statement is true or false.
If a statement is true, only then it will execute the code. If it is false, it would not execute the code as simple as that. But there are True and False which are Boolean in Python language. It means that if and its other conditional statement will work upon and use Boolean math to calculate their state of Boolean. One should also keep in mind the need for a colon (:) at the end of the if statement. This is required at the end of a flow control statement.
You can additionally combine many conditions along by using the previously instructed Boolean operators; not, and, or. Though you’ll nest statements among one another, it will become exhausting to manage. it might be a lot of Pythonic to stay statements flat. Which implies rather than nesting four if statements along to try to one thing, it might be higher to use one if statement with many and operators. Imagine that one just have to be compelled to compare six numbers in associate if statement. Such an associated if statement might become long and you do not wish to create it worse by nesting the separate components in six separate if statements.
A backslash (\) will facilitate solve this downside. A backslash permits you to interrupt up one long piece of code into many components. Just like the punctuation, you do not have to be compelled to worry concerning indentation following the ‘\\\n’. Since any character once the backslash can cause mis-calculation. This even suggests that excess whitespaces can cause miscalculation. One will need to be additional careful once operating with backslashes.
A more appropriate solution is the use of parentheses (()) to enclose whole code. Use of parentheses in a program works like the backslash, but it allows only for extra characters at the end of any incomplete logical line, which also includes whitespaces.
Many times when one wants to execute code in the event that the “if” statement is not true. Then there comes the use of the else statement. This statement will execute only when the if statement is false. It should be on the same indentation level as similar to if statement.
Though the else and if needs to be aligned with each other, the indentation of their code blocks can be dissimilar. Whereas there is no such requirement like that, it is recommended that you indent every four spaces. The else statement is basically used to make code more readable.
Else if Statement
Same as the problem faced before, we might need to do something that totally seems tedious. For example you want four if statements together, but you only want one to get executed. You will obviously try to write those for statements, but it would be easier writing a big one by using the elif statement. elif stands for “else if”, which works as an else and if. It means that if, the if is not true initially, then it will go to the elif and see if it is true or not.
If statement is not very good at being recursive. This means that the “if” statement cannot loop over and over again wherever necessary. This is where we require the while statement in Python also same as other programming languages. While statement will execute its process code block over and over again until its conditions are false. It can be useful if something needs to be repeated over and over for a particular amount of time.
The Try and Except Statements
This statement is also used in combination same as else if statement, if line of code within the try statement causes an error, then the execution of the code will stop and jump to the except statement. For any reason, if there is any error within the except statement, you will get the message during the handling of the exception. You should be careful not to put any code that could cause an error within the except statement.
Errors and Exceptions
With the name “Errors” & “Exceptions” we can make an idea about that computers can crash, monitors can short, and hard drives can also corrupt. In the moment that something goes wrong, purposely or not, the computer needs to understand that something is going wrong and we need to stop and fix the problem.
Software problems must be handled by computer software. Errors only happen when something do not go right. We can take example of pulling a CD out of a computer while burning it, then it will result in the CD not being completely finished or it may get corrupted. If the program is unable to handle this problem then it may be displayed and give undefined error, like trying to continue burning again in unavailability of a CD or the program could just hang.
So it is important to take care of these kind of errors. Luckily, Python language is able to handle errors and exceptions easily, unlike of other programming languages.
Python error handling as comparison with others: As we have seen so far that errors are very loud in Python language. If something does not follow the rules or doesn’t go right, Python will make sure to highlight that error. Having a very little mistake captured without any mercy might sound bad at first, but it actually becomes important to make program and computing error free.
Old programming languages like C, did not come with built-in error handling. Sometimes errors may went unnoticed. Other times they can cause the computer to crash. It was up to the programmer how one can handle error during programming in those languages. Even then it was very hard to catch errors in their tracks. Python has ultimate error handling goal which is to let user know that an error has occurred.
In python we have a bigger picture of errors, it becomes clear that handling errors is very important. Imagine that you are creating a large program. And program crashes when you use it. In such a case it becomes really important on finding out and fixing that problem.
Python let you know where in that program the error has occurred and what has caused the error. This makes the error fixing process very simpler and it allows rapid development with the knowledge that errors never go unnoticed. This is one of the many situations where error handling comes in importantly in this language. Python has near about twenty different built-in exceptions so that each problem can be addressed.
Python is very powerful in error detection capabilities. If it is possible to produce an error then Python is ready to detect it and show it to the user.