4.4. Sending data to Python#
Interactive page
This is an interactive book page. Press the launch button at the top right side.
So far we have seen examples of output, i.e., Python telling us things like in the “Hello world” example. We have also seen examples of code, i.e., us giving instructions to Python in order for it to do things.
In addition, we can also send data to Python. Often in science, we do this by having Python read data files (more on this in a later chapter), but we can also send information to Python using input()
:
a = input()
You can specify text for the label of the input box to clarify what kind of input you expect from the user:
a = input("Enter a number:")
Even if we type a number into the input box, it will always return a variable of type string str
. If we want to subsequently use our input as a number, we have to explicitly convert it to a number (remember: which property of Python causes this?). For example, we can use the float()
function:
a = input()
a = float(a)
print("The value of a is:", a)
print("a has the type:", type(a))
Because input
is buggy in the current version of the Jupyter book, try out this code in your local installation of VS Code.
Use the input
function to get parameters of integer, float, and string types into the program. Do this in VS Code.