input() function in Python !
5K views
Feb 6, 2024
"Dive into the interactive world of Python programming with our tutorial on the 'input() function in Python!' Uncover the versatility of the input() function as we guide you through its usage in capturing user input. Whether you're a coding novice or looking to enhance your Python skills, this video provides a hands-on exploration of how to utilize the input() function to create dynamic and engaging programs. Learn the fundamentals of user interaction, handle input data, and add a personalized touch to your Python projects. Join us for a step-by-step walkthrough, empowering you to harness the full potential of the input() function in your coding endeavors!"
View Video Transcript
0:00
Have you ever thought of inputting something during runtime of a program in Python
0:12
Do you want to know how to use it, how to work with it
0:17
How to create a program where you enter your desired data? Well, that's a question that I'm going to answer today
0:26
So stay tuned till end. Hello everyone in this video we're going to speak about input function that what is an input function in Python and how we can work with it right
0:39
so as it name denotes that this function is going to input something right so of course you're exactly right this function is basically going to input something anything but keep in mind that
0:58
during the runtime it's basically inputting from the user during the runtime of a program
1:07
right keep that term in mind during runtime so well that's clear an input
1:15
function is actually inputting or getting values extracting values from the user
1:22
now the question is how can I use an input function right so let's do the entire method so simply type in input and then empty
1:35
parenthesis right so that's an input function if I run this code a program
1:43
will be in a waiting state it will be waiting for me until I enter anything
1:50
any failure that's up to me so let's run this program and see what's going to
1:57
happen let me run it and now as you can see the program is definitely waiting
2:05
for me I have to enter something then the program will be terminated so if I
2:10
enter something like hello right the program successfully terminated well it's quite beautiful right so now here's another point that I want to rise
2:25
it's also taking an optional argument well what is then are what is that
2:31
argument you can actually a I would say you can actually give a message to the
2:38
user that what he has to type in in this input function so suppose I say enter name So if I run this program I will have this message during the runtime and it will also be waiting for me to enter my name or anything So this is just a simple message You can write anything in close in double quotations here So let run this program and see the result So as you can get to say
3:11
see down below here guys it's it's a very for me like I have to enter something
3:16
because I've used input function right and there's also a message it's
3:23
enter name so this argument actually can be helpful when you have so many
3:32
inputs in a program it's good like you have the message you know already it's
3:38
it's it's you know it's guiding you right it's guiding you you what to do so enter names so suppose my name is John that's it the program
3:47
terminated successfully now here's in another point that I would like to rise
3:53
that what is that being considered when I entered my name here I mean what is the
4:03
data type of this object is it a character yeah for sure not a character
4:08
because a character data type is basically a single character so is that a string
4:16
could that be an integer a floating value like let me show you if I enter two
4:25
here or five what is that five is that an integer or a string let me clarify you
4:35
an input function whatever it gets whatever the user enters is always and always considered as a string
4:47
that's a string actually do you want me to prove it so let's prove it well I would say
4:54
here I would type in enter number so I'll have to enter a simple number that can be
5:03
anything and then along with that I want to add something to it
5:08
right so we can we can store this input whatever I enter to the program in a
5:17
variable so suppose x equals to input and then you can print out that value
5:25
so print x and then also add to it a number suppose 10 right so if I run this program let see the result so I being prompted as enter numbers so suppose
5:42
six see I got an error why because that X which is referring to input and
5:53
that input is extracting a string from the user So we cannot add a string and an integer
6:02
The 10 is integer here and the x is basically a string because it's coming down from the input function
6:11
So that's why I get a type error. Can only concatenate STR, not int to STR
6:18
Well, that means you cannot add up a string to an integer
6:23
So in that case, we can do type costing. So what is type costing? Well, type costing is basically changing, modifying the data type of objects
6:37
You know, like an integer is an object. What if I change its data type explicitly to a string
6:46
So when that's considered as a string, then we can add up two strings
6:52
But how? Let me show you. If I change this one, this one. This
6:57
10 to a string then I can add x and then 10 because that 10 would be considered as a string as well
7:05
let me show you to convert an object to string we simply type STR and close that value in
7:17
parentheses so let's run this code again enter number so suppose 5
7:26
I get 510. It's basically a 5, a 5 which is coming down from the input function and that is a string and then 10 which was initially an integer but I did the type costing and I converted it into a string
7:48
So that both of them were considered a string so that's why first I got 5 and then 10. So these both are strings and are being
7:56
some doubt So that how you work with it Now here another question I want to enter an integer I want to enter an integer and then add some value to it So suppose I want to add 20 to an integer enter number So I get the error right because the X will be considered as a string which is coming down from the input and 20 is by itself an
8:28
integer so in that case what can I do I can type cost the entire input
8:36
function by typing int and then closing the entire input function inside of int parenthesis
8:48
So that would convert the input whatever I provide to the calling program
8:54
during the execution during the runtime. That would be converted into an integer
9:01
And when I print that integer and also add 20 to it
9:06
so I'll get the, I'll get the, this, there's some. So let's see the program
9:12
Let me run the code, rank code. So enter number. So suppose 10, I entered 10, I got 30. Why? When I entered 10 here, that was converted, which was initially a string that was converted into an integer through in function. And then, I mean, I did the typecasting. And then I simply printed out that value, which was 10 here. And also, I did add 20 to it. So that's why I got
9:42
got 30 that said guys that's how we work with type costing and I would say input
9:49
function so keep in mind that we almost every programmer has to use input
9:55
functions because that's that's very common I mean very important you have to
10:01
get values get different sort of data from the user so then we make use of
10:08
input function and always keep in mind that you must assign this input function to a variable because if you don't do that then you won't be able to work with it later on on your program
10:21
Like if I don't assign this input function to x so how can I I mean work with it letter on how can I add some values to it how can I manipulate it well
10:33
Almost impossible so that was all about input function hopefully you guys enjoyed this video so in the next video we will discuss
10:42
relational operators, right? So stay tuned for that video as well. If you enjoyed this video, give it a thumbs up and subscribe for more. We're going to cover all the basics of Python. So keep in touch. I will see you in another one
#Computer Education
#Intelligent Personal Assistants
#Other
#Programming