Python Note 5
Python Note 5
when
In Python, a comment is a piece of text that begins with a # (hash) sign and
extends to the end of the line.
If you want a comment that spans several lines, you have to put a hash in
front of them all.
This is often done during the testing of a program, in order to isolate the place
where an error might be hidden.
TIP
2. If you want to place a comment that spans several lines, you need to
place # in front of them all. Moreover, you can use a comment to mark a
piece of code that is not needed at the moment (see the last line of the
snippet below), e.g.:
5. Comments can be important when you are reading your own code after
some time (trust us, developers do forget what their own code does), and
when others are reading your code (can help them understand what your
programs do and how they do it more quickly).
The function is named input() . The name of the function says everything.
The input() function is able to read data entered by the user and to return
the same data to the running program.
The program can manipulate the data, making the code truly interactive.
Virtually all programs read and process data. A program which doesn't get
a user's input is a deaf program.
print("Tell me anything...")
anything = input()
print("Hmm...", anything, "... Really?")
The program prompts the user to input some data from the
console (most likely using a keyboard, although it is also possible to
input data using voice or image);
the input() function is invoked without arguments (this is the simplest
way of using the function); the function will switch the console to
input mode; you'll see a blinking cursor, and you'll be able to input
some keystrokes, finishing off by hitting the Enter key; all the inputted
data will be sent to your program through the function's result;
note: you need to assign the result to a variable; this is crucial -
missing out this step will cause the entered data to be lost;
then we use the print() function to output the data we get, with some
additional remarks.
The input() function can do something else: it can prompt the user without
any help from print() .
Note:
This variant of the input() invocation simplifies the code and makes it
clearer.
Type casting
Python offers two simple functions to specify a type of data and solve this
problem - here they are: int() and float() .
Having a team consisting of the trio input() - int() - float() opens up lots of
new possibilities.
Of course, these programs will be very primitive and not very usable, as they
cannot make decisions, and consequently are not able to react differently to
different situations.
This is not really a problem, though; we'll show you how to overcome it soon.
Our next example refers to the earlier program to find the length of a
hypotenuse. Let's rewrite it and make it able to read the lengths of the legs
from the console.
Let's ignore this weakness for now. We'll come back to it soon.
Note that in the program that you can see in the editor, the hypo variable is
used for only one purpose - to save the calculated value between the
execution of the adjoining line of code.
We want to show you that they have a second function. They are able to do
something more than just add and multiply.
We've seen them in action where their arguments are numbers (floats or
integers, it doesn't matter).
Now we're going to show you that they can handle strings, too, albeit in a
very specific way.
Concatenation
The + (plus) sign, when applied to two strings, becomes a concatenation
operator:
string + string
It simply concatenates (glues) two strings into one. Of course, like its
arithmetic sibling, it can be used more than once in one expression, and in
such a context it behaves according to left-sided binding.
Don't forget - if you want the + sign to be a concatenator, not an adder, you
must ensure that both its arguments are strings.
Note: using + to concatenate strings lets you construct the output in a more
precise way than with a pure print() function, even if enriched with
the end= and sep= keyword arguments.
Run the code and see if the output matches your predictions.
Replication
The * (asterisk) sign, when applied to a string and number (or a number and
string, as it remains commutative in this position) becomes a replication
operator:
string * number
number * string
It replicates the string the same number of times specified by the number.
For example:
You already know how to use the int() and float() functions to convert a
string into a number.
This type of conversion is not a one-way street. You can also convert a
number into a string, which is way easier and safer ‒ this kind of operation
is always possible.
str(number)
To be honest, it can do much more than just transform numbers into strings,
but that can wait for later.
We've modified it a bit to show you how the str() function works. Thanks to
this, we can pass the whole result to the print() function as one
string, forgetting about the commas.
You already know the basic data types, and a set of fundamental operators.
You know how to organize the output and how to get data from the user.
These are very strong foundations for Module 3. But before we move on to the
next module, let's do a few labs, and recap all that you've learned in this
section.
Key takeaways
2. The input() function comes with an optional parameter: the prompt string.
It allows you to write a message before the user input, e.g.:
3. When the input() function is called, the program's flow is stopped, the
prompt symbol keeps blinking (it prompts the user to take action when the
console is switched to input mode) until the user has entered an input and/or
pressed the Enter key.
NOTE
You can test the functionality of the input() function in its full scope locally on
your machine. For resource optimization reasons, we have limited the
maximum program execution time in Edube to a few seconds. Go to the
Sandbox, copy-paste the above snippet, run the program, and do nothing ‒
just wait a few seconds to see what happens. Your program should be stopped
automatically after a short moment. Now open IDLE, and run the same
program there ‒ can you see the difference?
4. The result of the input() function is a string. You can add strings to each
other using the concatenation ( + ) operator. Check out this code: