[Python #2] – User input and more on data types

[Python #2] – User input and more on data types

In the previous tutorial, we learned how to create variables and how to store data (strings or integers) inside of our variables.

In this tutorial, we will learn how to store data inside our variables from outside the code. With the input() syntax, we can assign data to our variable according to the user input. Sounds confusing? Then consider the below:

Our task is to instruct our code to ask the user to input his username. So far, our abilities allow us to ‘hard-code’ such data, or in other words, embed it directly into the source code, as follows:

username = "JessicaDoe123"

The above snippet of code, however, automatically assumes that we either know the user’s username or that the user has a direct access to our source code—both cases being quite inconvenient. For this reason, we need to learn the basic input function of the Python language — input().

In our Python editor, let’s try out the following code:

username = input()
print(username)

We have assigned the input() function to a variable, and then printed it into the terminal. As we can notice, however, nothing actually gets displayed in our terminal—why? The reason is that the input() function is waiting for the user’s input before it can print the value into the terminal.

The way we can fix this to make the code a little bit more intuitive, letting the user know what the code is waiting for, is to enter the parameter in between the parentheses of the input() function.

username = input("What is your username? ")
print(username)

Let’s see what changes in our code:

Animated GIF

We can now see that after the code is run, the question ‘what is your name’ automatically pops up and prompts the user to enter his username. Whatever value the user inputs (in this case ‘Adam’) is then stored within the ‘username’ variable which we printed in the example above.

To make the code even more intuitive, we can try printing a welcome message in our print function line:

This is just an example of how we can combine different variables within one function. We combined a hardcoded string variable “Good to see you back” and ‘username’ variable storing whatever the user inputs.

A bit more on data types

The two basic data types covered in the previous tutorials were str (string) and int (integer). Strings are are sequences of character data (numerics, symbols, alphabetical characters and so on) where integers are actual numbers that the computer recognises and can perform mathematical operations with them.

When storing variables, our data is allocated a part of our computer’s memory, meaning that a 10-digit would take up more memory than a single-digit number. In Python however, data management is not something we usually worry about, and any kind of number can be stored as integer. In the Java language, for example, we have four options of storing our integers:

We can see that the ‘int’ data type takes 4 bytes of memory, whereas a ‘byte’ data type takes only one byte. In Python, due to its simplicity, we are only able to assign any whole number to the ‘integer’ type. Therefore, when developing a code that engages closely with the computer’s processor and the computer’s memory, data types are extremely important. For this reason, Python is generally used for things like automation scripts or web frameworks.

Besides whole numbers, we might want to store a decimal number. In computing, such numbers are usually referred to as ‘floats’, and the data type assigned to them is, hence, called a ‘float’.

price = int(20.7)
print(price)

The above snippet of code would print out ’20’ instead of ‘20.7’ because the ‘int’ data type cannot store decimals. The code does not attempt to round the number up or down, it simply ignores the decimal number.

Much like when converting strings to integers, we can also convert strings to floats. Consider the following examples:

Price = "10.5"
PriceInteger = int(Price)
print(PriceInteger)
Price = "10.5"
PriceFloat = float(Price)
print(PriceFloat)

Whereas the second example would work to display the decimal number, the first one would throw us an error, since the code isn’t able to transform a ‘10.5’ string to an integer. Were the ‘Price’ variable an integer (simply a number without quotes), printing it subsequently would simply ignore the decimals as explained above, unless we specify it’s data type as ‘float’.

Exercise

Try to fix the following code so that the congratulations message correctly shows the score in decimals:

Leave a Reply

Your email address will not be published. Required fields are marked *