can you explain this logically and properly i have attention deficit disorder - Input and Output Redirection
When you submit Python code to the judge, it runs many test cases to
determine whether your code is correct. Is someone there, dutifully
waiting for new code and then frantically hammering test cases at it
from the keyboard?
No way! It’s all automated. There’s no one typing test cases at the
keyboard. How does the judge test our code, then, if we satisfy a call to
input by typing something from the keyboard?
The truth is that input isn’t necessarily reading input from the
keyboard. It’s reading from a source of input called standard input,
which, by default, is the keyboard.
It’s possible to change standard input so that it refers to a file rather
than the keyboard. The technique is called input redirection, and it’s what
the judge uses to provide input.
We can also try input redirection ourselves. For programs whose
input is small—just a line of text or a couple of integers—input
redirection may not save us much. But for programs whose test cases
can be tens or hundreds of lines long, input redirection makes it much
easier to test our work. Rather than typing the same test case over and
over, we can store it in a file and then run our program on it as many
times as we want.
Let’s try input redirection on Telemarketers. Navigate to your
programming folder and create a new file called telemarketers_input.txt. In
that file, type the following:
8
1
1
9
The problem specifies that we should provide one integer per line,
so we’ve written them one per line here.
Save the file. Now enter python telemarketers.py < telemarketers_input.txt to
run your program using input redirection. Your program should output
ignore, just as it would if you’d typed the test case from the keyboard.
The < symbol instructs your operating system to use a file rather
than the keyboard to provide input. After the < symbol comes the name
of the file that contains the input.
To try your program on different test cases, just modify the
telemarketers_input.txt file and run your program again.
We can also change where our output goes, though we won’t need to
for this book. The print function outputs to standard output, which, by
default, is the screen. We can change standard output so that it instead
refers to a file. We do so using output redirection, which is written as a >
symbol followed by a filename.
Enter python telemarketers.py > telemarketers_output.txt to run your
program using output redirection. Provide four integers of input, and
you should be back to your operating system prompt. But you
shouldn’t see any output from your Telemarketers program! That’s
because we’ve redirected the output to file telemarketers_output.txt. If you
open telemarketers_output.txt in your text editor, you should see the output
there.
Be careful with output redirection. If you use a filename that already
exists, your old file will be overwritten! Always double-check that
you’re using the filename you intended.
Summary
In this chapter, we learned how to use if statements to direct what our
programs do. The key ingredient of an if statement is a Boolean
expression, which is an expression with a True or False value. To build up
Boolean expressions, we use relational operators such as == and >=, and
we use Boolean operators such as and and or.
Deciding what to do based on what is True and False makes our
programs more flexible, able to adapt to the situation at hand. But our
programs are still limited to handling small amounts of input and
output—whatever we can do with individual calls to input and print. In
the next chapter, we’ll start learning about loops, which let us repeat
code so that we can process as much input and output as we like.
Want to work with 100 values? How about 1,000? And with just a
small amount of Python code? It is a little early for me to be provoking
you, I know, because you still have the following exercises to do. But
when you’re ready, read on