Skip to content
Snippets Groups Projects
Commit 5240655e authored by Andy Kuemmel's avatar Andy Kuemmel
Browse files

Update f22/andy_lec_notes/lec04_Sep14_VarsExprs/lec_04_Vars_and_Exprs_template.ipynb

Deleted f22/andy_lec_notes/lec04_Sep14_VarsExprs/lec04_vars_exprs_template.ipynb, f22/andy_lec_notes/lec04_Sep14_VarsExprs/lec04_vars_exprs_completed.ipynb
parent 1b4e7afd
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
# Warmup:
# Use the ** operator to calculate the square root of 17
print(17 ** (1/2))
# Use type to print out the type of the answer
print(type(17 ** (1/2)))
```
%% Output
4.123105625617661
<class 'float'>
%% Cell type:markdown id: tags:
### February 2: Expressions and Variables
Learning Objectives:
Evaluate Expressions
- by identifying operators and operands
- by identifying literal values and variables
- by identifying the correct order of operations
Write assignment statements with proper syntax
- Start with a variable name that follows proper rules for variables
- Use a single equals sign
- Finish with an expression
Define, give examples of, and identify 3 kinds of errors
- Syntax error
- Runtime error
- Semantic error
5.4 Write Python code that computes with strings ints, floats, and Boolean types
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
# Trick expression #1: What will this evaluate to:
1+1 == 2 or 3 ** 10000000 > 2 ** 20000000
```
%% Output
True
%% Cell type:code id: tags:
``` python
3 ** 10000000 > 2 ** 20000000
```
%% Output
False
%% Cell type:code id: tags:
``` python
1 + 1 == 2 or 4 / 0 == 1
```
%% Output
True
%% Cell type:code id: tags:
``` python
4 / 0 == 1 or 1 + 1 == 2
```
%% Output
-------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-9-a2e1d47f6136> in <module>
----> 1 4 / 0 == 1 or 1 + 1 == 2
ZeroDivisionError: division by zero
%% Cell type:code id: tags:
``` python
False or True
```
%% Output
True
%% Cell type:code id: tags:
``` python
# These examples might seem weird but they preview the kinds of errors
# students make in their code
```
%% Cell type:code id: tags:
``` python
False or "hi"
```
%% Output
'hi'
%% Cell type:code id: tags:
``` python
3 + 4 == 7 or 6 # Incorrect conditions
```
%% Output
True
%% Cell type:code id: tags:
``` python
# wrong way don't do this
3 + 4 == 6 or 7 # Incorrect conditions
```
%% Output
7
%% Cell type:code id: tags:
``` python
# wrong way don't do this
3 + 4 == 6 or 8 # Incorrect conditions
```
%% Output
8
%% Cell type:code id: tags:
``` python
# correct
3 + 4 == 6 or 3 + 4 == 7 #Correct conditions
```
%% Output
True
%% Cell type:code id: tags:
``` python
x = 10
print(x)
```
%% Output
10
%% Cell type:code id: tags:
``` python
x + 1
```
%% Output
11
%% Cell type:code id: tags:
``` python
x = x + 3
print(x)
```
%% Output
13
%% Cell type:code id: tags:
``` python
# shortcut for what we just did
x += 3
x
```
%% Output
44
%% Cell type:code id: tags:
``` python
# this decreases one from 1 and stores the result in x
x -= 1
x
```
%% Output
41
%% Cell type:code id: tags:
``` python
5 = x # Syntax error
```
%% Output
File "<ipython-input-33-7d72a4d0e008>", line 1
5 = x # Syntax error
^
SyntaxError: cannot assign to literal
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
# int demo
# Print out hours, minutes, and seconds
seconds = 12345
hours = seconds // 3600
print(hours)
seconds = seconds - hours*3600
#print(seconds)
minutes = seconds // 60
print(minutes)
seconds = seconds - minutes*60
print(seconds)
```
%% Output
3
25
45
%% Cell type:code id: tags:
``` python
# float demo
# Compound growth
start = 1000
interest = 7
years = 30
yearly_mult = 1 + interest / 100
final = start * yearly_mult ** years
print(final)
```
%% Output
7612.255042662042
%% Cell type:code id: tags:
``` python
# practice with the len function
# the len function finds the length of some thing, if it has a length
name = 'Sam'
print(len("Stevie"))
print(len(name))
```
%% Output
6
3
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
# str demo
# Alice vs Bob
player1_name = 'Alice'
player2_name = 'bob'
player1_score = 10
player2_score = 8
#Scores are difficult to compare
print(player1_name + ': ' + '|' * player1_score)
print(player2_name + ': ' + '|' * player2_score)
#Assuming name has a maximum of 9 characters
#Now scores are easier to read and compare
print(player1_name + ': ' + " " * (9 - len(player1_name)) + '|' * player1_score)
print(player2_name + ': ' + " " * (9 - len(player2_name)) + '|' * player2_score)
```
%% Output
Alice: ||||||||||
bob: ||||||||
Alice: ||||||||||
bob: ||||||||
%% Cell type:code id: tags:
``` python
# bool example
age = 10
valid = 0 <= age <= 100
print("you may continue: " + str(valid))
```
%% Output
you may continue: True
%% Cell type:code id: tags:
``` python
# bool example
age = 110
valid = 0 <= age <= 100
print("you may continue: " + str(valid))
```
%% Output
you may continue: False
%% Cell type:code id: tags:
``` python
# Warmup:
# Use the ** operator to calculate the square root of 17
# Use type to print out the type of the answer
```
%% Cell type:markdown id: tags:
### February 2: Expressions and Variables
Learning Objectives:
Evaluate Expressions
- by identifying operators and operands
- by identifying literal values and variables
- by identifying the correct order of operations
Write assignment statements with proper syntax
- Start with a variable name that follows proper rules for variables
- Use a single equals sign
- Finish with an expression
Define, give examples of, and identify 3 kinds of errors
- Syntax error
- Runtime error
- Semantic error
5.4 Write Python code that computes with strings ints, floats, and Boolean types
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
# Trick expression #1: What will this evaluate to:
# 1+1 == 2 or 3 ** 10000000 > 2 ** 20000000
```
%% Cell type:code id: tags:
``` python
# 3 ** 10000000 > 2 ** 20000000
```
%% Cell type:code id: tags:
``` python
# 1 + 1 == 2 or 4 / 0
```
%% Cell type:code id: tags:
``` python
# 4 / 0 or 1 + 1 == 2
```
%% Cell type:code id: tags:
``` python
# False or True
```
%% Cell type:code id: tags:
``` python
# These examples might seem weird but they preview the kinds of errors
# students make in their code
```
%% Cell type:code id: tags:
``` python
# False or "hi"
```
%% Cell type:code id: tags:
``` python
# 3 + 4 == 7 or 6 # Incorrect conditions
```
%% Cell type:code id: tags:
``` python
# 3 + 4 == 6 or 7 # Incorrect conditions
```
%% Cell type:code id: tags:
``` python
# 3 + 4 == 6 or 8 # Incorrect conditions
```
%% Cell type:code id: tags:
``` python
# 3 + 4 == 6 or 3 + 4 == 7 #Correct conditions
```
%% Cell type:code id: tags:
``` python
x = 10
print(x)
```
%% Output
10
%% Cell type:code id: tags:
``` python
x + 1
```
%% Output
11
%% Cell type:code id: tags:
``` python
x = x + 3
print(x)
```
%% Output
13
%% Cell type:code id: tags:
``` python
x += 3
```
%% Cell type:code id: tags:
``` python
x -= 1
```
%% Cell type:code id: tags:
``` python
# 5 = x # Syntax error
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
# int demo
# Print out hours, minutes, and seconds
seconds = 12345
hours = 0
print(hours)
seconds = 0
minutes = 0
print(minutes)
seconds = 0
print(seconds)
```
%% Output
0
0
0
%% Cell type:code id: tags:
``` python
# float demo
# Compound growth
start = 1000
interest = 7
years = 30
yearly_mult = 1 + interest / 100
final = 1
print(final)
```
%% Output
7612.255042662042
%% Cell type:code id: tags:
``` python
# practice with the len function
# the len function finds the length of some thing, if it has a length
name = 'Sam'
print(len("Stevie"))
print(len(name))
```
%% Output
6
3
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
# str demo
# Alice vs Bob
player1_name = 'Alice'
player2_name = 'bob'
player1_score = 10
player2_score = 8
#Scores are difficult to compare
print(player1_name + ': ' + '|' * player1_score)
print(player2_name + ': ' + '|' * player2_score)
#Assuming name has a maximum of 9 characters
#Now scores are easier to read and compare
print(player1_name + ': ' + " " * (9 - len(player1_name)) + '|' * player1_score)
print(player2_name + ': ' + " " * (9 - len(player2_name)) + '|' * player2_score)
```
%% Output
Alice: ||||||||||
bob: ||||||||
Alice: ||||||||||
bob: ||||||||
%% Cell type:code id: tags:
``` python
# bool example
age = 10
valid = 0 <= age <= 100
print("you may continue: " + str(valid))
```
%% Output
you may continue: True
%% Cell type:code id: tags:
``` python
# bool example
age = 110
valid = 0 <= age <= 100
print("you may continue: " + str(valid))
```
%% Output
you may continue: False
Source diff could not be displayed: it is too large. Options to address this: view the blob.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment