Introduction
Hello Everyone and welcome to my Fith lesson of a new series about the Python language. I also have a series on the Java programming language which can be found here https://steemit.com/programming/@robertlyon/java-programming-for-beginners-course.
The previous lesson of this series(Lesson 4 - Operators) can be found at https://steemit.com/programming/@robertlyon/python-programming-for-beginners-lesson-4-operators
In this lesson, we will be looking at if statements and the control flow of programs, if you can remember in the previous lesson we briefly touched on IF statements and how they can allow your program to make decisions based on certain conditions. I will be using the same program as the focus of this lesson.
So let's dive straight in.
Conditional operators
If you remember in the previous lesson we covered some of the basic operators that are used in the Python language, Those operators are mainly used for manipulating numbers. When we are working with conditional statements we use conditional operators, this allows us to get a true or false answer from calculations.
You may be wondering why we need a true or false answer, look below for an example of why.
if(true)
--Do something
else(false)
--do nothing or dosomething else
As you can see above the true or false answer gives us two different options, as we get more specific with our true and false answers we can obtain more options, look below for an example of this.
if(x is equal to 1)
--Do this
elif(x is equal to 2
--Do this
else(x is not equal to either 1 or 2)
--Do this
As you can see in the example above we now have three different choices that our program can make based on the value of x.
Before we start looking at code, lets have a look at the different conditional operators we have available to us.
Conditional operators
These operators should be familiar to you and work in the same manner as they do in mathematics.
- Greater than - > - (7 > 5)
- Less than - < - (5 < 7)
- Greater than or equal to - >=
- Less than or equal to - <=
- Equal to - == - (5 == 5) - Not to be confused with = which is the operator used to assign values to variables.
These operators may be slightly different to you, if you are familiar with logic gates then you will be familiar with how they work, these are known as logic operators.
- AND - && - Only true when the conditions on both sides of the operator are true e.g. (6 == 6 && 5 == 5) - this can be read as (6 is equal to 6 AND 5 is equal to 5), since both conditions are true, this statement would return true.
- OR - || - This is true when a condition on either side of the operator are true e.g. (6 == 6 && 5 == 1) - Because 6 == 6 is true this statement would return true even though 5 == 1 is false.
- NOT - ! - This operator returns the opposite of the condition it is added to e.g !(10 == 0) - Although 10 == 0 would normally return false, the not operator changes this evaluation to true.
These operators are very important in programming and you should practice using them until you are comfortable with them, once you have covered the syntax of the IF statement in the next part of the lesson you should come back to these operators and practice making your own statements.
IF statement Syntax
We will now take another look at the program from the previous lesson and have a look at how the IF statement is constructed and how logic operators were used to make decisions in the program.
Figure 1 - Basic calculator program.
print("Welcome to your basic calculator program.\n")
print("In this program we will be focusing on how operators work.\n")
print("Please practice with all of the operators learned within the lesson.\n")
num1 = int(input("Please enter a number:\n"))
operator = input("please enter an operator:\n")
num2 = int(input("Please enter a second number:\n"))
if operator == "**":
print(num1 ** num2)
elif operator == "*":
print(num1 * num2)
elif operator == "/":
print(num1 / num2)
elif operator == "%":
print(num1 % num2)
elif operator == "+":
print(num1 + num2)
elif operator == "-":
print(num1 - num2)
else:
print("Error")
So as you can see in the program above which you should be reasonably familiar with from the last lesson, we have an if statement that chooses what happens to the numbers we input based on which operator the user inputs.
The first thing we need to include when we want to create an if statement is the "if" keyword. This keyword is a prompt to let the program know that it is entering an if statement, without it the Python interpreter would have no idea that you wanted to use an if statement.
So the first part of the If statement for the program is as follows.
if operator == "**":
print(num1 ** num2)
as you can see we have the "keyword" followed by the variable "operator". The next thing we have in this line is the equal to operator "==", next we have the string "" and finally a semi-colon ":". This line can be read as "if the value stored in operator is equal to the string "":" The semi-colon lets the python interpreter know that you have finished declaring your condition(this is an easy thing to miss and a common error so keep this in mind). This means that if that condition is true the print statement declared within that if statement is executed.
However, if it is false then that branch of the if statement is not executed, instead the program will move onto the next condition, which in this program is
elif operator == "*":
print(num1 * num2)
In this part of the if statement we can see that the keyword "if" has been replaced by the keyword "elif"(which is short for else if). The elif keyword makes it easier for you to have more choice in your program, it basically means that if the condition above is false then try this condition. for example if "operator == "**"" from above was false then we would move to this elif statement and if that is false then we move to the next and so on until we reach the else condition or we run out of conditions to execute.
The else keyword is used at the end of an if statement and is usually used as a default case, you can think of the else to mean "If none of the other conditions are true then execute this". In the program above if the user did not enter any of the operators that are included in the IF statement then the else condition would be executed and "Error" would be printed to the screen.
Conclusion
This introduction to conditional statements is pretty basic and there is more to cover on the topic but this should give you some basic knowledge to go out and try and create your own if statements and allow you to create programs that can choose different paths depending on input. If you have any questions about Python or programming in general, then please ask in the comments below. I will try my best to help you :)
Look forward to my next lesson on Switch statements.
As always if there are any improvements you think I can make to this post then please leave a comment and I will consider adding it.
Thank you for reading and I hope that someone will get some use out of these tutorials.
Message to readers
Thanks for taking the time to read my post, if you are interested in Science, Technology or Computer Science then check out my blog, I am making an effort to provide good quality original content to the Steemit community.
This post has received gratitude of 0.54 % from @appreciator thanks to: @robertlyon.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
This post received a 92.84% upvote from @morwhale team thanks to @robertlyon! For more information, click here! , TeamMorocco! .
STEEM Price : 6.197 $
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
You got a 0.99% upvote from @upme requested by: @robertlyon.
Send at least 2.5 SBD to @upme with a post link in the memo field to receive upvote next round.
To support our activity, please vote for my master @suggeelson, as a STEEM Witness
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit