Input and Output in Python

An overview on variables and data types

Girolamo Pinto
7 min readMar 17, 2022
Photo by Fotis Fotopoulos on Unsplash

Welcome back, fellow developers.

Today, in this article, I want to talk about input and output in Python, the main functions in Python to get some data and to print them on the screen.

As always I want just to remember that this guides are intended to be gradually more complex and if you want to start from the beginning (maybe because you have no experience in Python or in computer programming at all), you can check the list the I’ve created about Python and all the related topics.

Ok let’s jump into the article and let’s discover more about variables and data types.

Variables

In computer programming, a variable is an abstract storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value; or in simpler terms, a variable is a container for a particular set of bits or type of data (like integer, float, String etc…)

Data types

Variables come in all shapes and sizes. Some are used to store numbers, some are used to store text and some are used for much more complicated types of data.

The data types to know are:

  • String (aka string): Used for a combination of any characters that appear on a keyboard, such as letters, numbers and symbols. It could be also just one character.
  • Integer (aka int): Used for whole numbers.
  • Float (aka float): Used for numbers that contain decimal points, or for fractions.
  • Boolean (aka bool): Used where data is restricted to True/False or yes/no options.

Note : if you’re interested in the boolean logic you can check this website.

Declare a variable and assigning it a value

In computer programming, when someone say “declare a variable”, he means that you should create that particular variable.

In order to do that in Python, you should just type the name of the variable and you should assign it a value, in this way:

As you can see declaration of a variable is really easy:

name of the variable = value of the variable

The = sign it doesn’t mean it’s equal to, but assign the value on the right to the variable on the left.

Note : the # sign that you see on the side it’s a coment, I’ll talk about it in the next section.

In many programming languages you should declare the data type of variables before they can be used, for example:

  • Visual Basic — dim score as int
  • Java — int score;

But in Python, you can simply start using the variable without declaring it.

Naming a variable

A variable to be so should have a name. This particular string will store the data written on the right of the = sign. In the example above we had:

This example means that to the variable called name it’s assigned a string value "Girolamo". The name given to each variable is up to the programmer, but ideally a variable name should have meaning, ie it should reflect the value that it is holding.

There are some rules about variable names:

  • Consistency: name is not the same as Name or NAME.
  • Spacing: variable names should not have a space in them. Use underscores or camelCase instead, for example name_of_client or clientName. A variable name can also start with an underscore, for example _name or _name_of_the_client_.
  • Digits: variable names should not start with a digit, but they can have digits between charcters.

Consider these example variable names, all of which could be variable names to store the length of a side of a square:

╔══════════════════╦═══════════════════════════════════╗
Variable nameComment
╠══════════════════╬═══════════════════════════════════╣
║ l ║ A poor choice – it has no meaning ║
║ length ║ Okay but a bit vague ║
║ side_length ║ Good ║
║ sideLength ║ Good ║
║ side length ║ Wrong – don’t use spaces ║
╚══════════════════╩═══════════════════════════════════╝

Comments

In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters.

For example, if I create a variable with a strange name, maybe I want to explain to whoever read the code, what the hell is that variable or to describe the behaviour of a function.

In Python to start a comment section, you should just type “#”, everything after “#” sign will be a comment. And the interpreter won’t compute it.

Input and output

Now let’s try to use variables to input something typing it with keyboard and then let’s try to print what we wrote on the screen.

Output with the print() function

To print something on the screen we will use a function that we’ve seen in previous guides: the print() function.

The print() function it’s very powerful and in this particular case can print on the screen what we’ll give as input to Python.

To use the print() function you should just write print() (obviously) and inside the parentheses you put the text that should appear on the screen between single or double quotation marks.

Note: The last print that I wrote will lead to an Error.

Input with the input() function

In Python, we use input() function to take input from the user. Whatever you enter as input, the input function converts it into a string.

Note: If you enter an integer value still input() function convert it into a string.

For example:

As you can see I used the type() function, which allows to programmer to know which type is a variable. The type() function on x variable said what we expected: str which means string. But as I said before the input() function transforms everything in a string, so the y variable, the variable assigned to the age which should be an integer, becomes a string.

Casting

If we consider the example above, maybe we want to convert the y` variable in integer, in this way we shouldn’t treat it like a string variable. In our help, to solve this problem, it comes the casting.

Casting is when you convert a variable value from one type to another. That is, in Python, done with functions like int() or float() or str(). Let’s try it with the example above.

We did it! 🎉🎉🎉

Let’s code

Okay, now we put everything we learnt in action and we try to use all the basic functions to ask to our user (ourselves for now) to provide some informations and to print them on the screen.

What we’ll do

To achieve this part let’s write down what we’ll do in order to accomplish our desire.

  1. We will ask the name of the user
  2. We will ask the age of the user
  3. We will ask if he\she has interest in Python
  4. And we will display an easy message depending on the answer to the above request

Just the last thing. In the last four lines of command I used a casting on the answer variable, trasforming it in a boolean variable. And then I used an if statement to check if the answer was True and if so I asked to the interpreter to print a message. In every other cases the message wouldn’t appeared. Like in this case:

As you can see I didn’t receive any output message because the if statement will be executed only if the answer is equal to false.

Don’t worry if you still don’t understand something it’s totally normal. The next topic will be the if statement.

See ya!!

References

--

--

Girolamo Pinto

Computer Engineer student. Python developer. C# and Unity addicted. Music, food and Formula 1 lover.