In the last session, you understood about dictionaries; now, try to answer this simple question on dictionaries.
Dict = {1:'Asia',2:'Africa'}
From this dictionary, return the value when the key is 3, and if the key is missing, return ‘NA’. You cannot use Dict[3] because it throws an error, as 3 is not present in the dictionary. Instead, you can use the dictionary get method: Dict.get(3,'NA').
So, what exactly is happening using this method that is helping us avoid an error?
This method simply establishes a condition that specifies: if a key is absent, then print NA. This is nothing but an if-else construct. Before we look at the if-else construct, let’s quickly understand binary and relational operators.
Having understood about relational operators, let’s now look at the if-else construct and the role of these relational operations in its implementation.
In the example discussed above:
x = 10 if x%2 == 0: print(x, "is even number") else: print(x, "is odd number")
x % 2 == 0 is a relational condition that would return 'true' or 'false' based on the value of x. If this condition is true, the set of statements under the if-block will be executed; otherwise, the statements under the else-block will be executed.
Important things to note here are as follows:
The colon that indicates the start of block creation, The indentation here defines the scope of the if- or else- statement, Either code under the if-block is executed or under the else-block is executed, not both of them.
So now that you have understood the basic if-else construct, can you try to write a code to return YES if x lies in the range of 1000 to 1100, else return NO?
If you break this down, you can get the following conditions:
Implementation:
if (X<1000): print('No') else: if (X>1100 ): print('No') else: print('Yes')
You may think that this is not possible using a single if-else statement, but that is not true. You can use the logical operators in Python and combine two conditions. Let’s look at them in the following video:
In the video, you learnt that there are three types of logical operators:
If you apply this concept to our earlier example for checking whether x lies in the range of 1000 to 1100, the code gets modified as shown below:
if (X>1000 & X<1100): print('YES') else: print('No')
Isn’t this simple compared with the earlier code? That is how logical operators make our logic building easy.
Similar to an if-else construct, there is if-elif-else construct available in Python. Let’s understand it by looking at an example in our next video.
In the example shown in the video:
if shoppinng_total >= 5000: print("You won a discount voucher of flat 1000 on next purchase") else: if shoppinng_total >= 2500 : print("You won a discount voucher of flat 500 on next purchase") else: if shoppinng_total >= 1000: print("You won a discount voucher of flat 100 on next purchase") else: print("OOPS!! no discount for you!!!")
Using an if-else construct would also give us an answer, but imagine that you have 10 conditions like this; wouldn’t it be clumsy? Instead, if you use something like an elif-construct, it would make the code more readable.
In this segment, you learnt about relational operators, logical operators, if-else, if-elif- else and nested if-else constructs. You might be pretty confused about when to use each of them. Read the tips given below, which may help you to arrive at a conclusion faster.
TIPS:
When there is more than one condition to check and you need to:
In the next segment, you will learn about the uses of looping constructs such as ‘for’ loops and ‘while’ loops.