2. Print, Values, Variables, Types
What is computation? What is a computer? What is programming?
2.1. What Is A Program/Algorithm?
Explain to a partner how you would go about making breakfast in the morning.
There, that’s an algorithm
It is a series of instructions that can be followed to achieve something
What kind of computer was executing this program?
2.2. What Is Debugging?
Most of the time your code will be wrong
This is true for both new and experienced programmers
Debugging is the process of addressing the bugs in your code
Realistically, expect to debug a lot
Remember, you will be wrong every time you run your program before you get it right
The point is, you will be wrong a lot more than you will be right — get used to this
Have you seen any Python errors yet?
What were they?
Did you understand them?
What is a natural language?
What is a formal language?
Why is ambiguity so important to natural language?
Why is ambiguity deadly for a formal language?
Do you think there is a limit to what I can describe with a formal language?
Can I describe anything? Any computation?
HINT: Is the following statement true or false: “This statement is false.”
Storage — Hard Disk Drive/Solid State Drive
Memory and Random Access Memory (RAM)
Interpreter vs Integrated Development Environment (IDE)
We already made use of
Write a (single-line) Python program that prints a witty message of your choice.
Print is a function that allows us to print out information to the screen
Print might end up being your best friend
Get used to writing it
2.6. Values And Types
Values are things that a program manipulates
These values are called literals
Notice how I described the type of each value along with the value itself
To a computer, the integer
1is not necessarily the same thing as the floating point number
1.0or the string
Some of the errors you will make will be a result from mixing types incorrectly
Some languages (e.g., C, Java) are strict about types
You have to be totally explicit about them
Python is a little more relaxed
Python will guess what the type is
Upside: less to worry about and less clutter in your code
Downside: more likely to introduce errors caused by mixing types
You can check the type of something in Python by using the
print(type(12))would print out
print(type("Hello, World"))would print out
print(type(3.75))would print out
print(type(type(1.1)))would print out
Notice that we are using two functions in the above examples
Write a single line program to print out the integer
Now write a single line program to print out the string
Can you tell the difference by looking at the output?
Variables let you store values in a labeled (named) location
You store values into variables by using the assignment operator —
1a = 5 2m = "Some String"
In the above example, the variable
anow has the value
Both the variable
aand the literal
5both have the same value
If I say
print(5), Python will print out the literal
If I say
print(a), Python will print out the value stored in the variable
a, which is
= in Python has a very different meaning from what you are familiar with in math. In math, when one writes
\(a = 5\), it means that \(a\) and \(5\) are equivalent as they exist — it is stating a fact.
In Python, and many other programming languages, it is not a statement about equality, but an assignment. In Python,
if one writes
a = 5, it means that the variable
a is now storing the value
5 within it.
2.7.1. Using Variables
You can use variables in the same way you use literals
1print(5 + 6) 2 3a = 5 4b = 6 5print(a + b)
The first one adds the literals
The second one adds the variables
Assign various values of types string, integer and float to variables.
Try adding variables of the same type. What happens?
Try adding variables of different types. What happens?
Try the assignment
5 = a. What happens?
Figure out how to display the current contents of a variable.
2.7.2. Naming Variables
You can use whatever you want within a few restrictions set by the language
Python wants variable names that begin with a letter of the alphabet and limits what non-alphanumeric characters you can use
A good choice is a variable name that is descriptive of what the variable is meant to contain
There are a few other important restrictions that you may come across
For example, you cannot use reserved words (words that already have a specific meaning in Python)
def = 55will not work since
defis a reserved word
Two important conventions we will follow
Use lowercase letters
Separate words in the variable name with underscores (snake case)
In Python, constants are just variables that we as programmers use in a special way
Imagine you are writing a program where you’re doing a lot of calculations with sales tax
1some_bill = 10.45 * 1.15 2another_bill = 4.99 * 1.15
This is clearly correct, however
What if someone else looks at this code and wonders what 1.15 is?
What if the gov changes the sales tax in the future?
Although there is nothing wrong with the above code, one could do the following instead
1SALES_TAX = 1.15 2some_bill = 10.45 * SALES_TAX 3another_bill = 4.99 * SALES_TAX
Now, just by looking at those lines of code, I know exactly what we are multiplying the numbers with
If the sales tax rate is ever lowered, all I need to do is change the one line of code (
SALES_TAX = 1.15)
The naming convention for constants is all uppercase letters separate with underscores
The idea behind the constants are that once the value is set by you, they are not to change
You can change them in the code, but the code should not alter the value of
In Python, there is nothing stopping you from changing the value other than the convention
In some languages, the language actually prevents the program from altering the value of a constant
2.8. For Next Class
Read the rest of Chapter 2 of the text