Zhiguang Huo (Caleb)
Monday Oct 24th, 2022
## Hello World!
## Hello, Lucas!
def inc(x):
"""Increase by one
Keyword arguments:
x -- the base number to be increased
"""
return x + 1
inc(3)
## 4
## 'Increase by one\n \n Keyword arguments:\n x -- the base number to be increased\n '
?inc
help(inc)
## 120
## 120
## 5
## 8
## 4
## 8
## 3
## 9
## 1
## 45
## Help on built-in function sum in module builtins:
##
## sum(iterable, /, start=0)
## Return the sum of a 'start' value (default: 0) plus an iterable of numbers
##
## When the iterable is empty, return the start value.
## This function is intended specifically for use with numeric values and may
## reject non-numeric types.
?sum ## this way only works for Jupyter Notebook
def hello(name):
print('Hello ' + name + '!')
def myadd(a, b):
return(a + b)
## Hello Lucas!
## 11
Student1 = {
"name": "Amy",
"age": "23",
"country": "Canada"
}
## {'name': 'Amy', 'age': '23', 'country': 'Canada'}
## name: Amy
## age: 23
## country: Canada
After import module, all the objects in the module are available via module.object
## 1.7320508075688772
## 10
## 11
## 4.0
## 4.0
## 3.141592653589793
## 3.141592653589793
## 3.141592653589793
## 1.6514961294723187
## Current working directory is /Users/zhuo/Dropbox (UFL)/teaching/2022FALL/lectures/Week10_functions/functions
## ['.Rhistory', 'functions.py', 'functions.rmd', 'my_file.txt', 'new_file.txt', 'functions.html', '__pycache__', 'foo', 'functions', '.ipynb_checkpoints', 'mymod.py', 'tmp']
## '/Users/zhuo/Dropbox (UFL)/teaching/2022FALL/lectures/Week10_functions'
## '/Users/zhuo/Dropbox (UFL)/teaching/2022FALL/lectures/Week10_functions/functions'
os.mkdir("tmp") ## create a single folder
os.makedirs("foo/bar") ## create a recursive folder
os.remove(afile) ## remove afile
os.rmdir(afolder) ## remove a folder
file = 'Untitled.ipynb'
# File location
location = "/Users/zhuo/Desktop/" ## change this to your own directory
os.listdir(location)
# Path
## ['.Rhistory', 's41592-021-01252-x.pdf', '.DS_Store', '.localized', 'sellingPoints.txt', 'shiny-examples', 'others', 'circadianWebsite', 'reference.pdf', 'env', 'bbaa257.pdf', 'test2022', 'update_20220401.txt']
## /Users/zhuo/Desktop/Untitled.ipynb
## True
## False
## False
## True
## 0.5228940589963078
## 0.5714025946899135
## 0.5714025946899135
## 2
## 2
## 'cherry'
## ['apple', 'banana']
## ['apple', 'cherry', 'banana']
## 7
## 5
## 3
def parrot(voltage, state='a stiff', action='voom'):
print("-- This parrot wouldn't", action, end=' ') ## end will switch from a new line to a whitespace
print("if you put", voltage, "volts through it.")
print("-- It's", state, "!")
## -- This parrot wouldn't voom if you put 1000 volts through it.
## -- It's a stiff !
## -- This parrot wouldn't voom if you put 1000 volts through it.
## -- It's a stiff !
## -- This parrot wouldn't VOOOOOM if you put 1000000 volts through it.
## -- It's a stiff !
additional matching rules
def parrot(voltage, state='a stiff', action='voom'):
print("-- This parrot wouldn't", action, end=' ') ## end will switch from a new line to a whitespace
print("if you put", voltage, "volts through it.")
print("-- It's", state, "!")
## -- This parrot wouldn't VOOOOOM if you put 1000000 volts through it.
## -- It's a stiff !
## -- This parrot wouldn't jump if you put a million volts through it.
## -- It's bereft of life !
## -- This parrot wouldn't voom if you put a thousand volts through it.
## -- It's pushing up the daisies !
a motivting example
two input
def add(a,b):
return(a + b)
def add(a,b,c,d):
return(a + b + c + d)
## ('Testing',)
## (1, 2, 3)
## Args:
## (1, 2, 3)
## Nothing:
## ()
add(1,2,3,4,5) # 15
add(1,2,3,4,5,6,7,8,9,10) # 55
def add(*args):
print(type(args)) ## the input args forms a tuple
sum = 0
for n in args:
sum += n
return sum
add(1,2,3,4,5) # 15
## <class 'tuple'>
## 15
## <class 'tuple'>
## 55
## {'x': 1, 'y': 2, 'z': 3}
def print_keywords_4(**keywords):
for kw in keywords:
print(kw, ":", keywords[kw])
print_keywords_4(x=1,y=2,z=3)
## x : 1
## y : 2
## z : 3
def print_params_4(x,y,z=3,*pospar, **keypar):
print(x,y,z)
print(pospar)
print(keypar)
print_params_4(x=1,y=2)
## 1 2 3
## ()
## {}
## 1 2 3
## (5, 6, 7)
## {'foo': 1}
assert expression[, assertion_message]
number = 1 ## try -1 as well
assert number > 0
assert number > 0, f"number greater than 0 expected, got: {number}"
## 15
## 15
## 15
## 30
## 30
## 300
## 300
## 200
## 300
## 50
## 60
## (9, 27)
## 25
## 125
## <zip object at 0x110958980>
## [('a', 1), ('b', 2), ('c', 3)]
## a1
## b2
## c3
## a0
## b1
## c2
## [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
## [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
## [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
## [0, 2, 4, 6, 8]
## ['A', 'B', 'C']
names = ["Amy", "Beth", "Carl", "Dan", "Emily", "Frank"]
import random
students_scores = {name: random.randint(0, 100) for name in names}
students_scores
## {'Amy': 83, 'Beth': 20, 'Carl': 4, 'Dan': 66, 'Emily': 62, 'Frank': 41}
python -m pip install numpy
>>> import numpy as np ## test if the package has been installed in python
python3 -m pip install numpy==1.23 ## specifying a package version
python3.9 -m pip install --upgrade numpy ## upgrade a package
pip install numpy --user ## if you are not a admin
py -3 -m pip install numpy
>>> import numpy as np ## test if the package has been installed in python
py -3 -m pip install numpy==1.23 ## specifying a package version
py -3 -m pip install --upgrade numpy ## upgrade a package
py -3 -m pip install numpy --user ## if you are not a admin
conda install numpy
conda install numpy=1.16
conda list --name numpy conda ## check current installed version
conda update numpy
conda create -n myenv
## conda create -n myenv python=3.9 ## create an environment with a specific Python version.
conda activate myenv ## activate the environment
conda info --envs ## list all environment
conda install -n myenv scipy=0.17.3
python ## open python with this environment
conda deactivate