Often when talking to, or about, programming you’ll hear the term Input, Processing, and Output. It’s kind of the big three in programming, and how we often work. We get some input, from a file, a user, etc… then we process that information, or data if you prefer, and then we provide some output either to the screen, a file, web service, etc.
Some computer programmers use this Input Process Output as a type of chart to track what goes into a program, how it is handled, and then what is the output.
However, we handle this on multiple levels, not even when we talk about a program itself. For example, we can look at a problem (get input), design a solution (process), and write a program (output). We can further look at the output, and turn that into input. For example, in finding syntax errors – until we fix them. Once we can build our software, we check for logic errors, until we are comfortable with the answer the computer gives us.
Syntax Errors
Syntax errors are errors in how our code is written. For example, misspelling a word, leaving out a required symbol, or failing to initialize a variable before we use it. A compiler or interpreter will give us an error and not allow the program to run if it encounters a Syntax Error.
Logic Errors
A Logic Error will run as instructed. However, the instructions are incorrect. This can be a minor typo, that is legal, but incorrect. And example might be accidentally using a > (great than) instead of a < (less than). Or it might be using the wrong formula to test an application.
Often logic errors are referred to as bugs. This is because of some computer history in which a moth flew into a computer and shorted out a section of hardware. Vice Adm. Grace Harper coined the term debugging because of this whenever they had to find errors in the computer system.
Displaying Data in Python
Let’s look at some simple ways to begin working with Python.
Open a Python console window. Inside the window type:
print('Hello World!')
When you pressed the Enter/Return key, it should have printed Hello World! on the screen for you.
Every time a computer language is taught, there is an unwritten rule that you must start with “Hello World”. There are several reasons for this, let’s look at some things we can see.
First, we have a command which exports out data to the screen print. This command is function which performs this service.
Next, we used a literal string. A literal string is a collection of characters between a set of single, or double quotes in Python. You can use either one, however, you cannot easily contain that type of quote inside the string. Try some combinations and see if it generates a syntax error for you or not.
print('Hello. You're my friend.')
print("Hello. You're my friend.")
print('"Hello," she said.')
print(""Hello," she said.")
print(""""Hello," she said.""")
Which of these gave an error, vs. which ran? Did you see how Python showed you the syntax errors?
Comments allow you to write information that is for you, or other programmers. They are very helpful when you have large programs. In python, comments start with a hash sign and then the rest of the line is ignored.
# This is a comment, nothing after the # is valid.
Comments are more helpful if you are running a python file. From a console window you would type py <filename>.py to run a python file.
Variables
Variables store information for use later. You can store data in them, like a number, or string of text, and then recall them at a later time. This could be the next line, or much later in your program.
Python has rules about how you name a variable.
- Python is case sensitive. That mean the variable foo, is different from Foo.
- A variable name cannot be a python reserved word.
- A variable name must start with a letter, or an underscore.
- Cannot have spaces in the name
- Can only be made up of letters, numbers, and underscores ( _ )
To assign a value, you give the name the variable, then an equal sign, and then the value.
temp = 98.6
print(temp) #notice the lack of quotes
Variables can change values over the course of the program. Hence the name variable. But they can even change between types (numeric and string).
Getting Input
You can get input from the console window using the input command. The general form is variable = input(<prompt>). In actually it looks like:
todaysTemp = input('Current temperature?')
print('It is currently: ', todaysTemp)
Notice that this short two line program does several new things. First, it gets the input from the user. Second it prints out a literal string, and the variable, from the same command, by separating them with the comma. Finally, as part of the second, you’ll notice that you are using the data that you stored in your variable.
Input, Processing, and Output was originally found on Access 2 Learn