Before going through the if else short-hand statement, we must be familiar with the basic definition, so we know what the short-hand statement actually is. In basic English, shorthand can be said as “a basic way of writing using abbreviations or symbols”. For example, we do not use the full name, Kentucky Fried Chicken, instead we use its abbreviation that is KFC. Writing in such a compact manner is known as shorthand. Now let us move towards the definition of shorthand with respect to the Python language.
“An executable statement, written in so compact manner that it comprises of only a single line of code is known as shorthand statement.”
Python supports many sorts of shorthand statements and with the help of these statements we can write our code in a more compact sort or form using less space. We learned the original form of “if and else” statement in tutorial #13 of our playlist, we can also write it in a shorthand form of code.
If we write the statement:
If-expression if(Condition) else else-expression
It will be same as:
Now let us come to the question that why should we use shorthand if else statement instead of the original one. First of all, you have to do less typing while using the shorthand form. If you are making a project that requires thousands of lines of code then, it is very hectic if you are write something in four or five lines that could easily be written in a single line. This advantage would not be too much convincing for a lot of viewers now and they would not like to adopt the shorthand method instead of the one they are using currently. In this case this tutorial is still very important for them because if you are a coder then reading other people’s code is a must. Even though you are clearing your concepts by going through some code on Stack Overflow or making a group project with other programmers, you will surely encounter shorthand code at any time, so it is best to have a prior knowledge about it.
The second advantage of using the shorthand way of programming is that the compiler handles it in an efficient way. It does not matter for short program even though if they are couple of thousand lines long but when working on a huge project, like Amazon (12 Million lines of code) or Facebook (62 Million lines of code) or Google (2 Billion lines of code), then it is very important to save as much lines of code as we can by writing it in a compact form.
There is also a disadvantage of using shorthand, that it is not easily readable. When we are working on a group project, we always try to choose the approach that is the most easy to understand, and writing a compact code always bring difficulties when we share our code with someone. Even though which ever approach we follow, we should always have knowledge about all possible approaches, because everyone is not using the same approach.
# list1 = [ ["Harry", 1], ["Larry", 2], # ["Carry", 6], ["Marie", 250]] # dict1 = dict(list1) # # for item in dict1: # print(item) # for item, lollypop in dict1.items(): # print(item, "and lolly is ", lollypop) items = [int, float, "HaERRY", 5,3, 3, 22, 21, 64, 23, 233, 23, 6] for item in items: if str(item).isnumeric() and item>=6: print(item)
No downloadable resources for this video. If you think you need anything, please post it in the QnA!
Any Course related announcements will be posted here