Harold Nelson
5/11/2022
## Error in reticulate::use_python(python = "/Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8", : Specified version of python '/Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8' does not exist.
These notes follow chapter 2 of the text closely. They are intended to be used in a hands-on manner, not just read. You should read each section of the text before working through the corresponding section of these notes. Make sure that you have python running to experiment. It would be best to create a Jupyter notebook to run code and make your own notes.
Read the text section then proceed.
What are the three types described in this chapter and how are they distinguished? Use the type() function to examine a constant value of each type.
The three types are:
A constant of type str is enclosed in quotes, either single or double.
A constant of type int contains numbers but no decimal point. It must not contain commas.
A constant of type float contains numbers and a decimal point.
What happens if an integer begins with zero?
Is 1,000,000 a valid integer?
If I let it run here, it stops the show. Note that python does allow you to break up integers with “_” instead of commas.
## <class 'int'>
## <class 'float'>
## <class 'float'>
## <class 'float'>
## <class 'float'>
Read Section 2.2 in the text.
Copy the definitions of variable and assignment statement from the text to your notebook.
Use assignment statements to assign values of each of our current types to named variables. Use any names you want. Then use the type function to investigate each.
What is the type of Tom?
## <class 'int'>
What is the type of Dick?
## <class 'float'>
What is the type of Harry?
## <class 'str'>
What is the type of tom?
Run this in jupyter What happens? Why
Sometimes you need an int value, but the data is in a str. The function int() will accept a string and return an int if the string is a valid int value.
Example:
## <class 'str'>
## 25
## <class 'int'>
## 25
## <class 'str'>
A legal python variable name contains letters, numbers and underscores. It must not begin with a number. It must not be one of the reserved words listed in this section.
Exercise: In jupyter try several different ways to break these and observe how python reacts.
Here are some obvious bad names.
Read the section
Write a command statement.
The usual binary operators are mostly as in Excel, with the exception of “^”. In python, exponentiation is represented by “**“.
There is a special version of division for integers.
Exercise: Compute 4/3 and 4//3.
Read the definition of expression in this section and copy it to your notebook.
This section makes a distinction between the use of a python expression with the interpreter as opposed to within a script. How does an expression work in a cell in Cocalc?
Read the section. Then predict the values of the following expressions.
## 17
## 25
## 0.13333333333333333
## 3.3333333333333335
## 1.2
## -4
Of the following three python expressions, which two are equal?
## 14134776518227074636666380005943348126619871175004951664972849610340958208
## 32768
## 14134776518227074636666380005943348126619871175004951664972849610340958208
Hmmmm???
Read this section then do the following computations and check your answers in Cocalc.
The operators + and * are meaningful for strings.
The symbol + denotes concatenation, placing together.
The symbol * between an integer and a string repeats the string the specified number of times.
Consider the following examples.
## 'HelloJoe'
## 'Hello, Joe'
## 'Hello, Joe Hello, Joe Hello, Joe '
## 'A Merry Merry Christmas To You'
This function prints its argument and returns whatever is type by the user in response.
This function always returns a string.
Experiment with the example provided in PY4E using Cocalc.
What happens if the user inputs a float?
It will fail. See the notes on input() above. Try it in Cocalc to reinforce the point.
How can you get from a string containing a float to a valid integer.
## <class 'str'>
## 25
## <class 'int'>
One point I want to emphasize beyond the usefulness of such variable names is the style of creating multi-word names.
You need to be consistent to avoid constantly checking your spelling. Consider date of birth.
There are multiple conventions:
I want you to follow the third one:
To expand on this, read a bit about PEP 8 at https://realpython.com/python-pep8/
2.11 Comments
Most instructors emphasize the need for comments. I go beyond that and push for “literate programming.” When you use Jupyter notebooks, it’s natural to think of yourself as writing a document containing bits of code.
https://www.youtube.com/watch?v=HW29067qVWk is worth watching to get the idea. It also gives you a lot of practical details on using a Jupyter notebook.