[no audio]
Friends, here we are going to discuss
about 'if-else' conditional statement.
So before going to enter with your 'if-else' conditional statements,
already, we know simple 'if' condition, right?
So, using that I want to write a small script, right.
Let me open my editor.
So here I am going to use Sublime Text Editor.
You can use any editor.
Let me give some name for my Python Script.
The name is, convert into title format,
so 'get_title_format'.
[no audio]
'get_title_format.py'.
[no audio]
So my intention is, I want to read
some string. That string,
I want to convert into title format.
Right. First, let me write that.
[no audio]
'usr_str',
I'm taking a variable called 'usr_str'.
So, I want to read a string.
So, if you want to read a string, you have to take always 'input(
"Enter your string: ")'.
[no audio]
That's it. Then after that, simply
what I am doing is, I am printing your 'usr_str.title()'.
You know, if you want to convert any string into title format,
there is a string operation called 'title()'.
Right. Let me open my command line.
Right. So, we have our scripts on 'Desktop\PythonScript'.
So here our script is
[no audio]
'get_title_format.py'. See that script.
We have only two lines of script.
If you run your script it will run from top to down. The very
first line is 'usr_str=input'.
So because of this your script will ask something like,
"Enter your string:" Let me run this.
Yes, it is asking. I am going to provide 'python scripting'.
[no audio]
So now if you press enter on your keyboard, what will happen?
Whatever the data you are providing here,
that data will go and store into this variable. And in the next
step what we are doing? So, this line is over.
If you press 'Enter', this line is over. Then after that, simply
your Python is going to print your given string,
I mean 'usr_str'. That string is converting into title format,
then it is printing. See the output?
Yes, you are getting 'Python Scripting', capital 'P' and capital 'S'.
This is fine.
But, before converting into title format, the script has to
ask like, "Do you want to convert this script into title format or not?"
If I say 'yes', then only it has to convert. If I say 'no', it
won't convert, but it has to print the given string as it is.
[no audio]
See that. What I am doing? Then I am taking user confirmation
as a variable, 'usr_cnf', and I want to read 'yes' or 'no'.
'yes' or 'no' is a string.
That's why again, I am taking 'input'.
"Do you want to convert
[no audio]
your given string
into title fmt?
Say yes or no."
[no audio]
That's it.
Right. Now, if user says 'yes' then only I want to convert the
data into title format, and then I want to display.
So, two lines. First line is reading a string, second line is getting
confirmation from the user.
Right. So, whatever the user is going to enter, I mean either 'yes'
or 'no', that we are storing into this variable. Now I am checking
with 'if' condition, 'if usr cnf=="yes"',
then only I want to execute this line. That is
[no audio]
this line, if user says "yes", then this value becomes 'True'
no, because user is entering "yes", and that value
we are storing into 'usr_cnf' variable.
Now we are checking 'usr_cnf', and this direct string,
are they equal? If user entered "yes", then yes, definitely
this is equal. If it is equal,
then this 'if' is allowing to execute this line by your Python.
That's it. Right. First see the output.
So, let me clear my screen.
Let me open your script.
Now I am going to run. So, whenever if you run you know it is
going to execute from top to down. Very first line. See the
output. First line executing, "Enter your string".
I am entering 'python scripting'.
Then it is asking, because of second line,
"Do you want to convert your given string into title fmt?
Say yes or no?" Let me type "yes",
and I am pressing 'Enter'.
So whenever if you hit 'Enter' on your keyboard, what will happen?
This 'yes' value will go and store into this. That's over. Then
two lines are over. Then your Python immediately
goes to third line. This is the third line.
So now you are entering "yes".
This 'yes', and this 'yes', both are equal.
That's what we are checking here.
If they are really equal, then this value becomes 'True'.
If it is 'True', then this 'if' will allow to execute this
line, that's why, see the output.
You are getting in title format.
I mean capital 'P' and capital 'S'.
Now, let me rerun and I am providing
simply 'python scripting' once
again. But I am saying 'no'. No, you're not getting any output.
See whenever if you enter 'no', you know 'no' is going to store
into this variable.
Now in your hand this variable is like 'no', 'no==yes'. No, 'False', no.
'no==yes', 'False'
If it is 'False', then 'if' condition does not allow to execute this line.
That's why you're not getting any output.
But my requirement is, even
though if you enter 'no', at least I have to get normal string
display. I mean the string whatever you are giving same string
I want to get, if I say 'no'. If I say, 'yes' I want to get
in the title format. Right.
So that's why what I am doing is, I am going to add one more
'if condition, 'usr_conf=="no":'
that means user is not willing to
convert their given string into title format.
Then at that time simply directly print your given string
as it is.
That's it. Right. Now, see the output.
[no audio]
'type get_title_fmt', title format.
Let me run this script, and see the result.
I am giving suppose
"welcome to
[no audio]
python scripting", and I want to convert this as a title format.
That's why I am saying to my script please convert it.
I'm entering "yes", then you are getting in title format.
Then rerun, right.
I am entering, "welcome to python scripting", but now I am saying
"no". If I say "no", what will happen? You are getting as it is
your string. Then how your script is working?
First observe here. Whenever you say "yes", this is 'True',
that's why you're executing this line. At the same time, whenever
you are entering
"yes", then this is 'False', no.
"yes", you are comparing with "no", 'False'.
That's why, this 'if' condition is executing this line whenever
you say "yes", and this 'if' condition is not executing your
line whenever you say "yes". That means if you say "yes",
this condition is 'True', and this condition is 'False'.
That's why this 'if' condition is executing this line, and
this 'if' condition is not executing this line. That's fine.
[no audio]
Okay. Now, let, in the second case, you are entering "no", right.
Whenever if we say "no",
"no", this 'if' condition is becoming 'False', that's why this is not
executing. And this 'if' condition is becoming 'True', that's why
you're executing this line,
I mean your Python is executing this line.
That's why you're getting this one.
Yeah, this is okay.
Your script is working perfectly as per your requirement.
But the thing is, guys if you say "yes", if you say "yes". Assume
that if you say "yes", this condition will definitely become
'True'. Then you are printing this line.
At that time, you don't need to check this condition, right.
Already user is saying "yes", then why you are checking for "no"?
Right. If this is 'True', you don't need to check this condition.
If this is 'False', then just check this condition.
Right. So guys whenever if you have only two options like "yes" or "no",
Like "yes" or "no".
Whenever if you have only two options like "yes" or "no" at that
time you don't need to write two separate 'if' conditions, simply
you can write 'else' here.
[no audio]
Then how it is going to work?
Nothing is there guys. Very simple. Observe that.
in case if this condition
is 'True', if this condition is 'True', your Python will execute this line.
I mean, 'if' is allowing to execute this line, and if this condition
is 'True' by default your Python is going to skip this part,
Python won't check this one.
[no audio]
But in case if this condition is 'False', this result is 'False',
you know 'if' is not going to execute this line.
It is not allowing to execute this line.
And if this is 'False', be clear, if this is
'False', by default without checking any condition your Python
will execute the 'else' block, in 'else' whatever you have
the code, that code will execute without checking any condition.
[no audio]
Right. So, remember that when you have to take "if else". Whenever if
you have only two possible values for your requirement, then
only you have to take 'if' and 'else'.
[no audio]
See for user confirmation, as of
now we are treating only "yes" or "no". If it is
"yes", this line has to execute. If this is "no" by default
we have to execute this line.
So, if you take "if else", once again I am repeating, if you
take "if else" condition, if whatever you're writing here, if
that is 'True' your Python will execute this line, and your
Python don't consider this part.
If this is executing, your Python simply by default it will
skip this part.
You don't need to bother about that. In case if this condition
is 'False' then only your Python will execute this line or
the block which is there under your 'else' without checking any condition.
Right. Let me take one more example so that it will be clear for you.
Now I am going to comment this.
Otherwise, I will write new script.
[no audio]
'find_greater_number.py
See my intention is, I want to read two numbers and then
I want to compare, so whatever
number is higher, that number I want to display.
See that. So to read your numbers you can take 'eval()' function,
then 'input("Enter your number: ")'.
So guys, if it is string don't take 'eval()' function.
Right. No need of taking 'eval()' function if it is a string. If it is
any value other than a string, then you have to use 'eval()' function
in Python3. "Enter your first number".
[no audio]
Then, I'm going to take one more number from user, second number.
Let's say 'b'. 'input("Enter your second number: ")'.
That's fine. Now, see directly
I can't display like,
' is
[no audio]
greater than ', I can't write directly in this way,
right, because without knowing the relationship between your
a and b, how can I say a is greater than b, or let's say one more line,
' is greater than '. I can't say, right.
[no audio]
Without knowing relationship between, without knowing your
a and b values, I can't decide which statement I need to display.
But if you remember, if you remember, you have
comparison operators, right.
Let me enter into my Python.
[no audio]
Let's say 3 < 2, sorry 4 <, any number, any
number 4< 2, it's 'False'.
Now I can take a = 4 and b =2.
Now I am comparing a < b, if really a < b then you will get 'True',
otherwise you will get 'False'. Then this 'True' or 'False' result,
I can use in 'if' condition.
Right. Whenever you are getting result as 'True', or 'False'
always I can use that in my 'if' condition. That's why what
I am doing here is, if I want to print this information,
right, if I want to print this information, a value must be
greater than b, that's why I can write here,
'if a > b', if really 'a > b', this value will become 'True'.
So, if it is 'True', be clear guys, if the result of a > b
is 'True', then only I want to display this line.
[no audio]
Right. 'if a > b' then only, I want to display
this line. The same way suppose 'if b > a'
then I want to display this line.
[no audio]
Right. So, what I'm doing is
[no audio]
if, if really a > b then definitely you'll
get a 'True'. If it is 'True', then you are going to execute this
line. Then it will work. The same way, if b > a,
then I can print this line.
Now it will work, but see the result.
[no audio]
So, our script name is 'find_greater_number', right.
So, I am going to run this. I am giving numbers like 5 and 9.
Yes, 9 is greater than, I'm taking I think both are 'b' values here.
Yeah, here I need to write variable 'a'.
[no audio]
Now see that, I'm entering first number as suppose some 5
and second number as 9, you are getting 9 > 5.
Let me take 3 and then 1. 3 > 1. It's
working perfectly.
Right. Now, instead of writing a > b and b > a
suppose, just assume,
it's not perfect,
If a > b, I want to display this information
if it is 'False', then I'm going to display this information.
It is correct up to a certain point. That is, just observe that.
I am entering in this way.
[no audio]
4 and 5. What happened?
Yeah, sorry. It's my bad.
I'm entering number 5, and then I entering some number
7. 7 > 5, it's working.
Then I'm entering 7 and some 2,
[no audio]
7 and then 2.
Yes, 7 > 2, it's working.
But if I give both are same numbers 5 and 5. You're getting
5 > 5
That is not at all correct.
Right. Now, see here.
[no audio]
When you have to take if and else? When you have to take if and else?
If there are only two possibilities for your result, I mean for your value.
See, if I take two values,
let's say 2 and 3. Assume that this may be greater than
this or maybe less than this or maybe equals to that, right.
If you take two variables like 'a' and 'b',
based on 'a' and 'b' values, a = b or a > b or
b > a, three possibilities are there. Whenever
you have three possibilities, you can't take simply 'if' and 'else' block.
You can't take simply 'if' and 'else' block, then
how you can write your code?
[no audio]
Right. See that. Now how I am going to manipulate this thing.
See you can add in this way.
'elif', if a < b,
[no audio]
Then I can write.
b > a, or I can also write a < b, 'a' is
[no audio]
less than b.
or else if maybe a==b, right.
There is a chance to have an equal.
So, at that time I can write.
[no audio]
Sorry, 'a' and
'a' and 'b' are same, are equal. That's it.
[no audio]
Now guys, observe here. 'a' may be greater than 'b', or 'a' maybe less than 'b',
maybe there is a chance to have 'a' and 'b' values are equal.
That's why whenever if you have multiple possibilities
while comparing your values, then you can extend your 'if'
condition in this way. 'if', 'else if' one more condition, 'else if'
one more condition, you can also write, if you have
multiple possibilities you can also write one more 'else if'.
[no audio]
Right. Fine.
Let me run this.
[no audio]
See, I'm entering 4 and 4.
What happened?
4 is less than 4, maybe 'a' and 'b'.
Yeah. I have given 'a' and 'a'.
It's my bad once again. Okay, see here.
I'm entering number 4 and 4. 4 and 4 are equal. 4 and 7, or let
me take some first higher number, second lower number.
It's working perfectly.
[no audio]
Right. Fine.
Now, see here.
[no audio]
Guys, first thing
[no audio]
you are having a simple 'if' condition.
Second thing, whenever if you have two values for your required
thing then at that time you can take if and else.
But here whenever if you are comparing two values, right, you have
three possibilities: first number greater than second number,
or first number less than second number, or first number and
the second number both are equal.
So, at that time you can use if, else if, else if condition, right.
And one more thing,
here three possibilities, right?
So, by this block, by this block, by these two conditions, you
are checking greater than and less than. So if these two conditions
are 'False', assume that, if these conditions are 'False' then
by default, a and b are equal, right.
So, at that time what you can do is simply you can write 'else'
block. No need to check your condition, whether a==b or not.
[no audio]
Why I am writing 'else'? If it is right in this way,
it's okay, no problem. It's okay.
It will work perfectly. Without any confusion
it will work perfectly.
But what I am saying is, assume that I am giving 'a' value 3
and 'b' value 4.
[no audio]
Yeah, first I will do this thing. 'a > b'. 'a' value
3 and 'b' value 4. This is 'a' and this is 'b'. 'a> b'? No, 'False'.
If this is 'False', your Python will skip this line because
'if' doesn't allow to execute this line. Then immediately your
Python will come to second condition. Here it will check.
'a < b', yes, 'True'.
If it is 'True', your Python will execute this line and remaining
all remaining conditions your Python by default
it will skip. It won't check. The first condition
if it is 'True', remaining all conditions are going to skip
whether they may be 'True' or not,
you don't bother about that your Python is going to skip
that, right. But in our case suppose as of now if I take 3
and 3, both are equal.
So, this condition definitely 'False', because you're checking
3 greater than 3? No, 'False', because they both are equal.
So, this is 'False', this is 'False'.
So, whenever you have two values and greater than and less
than conditions are false,
then remaining option is equals to, equals only, right?
That's why what I am saying is, you don't need to check this.
So directly I can write 'else' here.
So 'else' means, you know default block. When all above conditions
are false then only 'else' will execute. When all above conditions
are false then only 'else' will execute.
So, if we take any two values which are equal for your first
and second variable 'a' and' b', then this is 'False'.
This is 'False'.
That's why your 'if' is going to execute 'else' block by default.
In case if any one of the conditions is 'True' that condition
will execute.
Otherwise, if all conditions are 'False',
then only 'else' will execute.
Right. Fine. So, guys this is all about simply your 'if-else' and 'if-else if'
'else if' condition. Okay.
So, in next video, we will write one simple
script to understand effectively your 'if', 'else if', 'else if' condition. Okay.
Okay guys, thank you for watching this video.
[no audio]