[no audio]
Friends, here we are going to practice
with your conditional statements,
and that practice is, read a number between 1 to 10,
right, and then display it in terms of words.
So if you run your Python script, it has to ask, "Enter your
number". Then you have to enter any number like 1, 2, 3 or up
to 10. Then after that, through your Python script you have
to display that number in terms of words.
So that is what I am going to develop with your conditional
statements, right?
Let me open my editor.
So this is my editor.
Let me save the script name as 'number_to_word'.
'number_to_word.py'. That's it.
So guys first step is, your script has to read some number.
Let me take 'input', or 'num='. See guys,
we are reading a number,
that's why you have to use 'eval()' function in your Python 3.
'input', then here simply write, "Enter your number: ".
[no audio]
Right. So after that whenever you are entering the, number,
you have to convert that number into word. Suppose
let's say you are entering number 1,
then your given number is '1', and then you have to print it
in terms of words like 'o-n-e', "one".
So, directly I can't write in this way, 'print()',
you are entering number '1' suppose, right.
So for that I can't write in this way,
or simply, if your number is '1', right, if your number is '1' then
only I have to print this, "one", "o-n-e", "one'. If your number is
2, then I have to print "t-w-o", "two". Let me write first
'print("two")'. 'print("three")'.
Let me write it first so that you will get idea.
'print("four")', 'print("five")'.
[no audio]
Let me repeat it up to "ten".
'print("six")'.
'print("seven").
[no audio]
'print("eight")'.
'print("nine")'. Then, let me print it, 'print("ten")'.
That's it. But if I run this script, right. Let me run it first.
So our script is,
number to word conversion, right.
[no audio]
So, this is your script. Now if I run that, what is happening?
Because of very first line your script is going to ask,
"Enter your number: ".
Let me do that. "Enter your number: ". Suppose
I'm entering number "2".
So if I enter number 2, it has to display only "two" but
your script, whatever you have as of now, that is going to
print one, two, three, four, five, six, seven, eight, nine, and
ten. This is not correct.
If your given number is 2, then you have to print only "two". That
means you have to take a decision before going to print your statement.
[no audio]
Right. So, you know how you can take a decision. You can take a
decision using conditional statements.
That's why what I am doing is,
I'm writing 'if num', if it is
equal to 1, then I want to print this line. That's it.
Same way 'if num==2:', then I want to print this line.
Likewise, let me repeat up to, 'if num==3:',
then I want to 'print("three")'.
Right. So guys now I am using a very simple "if" condition, because
you can write your logic in any way.
But while writing you have to write some effective script.
For time being I am writing simple way using your
simple "if" condition. I will also show you some effective scripts to
implement this logic.
[no audio]
Let me do it.
[no audio]
Let me do it up to 9, then finally you have 10. Right. See that.
'num==10'.
That's it. Now go and run your script, right,
and then enter any number, "5" you're getting only "five", then
"1" you are getting "one". See that it's working perfectly.
Right. So as of now, whatever the script you have that is
working perfectly. But same script, assume that, same script
I am running by providing,
[no audio]
by providing a number called 12. Now you're not getting an output.
Right.
So if your entered number is 1 to 10 range then only
you're going to display.
So that's what you have to mention in your script.
So I am entering, "Enter your number: ". "Enter any number:",
"Enter any number between 1-10 range:"
Okay, for time being we'll keep this message in this way.
Now, if you run your script, yes, you are observing, "Enter
any number between 1-10 range:"
Now, I'm entering suppose "3".
Yes, you are getting. But even though if you are displaying,
"Enter any number between 1 to 10 range:", by mistake I entered
11. Now, you're not getting any output.
That's why what I am doing is, if any number is not in the given
range, 1-10 range.
I want to display like, I want to display like,
"Your number is an out of range number".
That's why I'm adding one more
'if' condition. At the ending, 'if num not in', if you remember
there are membership operators, 'not in [1,2,3,4,5,6,7,8,9,10]:' range.
Then I can say, 'print(
"Your number is
[no audio]
out of range.
Please select
between 1-10 range"). That's it.
By mistake if you enter any number other than 1 to 10 range. So guys
first of all you have to know what is the membership operator
here. See when this condition result, when this expression
result will become 'True' means, let's say you are entering
number 11, then 11 not in this range, right?
That's what we are checking.
If 11 is not in this range, then this expression will become 'True',
then you are executing this line.
Let's say, 10. 10, not in this range? No, 10 already
it is there, but you are checking for not, that's why if 10 is
the number then you will get this as 'False' result.
That's why you are not executing this.
Right. So guys, this is the simple way to use your 'if' condition
for this requirement. But here there is a disadvantage.
Of course, your code is perfectly correct.
It will work for your number,
I mean if any number you are going to enter between 1
to 10 range. But here problem is,
first let me explain with,
let us assume your given number equals to 1, then what will happen?
See, whenever if you run, your script will execute from top
to down. Because of very first line your script is asking,
"Enter your number between 1 to 10 range:". Suppose
I am entering 1, then this 1 will go and store into your 'num' variable.
Right. So after that, in second line you have "if" condition. So 'if
num==1'. Yes, our number is 1, and we are checking with 1.
This is true, no.
Then you're printing this. Then your first 'if' condition expression
is 'True', and you're printing only "one"
That is over. Then immediately
your Python will go and check next condition as well because
all are individual 'if' conditions. There is no relation between
any one of the 'if' conditions here. All are separate blocks, separate
rooms. That's why your Python will go and check with second
'if' as well, 'num' is 1, but we are checking with 2, '1==2'?
No. 'False'. Likewise, all conditions are 'False', but we know
that if 'num' equals to 1, why should we check unnecessarily
all these conditions? Anyway by default all are 'False' if your
number equals to 1. Even though if your number equals
to 1 unnecessarily you are checking all these conditions, because
all are separate 'if' conditions. If you remember we have 'if-elif'
condition. What is the usage of that? Nothing is there.
If I add in this way, 'elif', then the entire 'if',
is one 'if' condition only. This entire logic is one 'if' condition
only. Let me add it first, 'elif'. Then I will explain.
[no audio]
So now the meaning for this is, you have only one 'if' condition.
This entire thing is like one 'if' condition now. Previously
all are separate 'if' conditions, there is no relation between
any two of your 'if' conditions previously.
But now, this entire, from 2 - 23rd line, the entire
thing is one 'if' condition, but this is 'if-elif' condition.
Now how it will work? See, because of by adding 'elif' your
all 'if' conditions, are treated as one 'if' condition by your
Python. Then what is the advantage of writing in this way,
is, let me explain with same 'num' as 1.
'if num==1', what will happen?
First 'if', by default your Python will
check this 'if' condition, 'num==1'.
Yes, 'True'. If it is 'True',
your Python will execute this line. Your 'if' will allow
to execute this line, and if any one of the conditions, here
first condition itself it is 'True'.
That's why by default,
your Python is going to skip all these conditions without
checking. That is the advantage if you club all your 'if' conditions.
So that you are going to save your execution time, right?
Unnecessarily you're not going to check remaining conditions.
If very first condition is 'True', then definitely we know
that remaining all are 'False',
why should we go and check them?
[no audio]
So to avoid that you have to write all your conditions, all
'if' conditions as one 'if' condition. So that is possible by
writing in this way, 'if-elif-elif-elif-elif-elif',
this entire thing is now one 'if' condition.
Now let me explain with some other number. Let me take my input is '3'.
I am providing input as 3, then 3 is going to be stored in your 'num'
variable, in this. So while executing
for first line you're
providing number 3. Then first line, execution of your
first line is over. Now in your 'num' variable
the value is 3. Then immediately your Python will come to the second
line, immediately. '3==1', 'False'. Because this is 'False'.
Because this is 'False',
that's why your Python is coming to next 'if' condition, next
'elif' condition. This is 'False'.
That's why Python is coming to 'elif' condition.
So number three. '3==2'?
No, 'False'. Again, it is a 'False'
that's why we have one more 'elif',
that's why your Python is coming to here and it is
checking here.
'num==3'? Yes, it's 'True'.
This is true,
your Python will execute,
I mean your 'elif' will allow to execute this line.
So now this condition is 'True',
you are executing this line.
So if condition is 'True', then your Python will skip remaining
all conditions. Whether they are 'True' or 'False' your Python
won't take care.
If the first occurring condition is 'True', then remaining
all conditions are going to skip without checking.
[no audio]
Right. So this is the advantage by clubbing your multiple 'if' conditions.
[no audio]
Right. So I think this is fine. Now, here also I can reduce one more
'elif' condition. Guys, just assume that. Assume that
this condition, second, third, fourth, fifth, sixth, seventh, eighth,
ninth and tenth, if all are 'False', then what is the meaning for that?
Definitely your number is not equal to 1 or 2 or 3
or 4 or 5 or 7 or 7 or 8 or 9 or 10.
Your number is out of 10 range.
So why should we check this? If all conditions are 'False', by
default we can say that, "Number is out of range".
That's why, without writing your 'elif', simply you can write
in place of this line, simply 'else'
you can write it, 'else' you can write it.
If you want to keep it as 'elsif: ', "Your number not in
group of values".
No problem. That's fine.
But we know that if all first ten conditions are 'False' means
definitely your number is not in the range of 1 to 10. Then
no need to check condition again. Directly
you can say that if all above conditions are 'False', then
you can say that, "your number is out of range.
Please select between 1 to 10 range."
Right. So guys the 'else' is your optional.
So you have to remember that. When 'else' will execute means,
if all above conditions are 'False' then only your 'else' will
execute, otherwise it won't execute.
Right. And you have to
remember one more thing, while executing your 'if-elif' condition,
if any one of the condition is 'True', then your Python will
execute the lines which are there under that particular 'if'
condition, and remaining all will skip, all will skip. They
won't execute. They won't even be touched by your Python.
In this way you're going to save your execution time.
Right. Fine. And guys, if it is fine, then it's okay. Here also
there is one disadvantage.
What is that?
[no audio]
First of all let me execute this, and show you the result.
[no audio]
I am going to execute it, right. Sorry, not that.
[no audio]
I'm entering suppose let's say 4. Yes, you are getting "four". Now,
I am entering some number 34.
So, "your number out of range,
please select between 1 to 10 range".
[no audio]
Right. Fine.
Guys, as of now this script is also perfectly correct, but
if your number is out of 10 how is your execution happening?
Just check it once.
So let us assume your number variable,
I mean from command line for this line you're providing number
as some 12.
If it is 12, right, how is your Python going to execute your
if-else condition? From top to down. Immediately after reading
your number Python is coming to your first 'if' condition. So
it is going to verify, your given number is equal to 1?
No, 'False'. So this is 'False',
that's why your Python is coming to next 'elif'.
Number equals to 2? No, 'False'. Then again
your Python will go to next 'elif'. Number equals to 3?
No, 'False'. Again,
it will go to next. Likewise,
it is coming to last.
Then suppose here the number equal to 10?
No, because your number is 12. Then next your Python is trying
to execute next 'elif' or 'else' condition,
but as of now, if it is 'else' you don't have any condition.
If you don't have any condition, by default this line will
execute. So that if all are 'False' then only by default
your last line will execute.
Yes, in this way it is working.
Now, I have an idea. That is,
[no audio]
so if number is out of 10 range then unnecessarily you're checking
all these conditions, right, all these conditions,
then you are deciding number is out of range. After verifying
all these conditions, after that only, you are deciding your
number is out of range.
That's why what I am doing is, see that, I am writing one advanced way
[no audio]
this script. Let me comment this one, entire thing.
Right. Just assume you're reading a number. So I am also going
to comment this line.
[no audio]
So I will write once again that line at last.
So assume that you commented your previous script. Now, we
are going to write new script in the same program, right?
I am reading a number. At the very first itself
I am checking, that is 'if', if your number not in the range
of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, then if it is not, then if it is not in the range,
otherwise I will check it.
If your number in this range, then only I will
try to convert your number into word, right?
So here I will write a logic.
[no audio]
"Converting your num into word", but I'm not writing that
logic. We will see it. 'else' means, when 'else' will execute? If
your number is not in the given range, then only 'else' will execute,
then I can say, "out of range number.
Please select
[no audio]
1 to 10 range". That's it.
[no audio]
See here, simple. Simple thing is, just assume you are entering
number as 12. So previously whenever if we enter number 12
you are checking 10 conditions,
after that you are verifying 12 is not out of, 12
is out of range number.
See here guys, previously if number is 12,
first of all you're checking all these conditions,
then you are deciding number is out of range.
But now what I am doing is first step itself I am trying
to check whether the given number is there in the list of
10 numbers or not.
If it is there, then I will try to write a logic here to convert
your number into word.
In case if this is 'False', means your number is not in the
given range of values.
That's why 'else' block is going to execute.
It's very simple no. Only with two steps
you are deciding whether a number is valid number or not.
But in previous, you are deciding a number is not valid number
after checking 10 conditions.
So unnecessarily you're going to waste your execution time.
Right. But here you are saving that. Let me do that.
I mean, let me execute your code. 'python number_to_word'.
I'm entering number 45. You're getting, "your number out of range.
Please select number between 1 to 10 range". Right. Now
I am entering suppose 3
[no audio]
Guys, did we save this?
I'm not sure. Let me save it.
Yeah. Now let me give 45 number. "out of range number.
Please select 1 to 10 range.
Now, I am giving a valid number, suppose between 1 to 10 range
number, 4. "converting your number into word". But you have
to write logic there to convert your number into four. If it
is a valid number already
we have a logic to convert that, right. Already
we have a logic to convert that. So what I have to do is now,
now, instead of 'print' statement
inside of your 'if' condition I have to write one more 'if' condition,
because directly if number is a valid number directly
I can't say that it is a 1, it is a 2, it is a 3, right. Again
what I have to do? See that. 'print("one")', but when I have to
print this "one"? 'if', guys I'm writing inside 'if' one more
'if'. 'If num==1'. That's it.
So inside of that 'if' condition you have to write 'print("one")'.
So just try to execute. If you don't give space here what
will happen? You just try it, so that you can understand
why should we give space.
First of all, this is main 'if' condition. With this you are
validating whether given number is there in the given range
of values or not.
If it is in the range of values, then it's like in this way
guys. Just assume you have your house.
This is main door. Once if we enter into main door, there
are some rooms.
Again, you have to enter into this room.
So to enter into this room again you have a key.
You should have a key.
That's what we are doing here. First
I am checking whether I am in this group or not. Yes,
I have a key for my main room, home.
Yes, I'm entering. After entering based on your number, if it is 1,
you have to print "one", if it is 2, you have to print "two". Right.
That's why what I am doing is 'elif num==2:'
then 'print("two")'. Likewise you just complete up to 10. I'm
not writing up to 10, you can write it.
Right. So if you want I can do it.
So likewise, you have to write up to 10 guys.
You just try to practice in that way. That's it.
[no audio]
Right. Let me write it. Here also I have to provide,
I have to give one important point.
Let me write it.
[no audio]
Sorry.
So guys while writing your Python scripts indentation or
providing space is very, very important.
[no audio]
It's a six.
[no audio]
Okay. Let me write it,
[no audio]
'elif num==8:
'print("eight")'.
'elif num==9'
[no audio]
'print("nine")'. Then I have to write once again, right, your 'elif',
'if num==10:' 'print("ten")'.
Yes, this is perfectly correct.
It will work definitely.
Okay. Let me go and run it.
I'm entering number called 8. You are getting output
"eight". If I enter any number out of range you are getting, "out
of range number, please select 1 to 10 range".
That is fine. Now this script, compared to all your previous
scripts this is somewhat enhanced script.
But guys if you observe when you are entering into your first
'if' condition, assume that in your first 'if' condition this is a room.
[no audio]
So you are entering into this room if your number
is there in this list of values.
Right. Let's say you are entering number called 10.
[no audio]
Yes, 10 is there in the list, that's why you are able to enter
into this room. After entering into this room,
if number is 10 you have to print "ten".
That's why this is 'if-elif' condition. Your python will check first
'if' condition. Right. Python will check first
'if' condition. You are providing number as 10.
'10==1'? No, 'False'.
That's why python will come and check this one, 'num==2'.
No, 'False'. Then likewise,
assume 'num==9'.
No, 'False'. Then come here, 'num==10'
Yes, you are printing.
So guys, if all these conditions are 'False' up to 9, then definitely
your remaining number is 10 only, right?
Because first of all you're able to enter into this if your
number is within this range, 1 to 10, and if your first nine
conditions are 'False' then by default the last number is, if
first nine conditions are 'False', then definitely you can
say number is 10. Then no need to check this last condition.
Of course, if you write in this way that is perfectly correct,
but what I am saying, if all these nine conditions are false,
then definitely your number is 10, there is no doubt in that.
That's why what I am doing, I am not checking.
I'm not checking. Simply,
I'm writing 'else' block, that is good actually.
So, you know, when 'else' will execute. If all above conditions are
'False' then only it will execute. We know that if all are 'False'
then definitely number is 10.
That's why I am printing 10. That's it.
[no audio]
Right. See that. It will work perfectly.
Let me enter number 10.
Yes. Let me enter number some 23,
or some 4, it's working. Right. Fine.
Still I have very, very
[no audio]
useful way of writing for this requirement.
Still I have one more enhanced way to write this script. Maybe
you may not get that idea as of now, but once if you see whenever
if you get that type of situations, definitely you will get this
idea. So what is that? Right. See what is that. Let me comment
this code first. See now, as of now to display your number 1 to
10 range, how many lines you are writing?
[no audio]
so from 27 to almost,
let me delete empty lines, 50. That means almost 27 lines
you're writing to display simply your number, numbers from
1 to 10 range.
But what I am doing is, once again I'm going to comment this code guys.
[no audio]
See that. What I am doing? First of all, before going to read
your number or maybe after reading your number, no problem,
the first thing is, you should read some number from your
input. Yes, suppose assume that I am reading
my number. After that,
I am, after that
I am defining my dictionary guys, my own dictionary.
So what is the dictionary, how your dictionary will be there,
we know that. A dictionary will have key-value pair representation.
So now I am defining my dictionary in
this way. Our number to word conversion dictionary, 'num_word=',
dictionary should be always with curly braces, and with
key-value pair representations.
So I am taking key as number 1, and its value as "o-n-e", "one"
So that is one key-value pair representation.
Let me take second one.
Then third one.
[no audio]
Guys, if you go through this, I mean this logic, current logic,
then you will feel thrilled,
[no audio]
because it's very nice one and it's very useful one.
Just observe this.
Let me first create my dictionary with 1 to 10 as keys
and values as words 'one' to 'ten', right?
That's what I am writing here.
'six', then let me write 'seven'.
So guys, this concept is very, very useful in your real-time as well.
So while writing our real-time scripts you will come to know
the importance of defining your dictionary with key-value
pair representations.
[no audio]
One second. The last one is ten, right. Yeah.
[no audio]
Right. Fine. Now assume that you are, by running the script
what will happen? Because of 54th line
you're reading a number, you will read some number.
First of all, I'm validating whether number is a valid number or not.
'if num in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:', 10
[no audio]
then I can say, 'print("valid number")'.
First of all, let me write "valid number".
If it is not in the given range,
then I can say "please enter", "your number is", otherwise, "your
number is out of range, please select
1 to 10 range" That's it.
So guys here previously to convert your number into word,
you're writing some 'if-elif-elif' condition, but now
I am not writing that because I have a dictionary. In my dictionary,
I have a key and value pair representation.
Now, what I am doing is simply I am printing my dictionary,
my dictionary, 'num_word.get'
[no audio]
Get your key.
Whatever the number you are providing, that number.
I am placing here.
Now, before running this script
let me select this one, and I am going to Python terminal
there I will show you first, so that you can easily understand.
This is my dictionary, guys. 'num_word.
get', I am providing.
Right. I am providing
1. See the result. Or I am taking 2. See the result.
Now, whatever the input you are providing
that is a number, right?
So now what I am doing is, based on your input I am selecting
the key-value from my dictionary.
So you may enter 1, if you enter 1 you get 'one'.
What is the 1 value? 'one', 'one, that
we are displaying. It's very simple one, right?
Let me save it, and run it, and see the magic.
First of all, let me open your script. See the last lines.
See only we have one, two, three, four, five, six lines.
Instead of 27 lines
now we have only six lines.
Let me remove some empty lines here.
[no audio]
Let me save it and let me reopen your script here.
Right. So this is your script guys.
Now let me run it first,
your 'number_to_word'.
I'm entering suppose 4. You're getting output "four", no. See
what will happen? Whenever if you enter number as 4, that
4 is going to store into your 'num' variable, and you are checking.
If anyway, this is just defining a variable value, right, dictionary.
Then after that your Python will come and execute this one,
'num', 4. 4 in this range?
Yes. If it is in this range, what we are doing? 'print(num_word)',
from your dictionary a key, key->1, with that key what is the value?
That's what we are printing. Now, no need to take 'if-else'
condition also here.
Right. So guys, try to understand this simple magic.
It's very useful in your real time.
[no audio]
Okay. Okay.
Okay guys, thank you for watching this video.
[no audio]