Conditional Statements

Conditional Statements are those statements which give an output when a certain condition is achieved. The syntax is:

For example, I want to check if any number is even or odd. As per mathematics, if any number is divisible by 2 and remainder is zero, then that number is even, otherwise it is an odd number. Here, the condition will be, divide by 2 and get zero remainder. This will be the desired condition or True condition. The other condition will be, divide by 2 and do not get zero remainder. This will be the undesired condition or false condition. Conditional statements only give either of these two outputs. In English how can we say this? If the number is divisible by 2 and the remainder is zero, the number is even, else, it is odd. These two English words If- else are called conditional statements in Python. So, let’s write this even- odd discussion as our first program and learn some programming basics.

# WAP to find if a number is an even number or an odd number?

As discussed earlier, here we are taking a number a. The number should be an integer, so use int (). We can hardcode it by writing a = 10. The other way to write is by asking the user to input a value. This is done by writing input (). So the first line of code is prompting the user to enter a number which will be an integer. We have to check if this number is even or odd. In the next line, we are checking that if this number is divisible by 2 and the remainder is zero then, print that this number is an even number. If it’s not an even number, then, jump to the else condition and print that the number is an odd number. If you look at the output,  the number entered is 3. 3%2 will give the remainder 1, so, else condition will be executed and hence the output printed is written that 3 is an odd number. Let’s check for some other number.

Here, the number is 10. 10%2 will give the remainder 0. Hence, the output executed is in if condition. Hers, we also have to learn what should be written inside the print () command. If you want to display a message write the message in quotes. The quoted message will not change. This practice is not a hard rule; it only makes your display more meaningful to read.  We have tried the same thing earlier after arithmetic operators. Let’s try some more examples.

# WAP to find which number is greater in between two numbers?

Here, we need at least two numbers. Then only we can compare and say that OK one number is greater than another number. Let there be two numbers: a and b.  We need a comparison operator to compare. Here we need a greater than (>) or less than (<) operator. Then we need to check if a > b, then print a else, print b. Let’s code it down.

# WAP to find if a number is a positive number or a negative number?

I think we can now try this out. If a number is greater than 0, then, it’s positive, else, it’s negative.

Let’s try writing this using the Walrus Operator.

# WAP to find if a number is an even number or an odd number using a walrus operator?

First I am writing it without a walrus operator. I have already written it above as our first program, but let’s write in another way.

What I have done is, I have taken a number and then I am dividing the number by 2 and I want to check the remainder, so I am using modulus num%2. Then I am assigning the result of num%2 to another variable called rem. I am equating the result to zero. If it’s zero, then it’s an even number, else, it is an odd number. Now let’s write it down using the walrus operator.

The output will be

Now, we have two programs to find an even or an odd number without using the walrus operator and one program using the walrus operator.

Can we use the walrus operator as a replacement for our first program? If we see here, we have two lines having the same statements written: num%2 == 0 and rem = num%2. So, what are we doing? We are writing rem = num%2 and then returning it back to rem by writing rem := num%2 and then checking if this entire operation is 0 or not. That’s why I said in the previous post that what you can do with the walrus operator, you can also do without it. Use walrus operators when there are two statements having the same things written. Don’t stress yourself if you are not getting this at this moment. In later topics we’ll use more of this operator.

Moving on, we can have as many if- else conditions as we wish. We also can have if within if within if and so on. This condition is called Nested If- Else condition. There is also another condition called If- Elif- Else. It works like if one condition executes print the result, else if another condition executes and prints this result, else the third condition executes and prints the result. The syntax is:

Let’s try one more program to see the use of many if conditions and if-elif-else conditions. 

# WAP to find if a number is positive or negative using nested if else condition?

If a number is zero then it’s positive and if it’s greater than zero, then also its positive. These two conditions are true when the number is greater than or equal to zero. We can assign these as nested conditions.  If num is greater than and equal to zero, and If it’s equal to zero, then print that zero is a positive number, else, it’s greater than zero and print that number is a positive number. Else it’s a negative number.

# WAP to find if a number is positive or negative using if-elif-else condition?

Now it’s time to have some practice problems to see how operators and if-else conditional statements work.

# WAP to find if a year is a leap year or not?

We are now ready to use if and else conditions. Only thing is the condition. What should be the condition for a year to be a leap year? If the year is divisible by 4 and 400 but not by 100 then that year is a leap year. Let’s code it down.

# WAP to convert Celsius temperature into Fahrenheit temperature?

As per formula, Fahrenheit = (Celsius * 9/5) + 32. 

# WAP to convert Fahrenheit temperature into Celsius temperature?

As per formula, Celsius = (Fahrenheit – 32) * 5/9 

# WAP to find the square root of a number?

To obtain the square root of a number we have to raise the number to the power 0.5. This is achieved using the exponential operator.

# WAP to swap the contents of two variables?

By swapping we mean interchanging. If a = 5 and b = 6, then after swapping their values will become, a = 6 and b = 5. First let’s assign these two values taking two variables.

Now, python provides an interesting one line feature to assign values. If on LHS there are n- variables separated by commas and there are n- values on RHS separated by commas, then, each value will be assigned to each variable respectively position wise. Let’s check this thing.

Let’s check for more than two values.

5 is assigned to a, 6 is assigned to b, 7 is assigned to c and 8 is assigned to d. We will use this special Multiple Assignment in Single Line feature of python to swap our two variable’s values. If we write a, b = b, a, then, right hand side variable b will be assigned to left hand side variable a, and, right hand side variable a will be assigned to left hand side variable b. Let’s code it down.

Now, change the values of a and b and see. As per our code, if a is smaller than b, the values should not be interchanged.

It’s working great. Technically, this Multiple Assignment in Single Line feature is called Tuple Packing and Unpacking. We will learn about this in detail later in the section of Tuples. Before concluding, we have one last thing to consider. If we observe closely in all codes, a specific pattern has been followed. After if statement or else statement, the next statement is written after some space. Can we write it just below it? Let’s see what happens?

An error occurred. This is the most important error to discuss before moving on. Python does not have semicolons like other languages for statement termination.  Also, there are no curly braces to start and end any block. Therefore, how’ll python know if something is written within a loop or not. Or if some statement is depending on some condition or not. That’s why alignment of codes is necessary in python. No matter if your code is absolutely correct, if it’s not properly aligned, you’ll get an error. This error is called Indentation Error. So, remember the rule: Whenever the loop, function, class and conditional statement starts, write a colon first. It means a loop has started. The next line will be inside these loops. It will be 4 spaces or 1 tab key inside. So whenever a loop starts, place your next line 4 spaces or 1 tab key inside this loop. If there is another loop inside a loop, then place next line 8 spaces or 2 tabs and so on, depending on the condition. It seems complex, but don’t worry, with practice you’ll become perfect in writing indented codes.

That’s all for week 02. In the next section we’ll start Loops. Keep practicing.

Design a site like this with WordPress.com
Get started