Using if statement

Using if statement

Lesson Details:
June 29, 2020


I: Introduction

Learning a programming language is a good start for those who want to develop applications and websites. Python is one of the most popular programming languages, it can be used for web development, desktop application development, and for various purposes.

Python is easy to learn and use that’s why this tutorial will help you to learn basic programming with python. After going through the contents of this tutorial you will be able to write your first program using python.

Before going further you need to understand what a programming language is. A programming language consists of a set of instructions to perform a particular task, which is readable by a computer. A computer doesn’t understand English or any natural language so they use programming language to communicate with people.

II: Body

Python is an interpreted high-level programming language. It is very easy to learn if you are new to programming. Python can be used to develop desktop applications, web applications and also mobile applications. Both free and commercial software are available for python.

A: Using if statement

The if statement is very important in programming. Let’s start with an example. Suppose we have two variables named num1 and num2. We want to compare them and print the smaller value on screen. For that purpose, we use if statement as shown below:

if (num1 < num2): print ("Smaller") else: print ("Not Smaller")

This will work fine if num1 is less than num2 but if they are not less than each other, the else block will execute which is not what we want. So let’s wrap our code in if statement like this:

if (num1 < num2): print ("Smaller") else: print ("Not Smaller")

Now if both numbers are not less than each other then the else block will not execute. If you run this program on your local machine, it will print the string “Not Smaller” on screen. But if you run it online, it will print “Smaller” on screen because it depends on the input values. You can try different values to see how it works on different inputs. This is called an online testing.

You can use if statement for more complex situations as well. To understand more about how to use it, checkout this introduction to if statement tutorial.

B: How to check True or False Value?

Sometimes you may want to check whether a variable is equal to True or False. For example, if you don’t want someone to enter some false information on your website then you may want to verify the data entered by him/her before accepting it. You can do that by checking if the value of variable x is True or False. Let’s take a look at some examples.

If x equals True, then this code will execute:

if x == True: print ("x equals True") else: print ("x does not equal True")

While running this code online it will always print “x does not equal True” on screen because it depends on input values and we will get different results each time we run it. The output depends on the input values and we get different results each time we run it online. The output depends on the input values and we get different results each time we run it online. If x equals False, then this code will execute: if x == False: print ("x equals False") else: print ("x does not equal False") While running this code online it will always print “x does not equal False” on screen because it depends on input values and we will get different results each time we run it online. The output depends on the input values and we get different results each time we run it online. This code checks whether x is equal to False or True: if x == True or x == False: print ("x is equal to True or False") else: print ("x is neither True nor False") If you run this code online you will see an error message because True and False are only reserved words in Python and cannot be used as variable names. So when Python sees True = 10, it thinks that True is a variable name and tries to assign the value 10 to it but since there is no such variable, it throws an error. This code checks whether x is equal to False or True: while x != True: x = input(“Enter a number between 1 and 10: “) while x!= False: x = int(input(“Enter a number between 1 and 10: “)) Here while loop runs until the condition becomes true or false i.e., while x != True or while x!= False respectively. In both cases it will continue until user enters a number between 1 and 10 otherwise an empty string will be printed on screen if user enters something else other than valid number Between 1 and 10 initially, after user enters a valid number between 1 and 10 then while loop ends and the code inside the while loop executes that means int(input(“Enter a number between 1 and 10: “)). Then run this code online and enter different numbers between 1 and 10 and see how it works For example, if user enters a valid number 7 then from above code it runs again from while x!= False i.e., from Else part but since here user entered a number between 1 and 10 so while loop ends and the code inside the while loop executes that means int(input(“Enter a number between 1 and 10: “)). Then run this code online and enter different numbers between 1 and 10 and see how it works For example, if user enters a valid number 7 then from above code it runs again from while x!= False i.e., from Else part but since here user entered a number between 1 and 10 so while loop ends and the code inside the while loop executes that means int(input(“Enter a number between 1 and 10: “)). Now when you run this code online, enter 7 in the text box then hit enter button then it will display 7 because user entered number between 1 and 10 during runtime of while loop or else it will throw an error message because of conditional check in while loop like this if x does not equal 0 so its better idea to use range function for this purpose i.e., instead of writing total = total + number simply use total = total + range(1,10) so here number automatically becomes number divisible by 10 so no need for conditional check inside while loop like this if x does not equal zero so its better idea to use range function for this purpose i.e., instead of writing total = total + number simply use total = total + range(1,10) so here number automatically becomes number divisible by 10 so no need for conditional check inside while loop like this if x does not equal zero Now while loop runs until user enters any character other than digit or minus sign otherwise an empty string will be printed on screen if user enters something else other than valid character Like this while loop runs until user enters any character other than digit or minus sign otherwise an empty string will be printed on screen if user enters something else other than valid character Like this while loop runs until user enters any character other than digit or minus sign otherwise an empty string will be printed on screen if user enters something else other than valid character Like this while loop runs until user enters any character other than digit or minus sign otherwise an empty string will be printed on screen if user enters something else other than valid character Like this while loop runs until user enters any character other than digit or minus sign otherwise an empty string will be printed on screen if user enters something else other than valid character Like this while loop runs until user enters any character other than digit or minus sign otherwise an empty string will be printed on screen if user enters something else other than valid character Like this while loop runs until user enters any character other than digit or minus sign otherwise an empty string will be printed on screen if user enters something else other than valid character Like this while loop runs until user enters any character other than digit or minus sign otherwise an empty string will be printed on screen if user enters something else other than valid character Like this Now lets try another example together now suppose we want to check whether a person is male or female so he/she must have entered M for Male or F for Female or anything else otherwise an empty string will be printed on screen Now lets try another example together now suppose we want to check whether a person is male or female so he/she must have entered M for Male or F for Female or anything else otherwise an empty string will be printed on screen Now lets try another example together now suppose we

loader
Course content