Python coding for beginners
Systems
Linux shell
- https://swcarpentry.github.io/shell-novice/
COMMAND --help
ls [OPTION] [PATTERN]
-F: showing /
-a: showing ../
-r: reverse
-R: recursive
[PATTERN]: *.ext, ? (any single character)
pwd
cd
- w/o arguments: going back to home ~
..
- TAB completion
mkdir
-p: for multi-level folders
nano (text editor)
- Ctrl-o + Enter: save
- Ctrl-x: exit
mv SRC/ DEST/
cp FILE_A FILE_B
rm
-i: interactive mode
-r: for folders
wc (word counts)
>: print into
sort
head/tail
| (pipe)
uniq
-c: unique counts of sequential lines
cut
-d ,: delimiter
-f #: field #
- Shell scripts (.sh)
bash script.sh
#: comments
"$#": shorthand for argument #
Python
- https://swcarpentry.github.io/python-novice-inflammation/
- Installation took time.
jupyter
- Re-run
- Mouse vs. Keyboard strokes
conda env list
- Variables
- Data types
type(): type of variable
print()
help()
import numpy
loadtxt('*.csv', delimiter=',')
- type
numpy.ndarray
float64 (larger than a float)
shape (rows, columns)
- index
- Python is a “zero-indexed” language.
- Slicing
- up to but not including the number after the colon
- no final number means go to the end
mean(), std(), amin(),
amax(), diff()
(..., axis=0) (0 by column, 1 by row)
import matplotlib.pyplot as plt
- create a graph –> show the graph (
plt.show()) –>
save figure (plt.savefig(FILE.EXT))
imshow
plot
fig = figure(figsize=(8,8)
ax = fig.add_subplot(nrows, ncols, index)
ax.set_xlabel(), ax.set_ylabel(),
ax.set_xlim(), ax.set_ylim()
ax.grid('on')
fig.tight_layout()
- List
[]
- mutable
- nested list
- heterogeneousness
append(), pop(), reverse()
vs. reversed()
- Tuple
()
- Loops
for [VARIABLE] in [List]:
- indentation
- comments with instructions
import glob
glob.glob('PATTERN'): all matched filenames
sorted()
- Conditions
if (CONDITIONS):
elif (CONDI_A and CONDI_B):
else:
- indentation
>, >=, <,
<=, ==, !=
'''COMMENTS'''
- Functions
def FUNCTION(INPUTS):
- indentation
OUTPUTS = FUNCTION(INPUTS)
- Scopes (local, global)
Git
- https://swcarpentry.github.io/git-novice/index.html
- Configuration
git config --global user.name "NAME"
git config --global user.email "EMAIL"
git config --global core.editor "nano -w"
git config --global init.defaultBranch main
- Local repository
git init
git status
git add FILE_or_DIR –> (staged) –>
git commit -m "DESCRIPTION" –> (committed)
git log
- “q”: leave log
-#: last # entries
--oneline
git diff
- +(green): added
- -(red): deleted
--staged: for staged
.gitignore
- files/folders or patterns of files/folders to be ignored
- Useful commands
touch FILE: create an empty file
cat FILE: print file content
- Remote repository
- Github account
git remote add origin git@github.com:ACCOUNT/REPO_NAME.git
git remote -v
- SSH key
ssh-keygen -t ed25519 -C "COMMENTS"
- copy & paste the public key (
~/.ssh/id_ed25519.pub)
to Github/Settings/SSH and GPG keys/
ssh -T git@github.com (test connection)
git push origin main (or with -u
option)