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

Deleted f22/andy_lec_notes/lec03_Sep12/lec03_operators_template.ipynb

parent 75429255
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
# Warmup: calculate number of seconds in a year
```
%% Cell type:markdown id: tags:
## January 31: Operators
Learning Objectives: After this lecture you will be able to...
- Recognize examples of different Python data types: int, float, str, bool
- Explain the different behavior of the /, //, and % operators
- Evaluate numeric expressions containing mathematical and comparison operators
- Evaluate numeric expressions containing comparison operators
- Determine the correct order of operations in Python
- Evaluate string expressions containing operators and escape characters
- Evaluate boolean expressions that contain the operators and, or, not
%% Cell type:markdown id: tags:
### Command Line: Running code from Terminal/PowerShell
interactive mode (>>> )<br>
![command%20line%20interactive-3.png](attachment:command%20line%20interactive-3.png)
%% Cell type:markdown id: tags:
### Script mode
when you enter python and the name of a program
python test.py
%% Cell type:markdown id: tags:
### Jupyter Notebooks: a mix between interactive and script<br>
### If many lines of code are entered, the last line is displayed
%% Cell type:code id: tags:
``` python
# enter in 3 different calculations such as 17 + 3 * 2
```
%% Cell type:markdown id: tags:
### A print statement will always display its result
%% Cell type:code id: tags:
``` python
# enter the same 3 calculations, but with one or more inside a print statement
```
%% Output
0.6
31
1.4789915966386555
%% Cell type:markdown id: tags:
### Python has 4 basic types of values: float, str, int, bool
%% Cell type:code id: tags:
``` python
print(type(4.5))
print(type("hello"))
print(type(2))
print(type(True))
```
%% Output
<class 'float'>
<class 'str'>
<class 'int'>
<class 'bool'>
%% Cell type:markdown id: tags:
### Integers are represented with type int, Other numbers with type float
%% Cell type:code id: tags:
``` python
2 * 4.0
```
%% Output
8.0
%% Cell type:markdown id: tags:
### There are 3 operators that relate to division in Python: /, //, and %
%% Cell type:code id: tags:
``` python
# The / operator does Division as a float. Try a few examples
```
%% Cell type:code id: tags:
``` python
# The // operator 'floor division' always rounds down. Try a few examples
# int // int
print()
# float // float
print()
# float // int
print()
# int // float
print()
```
%% Cell type:code id: tags:
``` python
# Sometimes / will display small numbers in scientific notation
# print out calcualtions that result in very small numbers
print(34 / 1000000)
```
%% Output
3.4e-05
%% Cell type:code id: tags:
``` python
# the modulus operator (%) gives the Integer Remainder
print( 15 % 7)
# try other examples
print( )
print( )
```
%% Output
1
%% Cell type:code id: tags:
``` python
# you can use modulus along with 'floor division' to grab a certain digit of an integer
# get the last digit of an integer by using % 10
print( )
# get the 2nd last digit of an integer by using // 10 % 10
print(6834 // 10 % 10)
```
%% Output
3
%% Cell type:code id: tags:
``` python
# the wrong way to do exponentiation is ^
2 ^ 5
```
%% Output
7
%% Cell type:code id: tags:
``` python
# the correct way to do exponentiation is **
```
%% Output
32
%% Cell type:code id: tags:
``` python
# fractional exponents are like nth roots
3 ** (1/2)
```
%% Output
1.7320508075688772
%% Cell type:code id: tags:
``` python
# curious fact: repeated exponentiation is done from right to left
2 ** 9 ** (1/2)
```
%% Output
8.0
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Order of Precedence....this will be on your quiz
%% Cell type:markdown id: tags:
![precedence.png](attachment:precedence.png)
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
# What operation is higher? ** or - ?
# ** is higher
- 2 ** 4
```
%% Output
-16
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Strings
Strings are the *text* of Python. They are enclosed in `''` or `""` (but don't mix and match them!).
%% Cell type:code id: tags:
``` python
print('hello')
print("hello")
# error
```
%% Output
hello
hello
%% Cell type:markdown id: tags:
A string can contain special escape characters that start with \
- `\n` is for a newline character
- `\t` is for a tab character
- `\'` is for a literal single quotation mark
- `\"` is for a literal double quotation mark
- `\\` is for a literal slash
%% Cell type:code id: tags:
``` python
# TODO Write code to print the following messages!
# Hey "Carl"!
# Hello World \till next time
# With regards,
# Alice (notice the tab!)
```
%% Output
he
ll o
%% Cell type:code id: tags:
``` python
# the + operator concatenates two strings
"hello" + "There"
```
%% Output
'helloThere'
%% Cell type:code id: tags:
``` python
# the * operator repeats a string
"ha " * 10
```
%% Output
'ha ha ha ha ha ha ha ha ha ha '
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Boolean Operators
%% Cell type:markdown id: tags:
![boolean%20operators.png](attachment:boolean%20operators.png)
%% Cell type:code id: tags:
``` python
# try and, or, not with True and False values
print(True and False)
print( )
```
%% Output
False
True
%% Cell type:code id: tags:
``` python
# try comparison operators
print(3 < 7)
print( )
```
%% Output
True
True
%% Cell type:code id: tags:
``` python
# try to compare two strings with < or > or >= or <=
"a" > "A"
```
%% Output
True
%% Cell type:code id: tags:
``` python
# practice using != (not equal) and == (is equal)
print(45 != 40 + 1)
print(45 == 40 + 1 )
```
%% Output
True
False
%% Cell type:code id: tags:
``` python
# compare two types for equality
```
%% Cell type:code id: tags:
``` python
# compound comparisons with 'and'
print (4<7 and 19 > 2)
```
%% Output
True
%% Cell type:code id: tags:
``` python
# compound comparisons with 'or'
print (4<7 or 19 > 2)
```
%% Output
True
%% Cell type:markdown id: tags:
### Review:
Go back to the Learning Objectives at the beginning of this notebook and tell yourself one fact about each.
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
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