# Variables Expressions and Statements

9/5/2020

knitr::opts_chunk\$set(error = TRUE)

# Notes on PY4E Chapter 2

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.

# 2.1 Values and Types

Read the text section then proceed.

# Types?

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:

• str
• int
• float

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.

# Check an str value for type.

type("245")
## <class 'str'>
type("Hello Julius")
## <class 'str'>

# Check an integer value for type.

type(124)
## <class 'int'>
type(-756)
## <class 'int'>

What happens if an integer begins with zero?

# type(09)
# Try this in Cocalc. I can\'t do it here since it will stop my show 

# Comma’s

Is 1,000,000 a valid integer?

# Try the following line in Cocalc
# type(1,000,000)

If I let it run here, it stops the show. Note that python does allow you to break up integers with "_" instead of commas.

type(1_000_000)
## <class 'int'>

# Check a float value for type

type(12.45)
## <class 'float'>
type(-1.5)
## <class 'float'>
type(.32)
## <class 'float'>
type(0.32)
## <class 'float'>

# Section 2.2 Variables

Read Section 2.2 in the text.

Copy the definitions of variable and assignment statement from the text to your notebook".

# Exercise

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.

Tom = 1
Dick = 1.5
Harry = "2"

What is the type of Tom?

type(Tom)
## <class 'int'>

What is the type of Dick?

type(Dick)
## <class 'float'>

What is the type of Harry?

type(Harry)
## <class 'str'>

What is the type of tom?

# type(tom)
# Of course I can't 

Run this in Cocalc. What happens? Why

# Changing the type

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:

a_string = "25"
print(type(a_string))
## <class 'str'>
print(a_string)
## 25
print()
an_int = int(a_string)
print(type(an_int))
## <class 'int'>
print(an_int)
## 25

# What happens if the string is not a valid integer?

a_string = "25."
print(type(a_string))
# an_int = int(a_string) Run in Cocalc
## <class 'str'>

# 2.3 Variable Names and Keywords

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 Cocalc try several different ways to break these and observe how python reacts.

Here are some obvious bad names.

# 2.4 Statements

Write a command statement.

print(3.4)
## [1] 3.4

Now write an assignment statement.

x = 3.4

Note that an assignment does not cause python to display the result.

# 2.5 Operators and Operands

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.

print(5/2) # Divide as floats
## 2.5
print(5//2) # Divide as integers
## 2

# 2.6 Expressions

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?

# 2.7 Order of Operations

Read the section. Then predict the values of the following expressions.

• 2 + 3 * 5
• (2 + 3) * 5
• 2 / 3 / 5
• 2 / ( 3 / 5)
• 2 * 3 / 5
• -2**2

print(2 + 3 * 5)
## 17
print((2 + 3) * 5)
## 25
print(2 / 3 / 5)
## 0.13333333333333333
print(2 / ( 3 / 5))
## 3.3333333333333335
print(2 * 3 / 5)
## 1.2
print(-2 ** 2)
## -4

# Exercise

Of the following three python expressions, which two are equal?

• 2 ** 3 ** 5
• (2 ** 3) ** 5
• 2 ** (3 ** 5)

no_parens = 2 ** 3 ** 5
left_parens = (2 ** 3) ** 5
right_parens = 2 ** (3 ** 5)

print(no_parens)
## 14134776518227074636666380005943348126619871175004951664972849610340958208
print(left_parens)
## 32768
print(right_parens)
## 14134776518227074636666380005943348126619871175004951664972849610340958208

Hmmmm???

# 2.8 Modulus Operator

• 5 % 2
• 13 % 5
• 4 % 2
• 0 % 2
• -5 % 2

print(5 % 2)
## 1
print(13 % 5)
## 3
print(4 % 2)
## 0
print(0 % 2)
## 0
print(-5 % 2)
## 1

# 2.9 String Operations

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.

"Hello" + "Joe"
## 'HelloJoe'
"Hello" +", " + "Joe"
## 'Hello, Joe'
3 * ("Hello" +", " + "Joe " )
## 'Hello, Joe Hello, Joe Hello, Joe '
"A " + 2 * "Merry " + "Christmas " + "To You"
## 'A Merry Merry Christmas To You'

# 2.10 The input() function.

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.

a_string = "25."
print(type(a_string))
## <class 'str'>
an_int = int(float(a_string))

print(an_int)
## 25
print(type(an_int))
## <class 'int'>

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.

2.12 Choosing Mnemonic Variable Names

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:

• dateOfBirth
• DateOfBirth
• date_of_birth.

I want you to follow the third one:

• All lower case
• Words separated by underscores.

To expand on this, read a bit about PEP 8 at https://realpython.com/python-pep8/