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

Update...

Update f22/andy_lec_notes/lec05_Sep16_UsingFunctions/.ipynb_checkpoints/lec_05_template-checkpoint.ipynb, f22/andy_lec_notes/lec05_Sep16_UsingFunctions/lec05_using_functions_completed.ipynb, f22/andy_lec_notes/lec05_Sep16_UsingFunctions/readme.md, f22/andy_lec_notes/lec05_Sep16_UsingFunctions/lec05_using_functions_template.ipynb
Deleted f22/andy_lec_notes/lec_05/lec05_using_functions_template.ipynb
parent 0c7417c1
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
# Review last lecture: Go over the last examples that we missed
# Review last lecture:
# answers are posted in that completed notebook file
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
# February 4: Using Functions
# Lecture 5: Using Functions
Learing Objectives:
- Call `input()`, `int()`, and `float()` to accept and process string input
- Concatenate values onto strings within a print function by converting to `str()`
- Using correct vocabulary, explain a function call
- call/invoke, argument, keyword arguments, return value
- Use named keyword arguments with the `print()` function.
- Use some common built-in functions such as `round()`, `abs()`
- Import functions from a module using `import ...`, or `from ... import ...`
- Call functions in modules using the attribute operator (`.`)
%% Cell type:markdown id: tags:
### Call input(), int(), and float() to accept and process string input
%% Cell type:code id: tags:
``` python
# the input function accepts user input and returns it as a string
```
%% Cell type:code id: tags:
``` python
# Example 0: input with a string
name = input("enter your name: ")
print(type(name))
print("hello " + name)
```
%% Output
enter your name: Andy
<class 'str'>
hello Andy
---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
<ipython-input-2-49d1986e87e4> in <module>
1 # Example 0: input with a string
----> 2 name = input("enter your name: ")
3 print(type(name))
4 print("hello " + name)
~/opt/anaconda3/lib/python3.8/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
858 "raw_input was called, but this frontend does not support input requests."
859 )
--> 860 return self._input_request(str(prompt),
861 self._parent_ident,
862 self._parent_header,
~/opt/anaconda3/lib/python3.8/site-packages/ipykernel/kernelbase.py in _input_request(self, prompt, ident, parent, password)
902 except KeyboardInterrupt:
903 # re-raise KeyboardInterrupt, to truncate traceback
--> 904 raise KeyboardInterrupt("Interrupted by user") from None
905 except Exception as e:
906 self.log.warning("Invalid Message:", exc_info=True)
KeyboardInterrupt: Interrupted by user
%% Cell type:code id: tags:
``` python
# Example 1: input with an int (wrong way)
age = input("enter your age: ")
print(type(age))
#age += 10
#print(age)
```
%% Output
enter your age: 33
<class 'str'>
-------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-25-96f3975b0b0e> in <module>
2 age = input("enter your age: ")
3 print(type(age))
----> 4 age += 10
5 print(age)
TypeError: can only concatenate str (not "int") to str
%% Cell type:code id: tags:
``` python
# the int() function converts its argument to type int
print(int(32.5))
#print(int("2012") + 10)
#print(int("nineteen"))
```
%% Output
32
2022
-------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-8-1950fff0ff50> in <module>
2 print(int(32.5))
3 print(int("2012") + 10)
----> 4 print(int("nineteen"))
ValueError: invalid literal for int() with base 10: 'nineteen'
%% Cell type:code id: tags:
``` python
# copy the code from Example 1 and fix the int adding error
```
%% Cell type:code id: tags:
``` python
# the float() function converts its argument to type float
print(float(26))
#print(float("3.14") * 2)
#print(float("three point one"))
```
%% Output
26.0
6.28
-------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-13-144286ff014b> in <module>
2 print(float(26))
3 print(float("3.14") * 2)
----> 4 print(float("three point one"))
ValueError: could not convert string to float: 'three point one'
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
# Example 2: input with a float
# to enter their day care, kids have their temp checked
temp = input("enter your temperature: ")
print(type(temp))
temp_ok = temp < 98.6
print(temp_ok)
```
%% Output
enter your temperature: 45.5
<class 'float'>
True
-------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-44c5e8abd52d> in <module>
4 temp_ok = temp < 98.6
5 print(temp_ok)
----> 6 print("OK to enter: " + temp_ok)
TypeError: can only concatenate str (not "bool") to str
%% Cell type:markdown id: tags:
### Concatenate values onto strings within a print function by converting to str()
%% Cell type:code id: tags:
``` python
# the str() function converts any expression into a str type
# this is important when putting a number together with a string
year = 2022
print("next year will be " + year + 1)
```
%% Output
-------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-55139cede869> in <module>
1 # the str() function converts any expression into a str type
2 year = 2022
----> 3 print("next year will be " + year + 1)
TypeError: can only concatenate str (not "int") to str
%% Cell type:code id: tags:
``` python
# Copy Example 2 and add this print statement
print("OK to enter: " + temp_ok)
# then fix the syntax error
```
%% Cell type:markdown id: tags:
### Definitions
- Function: A grouping of code that can be called by naming it.
- Call/Invoke: A statement in Python code that instructs the program to run all the lines of code in a function, and then come back afterward.
- Parameter: A variable, defined inside the function....When the function is called, it is assigned the argument as its value
- Argument: A value that you provide to a function by placing it inside the parentheses in the function call.
- Return value: the value that is returned by the function
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Use named keyword arguments with the print() function.
%% Cell type:code id: tags:
``` python
#Sep allows you to configure separator between the arguments
#End allows you to configure what character gets printed after
#all arguments are printed
print("hello", "world", sep = "|", end = ";\n")
#print("hello", "meena", sep = "^", end = "...\n")
#print("*" * 4, "#" * 6, sep = "||", end = "<END>")
#print("*" * 6, "#" * 8, sep = "||", end = "<END>")
#print("\n", end = "")
#print("*" * 4, "#" * 6, sep = "||", end = "<END>\n")
#print("*" * 6, "#" * 8, sep = "||", end = "<END>\n")
```
%% Output
hello|world;
%% Cell type:code id: tags:
``` python
# other example with print and keyword arguments
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Use some common built-in functions such as round(), abs()
%% Cell type:markdown id: tags:
#### https://www.w3schools.com/python/python_ref_functions.asp
%% Cell type:code id: tags:
``` python
# round function
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### Import functions from a built-in module using import, from
%% Cell type:markdown id: tags:
#### Working with Modules
Modules contain a collection of functions<br>
We need to import the module into Python<br>
%% Cell type:code id: tags:
``` python
# let's try to find the square root of 17
sqrt(17)
```
%% Output
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-19-0e804da6320c> in <module>
1 # let's try to find the square root of 17
----> 2 sqrt(17)
NameError: name 'sqrt' is not defined
%% Cell type:code id: tags:
``` python
# we need to find the module and then import it
import math
math.sqrt(17)
```
%% Output
4.123105625617661
%% Cell type:code id: tags:
``` python
print(math.cos(3.14159))
```
%% Output
-0.9999999999964793
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
# use help to list all the functions available
# help(math)
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
# here are other ways we can use import
# import just a single function
from random import randint
print(randint(1,10))
# how is from ... import .... different from import .... ?
```
%% Output
8
%% Cell type:code id: tags:
``` python
# wrong way to call randint
#print(random.randint(1,10))
```
%% Output
2
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
# use help on math module
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% 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