Discover more from Brian Knapp’s Newsletter
[Zero To Python] Day 5 - Data Structures, Variables, and User Input
Time to get interactive!
Today is a big step forward in your understanding of programming in Python. This lesson is all about two big programming concepts - data structures and variables.
A data structure is pretty much what it sounds like. It is a way to structure your data. Structuring your data is useful in programming. It allows you to create larger and more useful programs. Also data structures allow you to turn boring data into useful information.
An address is a classic example of a data structure we all know and understand. An address (in the USA) has a number and street name, possibly apartment number or PO Box, city, state, and zip code. All of that data could exist separately and not describe a specific physical location correctly. Once you combine all that information together it becomes an address.
That is the power of data structures. But how do we use them in code? For that we need variables.
A variable is how we reference stored data in code. It points a the location in memory where data is stored so we can use it. Variables are used to store and retrieve data.
Variables can be named anything you want. The name you choose for a variable has nothing to do with the data stored inside. It is a good idea to name variables based around the data it holds, but that is more for programmer comprehension than anything else.
You can have variables store different types of data. Python mostly handles this for you as a programmer, so I’m not going to go deeper on that topic right now.
It’s important for you to understand that most of programming is about storing and retrieving data in various ways. Programmers use variables everywhere in code. It’s one of the most foundational tools you will use.
Take the time to learn and practice using variables in the next few lessons. It will make all the future lessons much easier.
Hello World With Variables
Alright, it’s code time! Let’s start by re-making our hello world program, but using a variable to store the text instead of printing it directly. In a new file called
input.py type the following code:
If you run the program you get the standard output same as before. But, how we got there is different.
hello = "hello world" is a variable assignment. The code assigns the text string
"hello world" to the variable
hello. Python uses that code instruction to store that value in memory somewhere.
In the next line we write
print(hello). That is the same print function as before, but we pass in a variable instead of a text string. The print function will look at the memory location pointed to by the variable to get the
"hello world" text string value we assigned to the
Reusing A Variable
A variable can be used over and over again. This is quite useful. Here is how it’s done.
Here you can see that we call
print(hello) more than once and in-between we call
Getting User Input
Variables are great, but on their own they are limited. Software is supposed to be interactive. Writing out every variable value in code is tedious and not interactive.
Interactive software demands user input. So, here is how we get user input in Python:
If you run that program and type in
steak as your favorite food you get this output:
What the code is doing is in this line
food = input("what is your favorite food? ") it assigns the result of the
input function to the variable
food. The Python
input function will output a text string and then wait for the user to input some text. When the user types something in and hits enter on the keyboard, that value is passed along from the
input function. In the case of this code, that means the result of the
input function is assigned to the variable
As I mentioned previously, variables can be named anything. If we change the variable name
bicycle as in this example:
The program does the exact same thing but the user input is stored in a variable named
bicycle instead of
Inputting The Journal Date
Next we are going to change our program to fit more into our daily journal program.
As you can see we changed the text on the input prompt and the variable name is now `journal_date`. With those two small changes the entire intent and user experience is different. Instead of the program being about your favorite food it’s asking you what day it is. Otherwise, the code is the same.
But, that’s still not exactly what we had in our original journal screen designs. So let’s tweak the code a bit more to this:
Which gives us this output:
That is very close to our faked screen experience, which is a good stopping point for today. We will be building on that in upcoming lessons, so consider this a sneak peak of how we will be using variables, data structures, and user input going forward.
There are two major parts to today’s assignment.
First, spend time using variables. Assign some values by hand and others use the
input function to get data from the user. Try different things and explore both what I showed you and maybe some ideas of your own.
Second, once you are comfortable with using variables create the final example of getting the journal date user input that matches the faked screen.
You Will See This Again
Everything in this lesson you will see and use over and over again. So, take the time to get the code under your fingers. You will have more opportunities to use variables in upcoming lessons. These concepts are foundational to your success, so learn them well.
That said, focus on progress. Get a little better each day. You can do this!