DEV Community

Cover image for Our Gigantic Python Adventure. Now Learning Input & Output
Sandra
Sandra

Posted on • Originally published at sandramartin.hashnode.dev

Our Gigantic Python Adventure. Now Learning Input & Output

In my first post about Python, we discovered how to set everything up to start programming. In this post, we will create together our first Hello World program, and we will learn about how to create inputs and outputs by interacting with the program user. I hope you find this post handy in your Python developing adventures!

Our first program: Hello World!

You can use either your IDLE or your code editor for this program. I will use my Visual Studio Code editor.

So, start by opening the software of your choice, and be prepared to create your very first program (also called script). It is a tradition to write a Hello World as your first program, and we won't be the ones to break that rule.

We will make Python show us the Hello World text on the screen. For this, we will use the print() function.

The print() function tells the program that it must show something on the screen. We are the ones in charge of indicating to Python what to display. It can be text, a number, etc.

To be able to print some text, we must add some double or simple quotes between that text (this will mean that it will be a string, but we'll get to that in another post).

Notice that the sentence you want to print must also be between parenthesis (many rules, I know). So, the code would be as follows (try to do it yourself too):

print("Hello World!")
Enter fullscreen mode Exit fullscreen mode

To run the script, look for the run button of your program. In the case of Visual Studio Code, you can find it here:

Run button in Visual Studio Code

You can also press F5 as a shortcut. Remember that you must save the code to run it. The result will be:

Hello world message

With this print() function, the script can also show on the screen something that the program user introduced before, the result of a calculation, or almost anything you can imagine.

What this print() function puts on the screen is known as output: the program data shown to the user.

Input - Output flux

The input-output flux consists of creating a script on which the user introduces data, and the program gives an output depending on that data.

We can create a script that will say hi to the user.

Input function

To ask for data from the user, we use the input() function. For example:

print(input("What is your name? "))
Enter fullscreen mode Exit fullscreen mode

Input code result

Notice that the whitespaces between the " " are also characters and that the program will print them on the screen. We usually use those whitespaces at the end of the question for aesthetic purposes (so the answer given is separated from the question).

It is necessary to know that what we can introduce in the input doesn't need to be a name, as we haven't stated that in the program. We can write a number or even click enter to insert nothing.

Input example

With the input itself, we can print the lines in the parenthesis. With the print, we will print on the screen the data introduced by the user. For example, by using only input():

Only using input

You can try doing this yourself to understand it better.

Storing data

But what's the use of having user input if we can only print it? Here is where storing data comes to play. We can save this data so we can use it afterwards.

We need to tell Python where to save this data, and for that, we use what are known as variables.

I will talk about this in another post, but a brief explanation would be: A variable is a space in the device memory to store data.

We create variables by giving them a name, and then we assign them a value:

age=27
Enter fullscreen mode Exit fullscreen mode

In this example, I have created the variable age and stored 27.

We can also print the value of that variable:

age=27
print(age)
Enter fullscreen mode Exit fullscreen mode

You can try this to start getting comfortable working with variables.

We can see that I haven't used " " inside print(). It is because what I'm printing is a variable.

If I use " ", Python will understand that I'll want to print some text, and if I don't use them, it will believe that I want to print the value of a variable.

You can also combine text using +. For example:

age=input("How old are you? ")
print("You are "+ age +" years old")
Enter fullscreen mode Exit fullscreen mode

Example code output

You can try recreating this program and giving your personal touch to it.

Cheatsheet

Here you can find the commands I have used in this post as a summary:

print("text")                 #Print some text
print(input("text"))          #Print some text introduced by the user
Enter fullscreen mode Exit fullscreen mode

To sum up

I believe that the first step to being able to be a programmer is to learn how to interact with the users of your program. To do that, we use both print() and input() functions.

I hope that you have found this post handy. Now you should be able to ask users for input and to print different messages and data. Happy coding, and see you in my next post!

Useful links

Here you can find a post where I explain how to set up your computer to begin writing code. Check it out!

Ready for Our First Contact With Python. How to begin

Top comments (0)