In this post, we will begin to explore the fundamental idea of storing information in a Python program. As a matter of fact, the ideas here can be translated to other languages as well.
We will keep it as practical and non-academic as possible, not too much theory at one time, in order to not make it boring.
Purpose
The purpose is to become familiar of basic building blocks of programming and Python. Storing information is where it all begins in a computer program, you must start here and understand how to make the best use out of them.
Goal
By the end of this post you can
- create variables and understand why they are necessary
- use variables of different types: primitive single-value variables, tuple and list
- start to work on the training app we introduce, and which we’ll continue to improve over the course
Prerequisites
You need to have Python installed and be able to start IDLE to follow along. You should be familiar with how to create modules/script and run a Python script, either from the terminal or the GUI window.
A good starting point is this link: How To Install Python On Your Computer
Python Variables – how to store information
Store variables is necessary because a program need to keep track of information while the program is running. The logic in a program is based on logic and information. Since the amount of information is usually more than what the programmer can keep track of in short-term memory, we use variables.
Everyone starts to learn about variables when they write their first computer program.
On a basic level, just remember this:
- We use variables as storage in our programs
- Variables makes the program easier to read
- Variables off-load the programmer’s short-term memory
- Variables can contain primitive values like numbers or more complex structures, e.g. like lists of numbers
Code Block to copy
In this section you’re expected to copy and run the code. The purpose is to become familiar with the process. Start IDLE or your environment of choice and copy the code block below.
Run the program If you didn’t encounter any errors, congratulations! You stored your first piece of information in a computer program and should see three lines of output.
name = "Karl" direction = "North" cost = 200 print(name) print(direction) print(cost)
The three lines in the output correspond to the three variables: name, direction, cost. These are three different variables, each storing a different type of information.
When you write something within quotation marks, it’s called a “string”, as in the examples “Karl” or “North”. It’s just a piece of text basically, but it’s important to know the correct terminology.
The variable cost, on the other hand, contains a number (integer).
You must remember the following: a variable has a value and a data type.
So far, the data types we’ve seen are string and integer, other types we will encounter later on.
Project – Introducing the Workout App
In order to enhance learning, we will introduce the project for managing workout programs.
Here we will lay the foundation for the program by reusing our knowledge of variables in a real project.
For now, we will focus on tracking which exercises to perform, how many times you’ll train per week, and similar tasks.
Here’s how you can start building the foundation for a real app to create a workout schedule, we will introduce the tuple now.
Run the program
Copy the code into IDLE in a new script and run the program. Print the variable exercises by using the print() function, as we did above.
week = 1 schedule = 'Workout A' exercises = ('Bench press', 'Deadlift', 'Squat', 'Biceps curl')
The only new construct here is the line containing the variable exercises. The right-hand side is a data structure called a tuple. The tuple contains four elements, one for each exercise name. We are still only using variables, pretty cool huh?!
Question: how many exercises should I have? Answer: there’s no limit to how many elements you can have, except for your computer’s capacity, of course.
Now this is enough to consider it a small program, it is similar to a “post-it” note, however since this is programming, we can extend this in a more dynamic fashion as we move along, and we’ll see how quickly this will be useful.
Let’s agree to still call this our minimal program for a Workout App – name it whatever you’d like! Our schedule for week 1 is simple and it’s a program that hold three variables: week, schedule, exercises
Exercises
- Add a variable
frequencythat indicates how many times per week you train. Remember, you only have four exercises, a valid value could be something between 1–4—you decide! - Add new line containing the
inputfunction, see code block below. This allows the user to enter a response to the program. Run the program, enter a response, and press Enter. What do you see? - Add a new question to the user, again use
inputwith the question: Which exercise do you want to know more about?. Print the result (the value) from that input. You can use the code block below, remember to change the content between the quotation marks only.
Use this code block where the exercise asks for it
response = input('Do you want to see all the exercises in the workout plan?') print(response)
When the program runs, it will pause and wait for the user’s input, i.e. press a key. If you don’t press a key, the program could wait indefinitely. So try pressing Enter and see the program finish. In the code, we’ve added a variable response to the left of the input expression.
A note on return values
week = 1 response = input('Do you want to see all the exercises in the workout plan?')
If we compare these two right-hand sides, `1` is called a constant value, and something we as the programmer have assigned to the variable week.
What we need to understand is that input will generate a value, technically it’s the return value of the function. While it doesn’t appear as a number in the code, it will guarantee a value will be be stored in a variable response.
The return value of input will be stored in the variable response. Remember this!
Since we cannot see the value, it’s up to the programmer to either further examine the value or leave it as is. As part of this learning, we decide to print the value.
For now and for a good while ahead, it’s enough to understand and assume that what stands to the right of the equal sign must be an expression that results in a value.
A note about functions in programming
You’ve used input and print as part of Python. Both are called a built-in Python function. Their purpose is to handle communication between the user and the program, they help make the program interactive.
Functions are packaged solutions to common problems for a programmer – as matter of fact, they exist in all languages.
Relying on built-in Python functions can take you a long way, and later we will write our own functions too.
Imagine a carpenter cutting a piece of wood, that could be done with a common tool such as a saw (function). An electric saw would be even better, both are examples of functionality the carpenter can use to help him perform his task. Both are better than trying to cut the piece with bare hands only.
Likewise, the reason print and input are built-in functions is that it’s complex enough for the programmer to handle how a computer “communicates” with a user under-the-hood, and it would be a waste of time to program that logic yourself. Functions hide complex logic in a simple format so that the programmer can have a more enjoyable and easier life.
This principle is called abstraction and something we will explore later.
The complete program code
week = 1 schedule = 'Workout A' exercises = ('Bench press', 'Deadlift', 'Squat', 'Biceps curl') response = input('Do you want to see all the exercises in the workout plan? (Yes/No)') print(response) # Prints the user’s response
The program flow is
- The program asks the user if it wants to view all the exercises of the workout plan
- The user answers the question
- The program stores the response (return value) from the function in the variable
response.
Reflections
When we build a program, it’s normal to think about the relevant questions needed for the program to function the way you want. This will be a recurring theme in your job as a programmer, it’s important to have an idea and work towards that. It’s important to think this way early to avoid building the “wrong” program, or rather to build the program that suits the user as best as possible.
Remember, we haven’t been precise about what a workout plan should look like, so you must decide how the program should be used as well. This is called requirements analysis in industry language. Translating requirements into code is a major part of writing good programs. The best way to learn is to do it with a real project in mind, that’s the motivation we start with examples early on.
Terminology
Review these terms
- string
- functions
- abstraction
- tuple
- variable
- return value
Summary
In this section, we’ve gone through concepts that can be used to build many different programs. You’ve practiced Python’s built-in functions and saved information in a program.
We have also gone through a simplified process for how a program can be developed. We started with an introductory text and expanded it in several steps by asking the user a question. We also printed the user’s response. This is how programming begins—different parts evolve at different pace.
It’s not easy to do everything at once, so it’s good to think of this process as normal. If someone wants to do everything at once, avoid these personalities at work (hah).
In the next section, we’ll see how to control the flow of a program in different ways and use more Python functions to read text files.
Do you have any ideas right now of how you want to develop your program? After the user enters a response, you can, for example, make the program more user-friendly and ask for details about an exercise.
See you in the next section!