0% found this document useful (0 votes)
17 views2 pages

2.6 - Standard Input and Output - mp4

The document discusses basic input and output in Python. It covers the print function for output and formatting strings using format. It also covers the input function for getting user input and assigning it to a variable.

Uploaded by

NAKKA PUNEETH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

2.6 - Standard Input and Output - mp4

The document discusses basic input and output in Python. It covers the print function for output and formatting strings using format. It also covers the input function for getting user input and assigning it to a variable.

Uploaded by

NAKKA PUNEETH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Now we'll learn how to do basic input and output in Python.

Of course, we have been seeing


some basic printf commands for a while. Sorry, basic print commands for a while, but let me
just go over it very quickly. So if you just say print with your parentheses, and whatever you
say here will be printed. As you can easily understand, if there is a variable a, which is
assigned a value of ten, and if you say print, the value of a is comma separated by a, it'll print
you ten. Or the other way to do it is you can just say, since this is a string, this is a string,
right? You can use the plus sign, the plus operator, and convert your a into a string. This
converts your a into a string, and then you can concatenate this string with this new string,
or you can join these two strings. Concatenation is nothing but simply joining one string
after the other string, right? Even this works actually in earlier versions of, in earlier
versions of Python. So what we're using right now is Python three. So what used to do is if I
just said print a without any parentheses, it used to work. It used to work very well in
previous versions of Python, like Python 2.6 and 2.7, it no more works. So if you type
something like this and it's not working, probably you should just go to the top right of your
ipython notebook and check out what you're running. So we are using Python three here. As
it's clearly explicitly mentioned here. If you're using some other version of Python, like 2.6
or something, then a command like this might just simply work. But from Python three
onwards, you need to place your small brackets or parentheses around your print
statement. So that's one major difference between Python two and Python three. And since
some of me, people like me who have been used to Python for a long time, keep doing these
simple mistakes because we're used to a syntax like this. Having said that, now let's see how
to format our output, because this is very important. This is like your printf, right? In printf,
what do we do? We can say what we want and we can just give the parameters that we want
to print here. So what is the equivalent of printf? Right? So let's assume I have two variables.
A equals to ten and b equals to 20. Remember, this is a one line assignment. We just saw
assignments a while ago, right? So I'm assigning ten to a and 20 to b, and in a single line I'm
doing two statements, right? So having said, having assigned these two values. Now let's
assume I want to print the value of a is with some value here, value of b is this. So if I just
put curly braces here and I can say, so this is a string, right? This is a string, and string has a
function called format. Format, I could just say by doing this, what happens is my a goes
here and my b goes here, right? So what you get is the value of a is ten, the value of, and b is
20, as you would expect. So format is the important function here that we use for
formatting. Output seems so natural, right? The next thing is, let's do it slightly differently. If
I want to say the value of b is so here, if you notice, as compared to here, here, I've just left
my curly braces empty. Here I'm giving values here. Here I'm giving a value of one, I'm
giving a value of zero. So whatever is inside your format, the parameters of your format, this
has an index of zero and this has an index of one. So since I am saying the value of b is and
within braces, I'm putting one here. So the value that is there in b, which is 20, gets placed
here, and the value of a, which is ten, as per this statement, gets put here, right? So it's not
necessary that I always need to have my variables that I pass to this function in the same
order. I can just give it indexes. The third very interesting way, which is fun to use, is
something like this. Let me just show you, okay, so here I'm saying print hello. And in my
curly braces, I'm not giving an index like this, I'm not giving zero or one. I'm actually putting
a name. So I'm putting a variable name called name here. And again, this is another
parameter. So in my Format string, I can actually give things like this. I can say name equals
to satish. So then what happens is satish gets placed here and my greeting equals to good
morning. And this gets placed here. You can think of them as key values, right? I mean
bluntly thinking of it. So this is the key that I'm using, and satish is the value and the value
gets placed at the key, right? So this is a very nice and very useful form of formatting strings,
which is fairly unique to python, I think. There are very few languages which let you do this.
Now the other thing here is we can combine various types of arguments. This is again
another fun part. So if I say the story of within braces, I put zero, what does zero mean here?
Zero means bill because that's the first parameter, right. Comma curly braces, one. What is
the first parameter here it is Manfred, right? And I'm giving a string here. I'm giving a
keyword argument, right. So other here equals Greg. So Greg will be put here so I can mix
and match these various options that I have here while formatting my output. Now let's look
at input. Okay, we have understood how to format our output. We have understood about
simple print command. Here is a fun part. I think I'll have to run this. So let me quickly run
this. And what you get here is a box. It says enter a number. Suppose if I say 1234 and enter,
okay, voila, it just prints my number. So that's what the code is, right. So the input function,
sorry, the input function in Python gives you the text box that we just saw where you can
enter some value and assign that value that we entered to a variable called num, to any
variable. Basically, in this case we were assigned it to num. This is the basics of a very basic
input output in Python.

You might also like