Learn Python with Dhawal -4- Chapter


CHAPTER 4

Now, as you know how to perform calculations using variables and other basic operations in python, lets now study the conditional statements. Here, it'll be a fun task creating your own calculator in python using the conditional statements.

Before we begin, I need to inform you the few things you need to keep in mind while using the jupyter notebook. Each time you create a new program you can either create a new notebook or you can restart the notebook and clear everything which is an option in the tools that you'd find under Kernel tab. Also, there are a few shortcuts that you can use to operate on the UI and many more instructions that you'd find here on the official website.

So once you are well versed with the UI, we can now head onto the main portion which is coding. At times a few things might work a bit different based on different IDEs which you need to take care of while following this guide but for most part if you are following me using a notebook then there's very less chance of you ending up on hurdles while completing this course.

Conditional Statements

The conditional statements are the conditions that we use to check if a particular condition is met or not. Usually the answer by default comes in boolean values ( true or false ) but at times you can also specify it the return value that you want and make it customisable but the main objective stands to do something or return some value depending upon a condition.

Types of Conditional Statements.

  1. IF Statement
  2. IF....Else Statement.
  3. ELIF statement
  4. Nested If...Else Statement
You can use any of the above configurations in any fashion you want once you are very thorough with how to use and get results from them.

IF Statement.

The IF statement is used to check if a particular condition is true or false. It returns a boolean value of either true or false, in binary its either a 1 or 0, in which 1 means true and 0 means a false.

Syntax : 
Statement xyz1
if ( a condition that I want to check)
<tab>Statement 1
<tab>Statement 2
<tab>Statement 3
<tab>Statement 4
statement xyz2


IF...ELSE Statement.
The IF...ELSE statement can be considered as an extension of the IF statement. The IF statement only executed the block of statements when the condition was true, now what if we wanted it to perform something if the condition was false? For that purpose, the IF...ELSE statement is used. So that if a particular condition you wanted to check was false then it'd execute the next set of statements that you'd like to run when the condition was false. This will save you time and resources of check a similar condition twice.

Syntax : 
Statement xyz1
if ( a condition that I want to check):
<tab>Statement 1
<tab>Statement 2
else:
<tab>Statement 3
<tab>Statement 4
statement xyz2


ELIF Statement

The ELIF statement is also another extension of the IF Statement. It is used in a scenario where you want to get a set of conditions checked and execute a particular set of statements without triggering the other sets of statements.

Syntax : 
Statement xyz1
if ( a condition that I want to check)
<tab>Statement 1
elif(a condition that I want to check):
<tab>Statement 2
<tab>Statement 3
<tab>Statement 4
statement xyz2


Nested IF....ELSE
In Nested IF....ELSE the all of the above options can be combined and nested together putting them one inside other to execute a very complex set of conditions.

Syntax : 
Statement xyz1
if ( a condition that I want to check)
<tab>Statement 1
elif ( a condition that I want to check):
<tab>if ( a condition that I want to check):
<tab><tab>Statement 2
<tab>Statement 3
else:
<tab>Statement 4
statement xyz2


Note : <tab> refers to make a one single tab space. Indentation is very important in python. Whatever is inside the same indentation until it breaks is considered as one loop or a group of statements. If your indentation isn't proper than it might result in errors. Once you understand the concept of IF and indentation, others are quite easy and relatable to understand.


Code 1:

Input:
1. a=12
2. b=32
3. print('a is',a,'b is',b)
4. if (a>b):
5.<tab>print ('a is greater than b')
6. if (b>a):
7.<tab>print ('b is greater than a')

Output:
a is 12 b is 32

b is greater than a

To print multiple variables you can use , to concatenate the sentences and variables to have more complex print statements. The above code takes two variables a and b and then we compare a with b to find out which one is greater. There's only one output even after two IF conditions because If condition only executes the statements within its limits only if the condition is true otherwise it does nothing.


Code 2:

Input:
1. a=int(input("Enter a "))
2. b=int(input("Enter b "))
3. print('a is',a,'b is',b)
4. if (a>b):
5. <tab>print ('a is greater than b')
6. else:

7.<tab>print ('b is greater than a')

Output:
Enter a 12
Enter b -12
a is 12 b is -12
a is greater than b

This is an example of an IF....ELSE condition. In this case, if the condition is true the first set of statements are executed else the statement after the 'else' tag are executed. Here we improvise on our if condition in a certain manner. You don't need to check the condition twice for the same thing as it speeds up execution and makes your code much lighter if the condition is huge enough. This is a simple example hence it won't make more than a few milliseconds of difference but in real world case scenarios it can make significant differences when you write huge pieces of code.

Note : input() method takes everything input as a string, you need to typecast the input you get it from the method into the type of data you want. You can use typecasting methods like int(),float(), etc to do that. 

Typecasting means changing the type of data from string to int or float. For doing input operations on number by using the input method you need to typecast as shown above or you can take the input into a separate variable and then use it for the operations. You can look up the python documents for more information and commands on typecasting.

Remember : Commands might be subjected to changes as the python language progresses. In python 2.x versions the input was taken using raw_input() but after python 3.x versions it is renamed to input().


Code 3:

Input:

  1. a=int(input("Enter a "))
  2. b=int(input("Enter b "))
  3. c=int(input("Enter c "))
  4. print('a is',a,'b is',b,'c is ',c)
  5. if (a>b and a>c):
  6. <tab>print ('a is greatest')
  7. elif(b>c and b>a):
  8. <tab>print ('b is greatest')
  9. else:
  10. <tab>print('c is greatest')



Output:

Enter a 1
Enter b 2
Enter c 3
a is 1 b is 2 c is  3

c is greatest

In this example, we use the ELIF or as we say in other programming languages ELSEIF conditional statement.  Here, the IF condition is checked then if its not true it checks the next condition and still if that's not true it'll execute the last set of else. It can run without else statement as well just like your IF condition. In this way you can check multiple conditions in one go.


Code 4:

Input:


  1. b=input("Enter the marks out of 100")
  2. a=int(b,10)
  3. print('The student has scored ',a,'/100.')
  4. if (a<40):
  5. <tab>print("The student has failed")
  6. else:
  7. <tab>if(a>=90):
  8. <tab><tab>print("The student has passed with Grade A+")
  9. <tab>elif(a>=80):
  10. <tab><tab>print("The student has passed with Grade A")
  11. <tab>elif(a>=70):
  12. <tab><tab>print("The student has passed with Grade B+")
  13. <tab>elif(a>=60):
  14. <tab><tab>print("The student has passed with Grade B")
  15. <tab>elif(a>=50):
  16. <tab><tab>print("The student has passed with Grade C")
  17. <tab>else:
  18. <tab><tab>print("The student has passed")



Output:


Enter the marks out of 10050
The student has scored  50 /100.

The student has passed with Grade C

Here is an example of a Nested IF...ELSE, you need to be careful about the indentation though. Hence the <tab> indentation option is shown, in the further exercises it might not be shown. So kindly be careful about the indentations and how they are to be used. In traditional languages we often put the statements under brackets and indentations are made to make the code more readable but in python indentations are the sole way of letting the compiler know where a particular loop or a set of statements begin or end.



In Next chapter we will learn about loops and how to use them.

<< PREVIOUS CHAPTER || NEXT CHAPTER >>  --INDEX--

~*~*~*~

This series is totally authored by me (Dhawal Joshi). Any similarities found on the text, or codes or anything is purely accidental. All the sources of reference will be mentioned, linked and will be given the proper credits. If I miss anything or there's anything wrong, feel free to comment or send me an email and I'll try to edit it out. I am not a Python expert, I am sharing whatever I have learnt on my own and with a few sources around to refer from which will be mentioned. Also feel free to share this series with others so most can benefit out of it.

~*~*~*~


Do comment and share your thoughts about it! I'd love to know what do you think. Also, I'd keep updating it quite often so do follow the Website to get all the updates by clicking here.

Also, a minor headsup.... Obsessed is free to read on Kindle Unlimited! Do check it out. I'd be glad to read your reviews!
Share :

Are you getting regular fresh content updates? If not click on the button below.

0 comments:

Post a Comment

Do comment and share your thoughts.