Variables and Basic User Interaction in Python

  • by

When learning to program, the first set of operations you want to be able to complete are inputs, outputs, and storage of data. Almost every programming problem involves taking some form of input from a user, computing logic, then outputting some sort of end result to the user. The goal of this article is to introduce the syntax for these operations, and explain exactly what happens when we run them.

To start, we will look at simple outputs. To start off, we will learn how to print text to the screen using Python. Here are a few examples of print statements in Python:

Image for post
Print statements in Python

There are a few key things to remember when printing output. print is a special function, which is designed to print text to the screen. When we put text in the brackets, we need to surround the text in quotes, so that Python understands that we are referencing text. If we were putting a number or variable in the brackets, we wouldn’t need the quotations, since the output is not text.

When we execute our program, the Python interpreter reads our code, and translates into code that our computer understands, typically referred to as machine code. Our computer then takes the machine code, and executes it as interpreted, giving us a result. In this example, the result is printing the value in the brackets to the screen for the user to see.

Now that we understand outputs, we can take a look at inputs and variables. Variables are a method of storing input from a user. We define a section of our computer’s memory to hold the value, and then we are able to reference or manipulate the value as required. We give variables a name so that they are easy to reference. To take user input, we use an input function, and store the results within a variable. Here is an example of a variable that takes an input, and outputs it to the screen.

Image for post
Inputs in Python

First, let’s take a look at the input function. The input function is similar to the print function in the sense that we provide something to be printed to the screen. The main difference is that the input function prompts the computer to wait for an input to be given before continuing. Once an input is provided, the program continues to the next line. variableForInput is the variable that holds what the user has inputted. The general flow of the program is as follows:

1. The user is prompted for an input with the text Enter a number

2. Once the user provides an input, the computer allocates space in memory, and stores the variable in that memory space.

3. Python associates the memory space where the input was stored with the name variableForInput, such that whenever we reference variableForInput, Python gets the value of the variable from where it is stored in memory

4. We then tell Python to print the value stored in variableForInput to the screen

To note, we don’t always need to take an input to have a variable, and we can modify a variables value throughout our application. For example, let’s suppose we wanted to create a program calculated the area of a circle, given the radius by a user. To code this, we would do the following:

Image for post
Calculating the area of a circle

Let’s walk through what this program does line by line to get an understanding of how it works.

1. First, we allocate some memory to store the number 3.141592653. We give it the name PI to be referenced by later

2. We next ask the user, Enter the radius of the circle. We store the result given to us in a variable called radius

3. Next, we need to turn the input into a decimal number, so that Python understands how to multiply it with PI. To do this, we set the value of radius equal to float(radius). A float is the name Python gives to decimal numbers. Doing float(radius) tells Python that the value stored in radius is a decimal number

4. Finally, we print the result of radius * radius * PI, which is the input multiplied by itself, multiplied by 3.141592653.

From this example, you can see that we can set values for variables, and do math operations on numeric variables. One question you may have is why we need to do float(radius). In Python and most other programming languages, variables are given a type in order to represent what data is stored in the variable, so the language knows how to interact with it. In Python, inputs are stored as text, or strings, as Python calls them. If we ask Python to multiple text by a number, it doesn’t know how to do this properly and will give an error. To fix this, we tell Python that radius is of type float. This way, Python knows that data stored in radius is a decimal number, and can treat it as such.

There are a number of common data types in Python, which I will briefly discuss here.

1. Integers: Represented by int, this is any integer, or non decimal number

2. Float: Represented by float, this is any decimal number (note that this includes integers, since 1 can be represented as 1.0)

3. String: Represented by str, this is any text value

4. Boolean: Represented by bool, this variable type has only two values, either True or False.

There are many other data types in Python, but these four are the most common that you will work with. All of these variables can store a single value. As you continue to learn programming, you will see more complex structures that can store multiple values. Even with just single values, you can compute a lot of powerful results. The example of calculating the area of circles is a great application, as you can now run the program anytime you want the area of a circle to get an instant result.

With an understanding of inputs, outputs, and variables, you are well on your way to learning to program. With this knowledge, you are now ready to learn decision structures, and repetition structures, which will give you the foundations to create many useful programs.

Leave a Reply

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