2. Print, Values, Variables, Types
Activity
What is computation? What is a computer? What is programming?
2.1. What Is A Program/Algorithm?
Activity
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
Quick Activity
Have you seen any Python errors yet?
What were they?
Did you understand them?
2.3. Languages
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?
Activity
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.”
2.4. Terminology
Hardware/Software
Input/Output (I/O)
Processors
Storage — Hard Disk Drive/Solid State Drive
Memory and Random Access Memory (RAM)
Binary
Compiling
Interpreter vs Integrated Development Environment (IDE)
2.5. Print
We already made use of
print
in the previous topic
Activity
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
Strings:
"abcdefg"
,"Hello World"
Integers:
7
,42
,97
Floating-point numbers:
3.792
,0.000000000005
These values are called literals
Like,
1
is literally1
Notice how I described the type of each value along with the value itself
Strings
Integers
Float
To a computer, the integer
1
is not necessarily the same thing as the floating point number1.0
or the string"1"
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
type
functionprint(type(12))
would print out<class 'int'>
print(type("Hello, World"))
would print out<class 'str'>
print(type(3.75))
would print out<class 'float'>
print(type(type(1.1)))
would print out<class 'type'>
Notice that we are using two functions in the above examples
print
type
Activity
Write a single line program to print out the integer
1
.Now write a single line program to print out the string
"1"
.Can you tell the difference by looking at the output?
2.7. Variables
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
a
now has the value5
Both the variable
a
and the literal5
have the same valueIf I say
print(5)
, Python will print out the literal5
If I say
print(a)
, Python will print out the value stored in the variablea
, which is5
Warning
The =
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)
Both
print
function calls will print out11
The first one adds the literals
5
and6
The second one adds the variables
a
andb
Activity
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
good:
density
less good:
d
bad:
definitely_not_density
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 = 55
will not work sincedef
is a reserved word
Two important conventions we will follow
Use lowercase letters
Separate words in the variable name with underscores (snake case)
some_bill
2.7.3. Constants
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
SALES_TAX
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