0:00
hey guys welcome back to this channel if
0:02
you're new then welcome to this channel
0:03
well I'm back with one more interesting
0:05
video and today we are going to talk
0:07
about the input function in Python
0:10
so far we have learned about variables
0:12
data types numeric expressions all cool
0:15
stuff right but up until now all our
0:19
code just sat there running on its own
0:23
so which we wanted to make our code talk
0:26
to the user like actually ask for
0:28
something well today we're going to
0:30
learn exactly how to do that with the
0:33
input function in Python trust me once
0:37
you learn how to get input from the user
0:40
your programs will instantly feel
0:42
smarter and more interactive
0:45
so yeah let's jump in
0:48
so first let's start with what is input
0:51
so what is this magical input function
0:55
well input is actually a built-in
0:57
function in Python that lets your
1:00
program to pause and ask the user a
1:04
question and grab the user's response
1:08
something like this like you could see
1:11
here like for example if you take name
1:16
equal to and now I'm using input
1:30
and now when you run this
1:34
or you can just print the name
1:43
like for example if you run this here
1:48
John and now it's printing the John as
1:51
output so here if you see the program
1:55
actually stopped here at this particular
1:57
point and it is asking you for the name
2:00
whatever the name you can give whatever
2:02
the name you want and you can just print
2:08
let me rerun it once again i think I
2:10
have done something wrong when hitting
2:12
on run can just add lucky and just hit
2:16
enter so this is what
2:19
it will do so Python is actually
2:22
listening to me now exactly that's the
2:28
so let's break it down to a bit more so
2:32
in this line if you see here input enter
2:35
your name so input is actually a
2:38
function so the text inside the quotes
2:44
is the prompt like it's what the user
2:47
will see so it is what the user will see
2:51
user will not see the entire code right
2:53
you'll just see enter your name and
2:56
whatever the user types in gets stored
3:02
so the name lucky is stored inside the
3:05
variable name so then we use that value
3:09
right away in the next line by just
3:11
printing it pretty cool right
3:15
so now here is a super important detail
3:18
a lot of beginners miss like the value
3:23
you get from input is
3:28
so the value you get from input is
3:30
always always a string even if the user
3:34
types a number like 25 Python still sees
3:38
it as 25 not the number 25 so if you try
3:43
to do a math with it things will break
3:53
okay and print age and now you're adding
3:59
sorry so this should be H
4:03
now when you run this
4:08
see you got a big error you don't even
4:11
know what's going on here so Python's
4:14
going to throw a fit here like because
4:17
you are trying to add a number to a
4:19
string because this is a string and you
4:21
are adding number to a string and that's
4:26
so if you want to actually use numbers
4:29
that user gives you you need to convert
4:31
them first like I can show you here so
4:36
if you want integer as input then you
4:38
have to give int of input
4:41
if you want string then you have to you
4:43
don't have to give string because it
4:45
will take the inputs to string by
4:48
default so you don't you don't have to
4:50
worry about that like now if you try
4:54
and if you see here 25 and now it added
4:58
five to that 25 because Python now know
5:01
that it is an integer
5:03
so this is what you have to remember so
5:06
here we are actually converting the
5:08
input to an integer using int method so
5:11
Python knows we want to do math with it
5:13
so if the user types 25
5:17
Python stores it as a number 25 not the
5:20
string 25 and we can add one or you can
5:24
add even five no problem
5:27
i want you to try this yourself like
5:29
let's play with one more example like
5:32
this time it should be in float i want
5:39
and it should be the float as input and
5:42
then you have to print
5:53
1.5 or it could be 1.10 whatever the
5:56
value that you want to print so yeah I
5:59
want you to do this exercise
6:03
i want you to pause at this particular
6:04
point and I want you to do this
6:09
but I can show you how also so input
6:14
enter the price of the item
6:21
now we want this in float right so we'll
6:24
give float here first thing we have to
6:29
close this now we'll try again
6:40
then it it will add 1.5 to it and
6:43
multiply 1.5 to it and it's 3.75
6:47
actually we just created a simple
6:48
calculator that adds some percentage of
6:51
tax to whatever the number that user
6:53
enters just make sure that you're using
6:56
float here if you expect a decimal and
7:00
integer if you expect a whole number
7:06
so you can actually even combine what
7:08
you have learned so far into something
7:11
cool and simple like for example
7:17
name equal to input because it will
7:20
it'll be in string so you can add
7:23
something like what's your name
7:35
and age equal to we need this in integer
7:52
and now you can just print
7:55
hi space and now we are going to add the
7:59
string here so when adding strings you
8:02
have to give this plus operator and
8:12
i hope you are getting me don't worry
8:14
I'll explain once again in 5 years
8:38
age 25 maybe hi lucky in 5 years you'll
8:42
be 30 now we are actually using both
8:46
string and integer mixing text and
8:49
numbers so here we are mixing text and
8:52
numbers and giving a nice human response
8:55
that's the kind of code people love i
8:59
hope you got this so whenever you are
9:02
adding one like this is a string and
9:05
this is a string so when you'll add
9:07
it'll be a string again so hi name lucky
9:12
and we need this in 5 years in 5 years
9:16
comma you will be so this is a number so
9:19
you cannot add this with the strings so
9:22
if you try adding this with the strings
9:24
then it will give you error
9:28
like when you try running this so when
9:30
you're adding these then if you try
9:33
again like for example let me run this
9:42
sorry it's 25 or 26 whatever so here you
9:47
are seeing the error can only
9:50
concatenate string not integer so the
9:52
Python is not getting it it is an
9:54
integer and why are you adding it with
9:58
now it's it's a big no no again so that
10:01
is why you have to give comma here so
10:04
that Python can understand to keep
10:06
things separate even for it to manage
10:20
so this is how it will exactly work so
10:22
we have learned the input input method
10:25
which will lets your program to ask for
10:28
user input so whatever the user types is
10:31
always stored as a string and to do math
10:34
or logic you need to convert it using
10:40
and you can then use the input in any
10:43
way you like you can print it you can
10:45
multiply it combine it with other data
10:48
and totally up to you so this might seem
10:51
small but trust me once your code can
10:53
interact with people it's a total game
10:56
changer if you found this helpful go
10:59
ahead like the video and subscribe to
11:01
the channel and tap that bell icon so
11:03
you don't miss the next part where we
11:05
are going to dive into printing output
11:08
with some cool formatting tricks thanks
11:10
for watching keep coding and I'll see