Zhiguang Huo (Caleb)
Thursday Oct 19th, 2023
## 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/2023FALL/lectures/Week10_functions/functions
## ['.Rhistory', 'functions.py', 'functions.rmd', '.DS_Store', 'functions.html', '__pycache__', '.ipynb_checkpoints', 'mymod.py', 'tmp']
## '/Users/zhuo/Dropbox (UFL)/teaching/2023FALL/lectures/Week10_functions'
## '/Users/zhuo/Dropbox (UFL)/teaching/2023FALL/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', 'fdgth-03-727504.pdf', 'Screen Shot 2023-09-28 at 8.55.01 AM.png', '~$deed.docx', 'BayesianCircadianSlides.pptx', 'GatorPKG', 'drugs and insurance.txt', '.DS_Store', 'Jupyter_note.PNG', 'PIIS0092867421003792.pdf', 'LOI.pdf', 'NFWF_Data_Management_and_Access_Plan.pdf', 'GrantApplication.pdf', '.localized', 'Untitled.ipynb', 'Gator2022', 'example', 'Screen Shot 2023-10-18 at 11.22.40 AM.png', 'Screen Shot 2023-10-02 at 3.55.26 PM.png', 'Ding_GA-Work-Schedule.pdf', 'shiny-examples', 'Screen Shot 2023-10-10 at 11.36.11 AM.png', 'others', 'circadianWebsite', 'wga.pdf', '2024_Dental_Monthly_Premium_Chart.pdf', 'This manuscript studies the potential causal relationship between sleep behavior and renal outcomes.docx', 'budget_justification_SOMOA_Huo 051823.pdf', 'Screen Shot 2023-09-21 at 1.38.12 PM.png', 'chatGPT.txt', 'budget_justification_SOMOA_Huo 051823 (1).pdf', 'YCR-2021-2022-AcademicSalaryReport.pdf', 'Auto-GPT', 'JEV template (1).pdf', 'Action Required_ Proposal SFI Certification Requested PI_ Esser.eml', 'openAI_key.txt', 'Committee_Change_Form_Dongyuan.pdf', 'Personalized glucose respoinses Naef Cell Reports 08042023.pdf', 'eamm', 'bbaa257.pdf', '.ipynb_checkpoints', 'Screen Shot 2023-10-04 at 12.50.24 PM.png', 'J American Geriatrics Society - 2018 - Rogers‐Soeder - Rest‐Activity Rhythms and Cognitive Decline in Older Men The.pdf', 'Mail - Huo,Zhiguang - Outlook_files', '2024_Dental_Plan_Comparison_(Final).pdf', 'Screen Shot 2023-10-05 at 10.23.28 AM.png', 'Screen Shot 2023-09-21 at 1.38.20 PM.png', 'Screen Shot 2023-10-10 at 11.35.17 AM.png', 'thumbnail_image.png']
## /Users/zhuo/Desktop/Untitled.ipynb
## True
## True
## True
## True
## 0.16687382761630964
## 0.5714025946899135
## 0.5714025946899135
## 2
## 2
## 'cherry'
## ['apple', 'banana']
## ['apple', 'cherry', 'banana']
## 7
## 5
## 3
def parrot(arg, brg=["a", "b"], crg='python'):
print("arg: " + str(arg))
print("brg: " + str(brg))
print("crg: " + str(crg))
## arg: 1000
## brg: ['a', 'b']
## crg: python
## arg: 1000
## brg: ['a', 'b']
## crg: python
## arg: 1000000
## brg: ['a', 'b']
## crg: R
## arg: 1000000
## brg: ['a', 'b']
## crg: JAVA
## arg: 32611
## brg: [1, 2, 3]
## crg: C++
parrot(567, brg=list("abc")) # 1 positional, 1 keyword
# parrot(brg=list("abc"), 567) # 1 positional, 1 keyword, doesn't work
## arg: 567
## brg: ['a', 'b', 'c']
## crg: python
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 0x119aa1e40>
## [('a', 1), ('b', 2), ('c', 3)]
## a1
## b2
## c3
a = zip(d,e)
next(a)
## a0
## b1
## c2
## [(0, 'a'), (1, 'b'), (2, 'c')]
## (0, 'a')
## [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
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
## source ~/opt/miniconda3/bin/activate ## need this for macOS Catalina or later
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