Monday, September 15, 2014

Python Tutorial 4 – If selection


Python Tutorial 4 – If selection
 Hey, welcome back. In this tutorial we will go through If selection. By the way selection is one of the basic concepts of programming. It helps users select from a multiple choices. Today we will write a little program that will let users to input their password twice and assure them that they wrote the same password.
Here is the source code >>

 
And when we run this (You can press F5 to run) >>

If is a statement that evaluates to a single selection. There above, we selected from correct password and wrong password. When we go through the code the first line obviously prints Please type in your password. Then the next line stores whatever password the user input in password1. Then we store what the user inputs when asked to retype in the variable password2. Then when we go to the if selection the first thing to know is how to write if statements. The syntax is >>

If the first condition evaluates to true, it will go through the statements inside if semi-colon. But if the condition evaluates to false, it will go inside the else statements. Easy logic. So in our case >>
 

So let’s run our program again  >>
 
As you can see up there, the second case must have gone through the else because it printed  Sorry, your passwords donot match!.
So that was all for this tutorial. I hope you understood the concept, it’s pretty easy.  See you in the next tutorial.