or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Workshop Details
Dates: September 6th - 13th, 2022
Time: 9am - 12pm
Workshop Agenda:
https://ucsdlib.github.io/2022-09-06-carpentries-uc/
Workshop Lesson:
http://swcarpentry.github.io/python-novice-gapminder/
Day 1 - 3: Introduction to Python
Software Installation:
Anaconda
https://www.anaconda.com/download/
Lesson Data (download)
NOTES:
A copy of the instructor live session notes will be made available to participants upon request at the end of the workshop.
Jupyterlab will be used for the lessons
[m] Markdown cell = notes
[#]also works in code cell for notes
[b] = add cell below [a] is above
[r]Raw cells cannot have text edits
(for Python lessons)
https://www.markdownguide.org/getting-started/
https://www.markdownguide.org/basic-syntax/
Workshop Day 1
First name and Last Name/Organization/Dept./Email |
Day 1 Questions:
Please enter any questions not answered during live session here:
1.
Day 1 Live Class Notes:
Download link: https://www.anaconda.com/products/distribution
Working in Anaconda JupyterLab
GUI (middle-man, colloquially pronounced as "gooey") vs command-line
Today's workshop is strictly in JupyterLab GUI
Computer programming languages - there are a lot of them, and what they do is similar, syntax is also similar between different languages (although, each is specific). Able to learn the basics and apply them to different langauges.
Your favorite search engine is a good resource when you're looking for answers to your programming questions (kat's note: I <3 Stack Exchange)
working directory - in JupyterLab, working directory is shown on the left sidebar. Left sidebar also shows tabs, such as file browse (where you can select your working directory, create new files/folders), a list of what terminals are running, etc. The left sidebar can also be collapsed or expanded. Running anaconda JupyterLab is local to your computer, so when you're using a public computer, any files are saved on that public computer
new file - Day1_Python_LiveNotes.ipynb (to rename, right click on file to bring up submenu)
Interface - menu bar at top contains more options than the tabs in the left sidebar quicklinks
Command and Edit modes - press B will create a new cell below current cell
Hello There
Numbered lists
2. level 2, also requires tabs
A tool like HackMD lets you practice markdown.
Bold and italics
In JupyterLab markdown cells, you can combine some html elements, such as <br>
backslash \ before the less-than-symbol will escape the character so it isn't read as html \<br>
Mixed list
Headings use # to create different sizes
largest
one smaller
smaller
etc
even smaller
Markdown Cheatsheet
Save and save often
Challenge #1
Lesson 2: Variables and Assignments
age = 42
first_name = 'Ahmed'
thisisaverylongnamethatishardforahumantoread = "Jimmy"
this_is_more_readable = "Jimmy"
thisIsCamelCase = "jimmy"
x
is not self-describing,age
orweight
are self-describing)_dont_use_until_you_understand_what_it_means
)3age
(starts with a number) orread@one
(uses any symbol other than the underscore _)Built in functions
print()
prints things as textprint(first_name, 'is', age, 'years old')
will printAhmed is 42 years old
print()
will automatically add single spaces in the current version of Python.print(argument1, argument2, argument3, argument4)
Variables
print(myval)
will give an error ifmyval
isn't already created with a valueThis will throw an error because
last_name
does not have an assigned valueThis will not throw an error
Challenge #2
Assign the variable named color1 to the value red and the variable named color2 to the value blue. Then print
red is not blue
using the variable names as input (or arguments)Blocks of text
variables used in calculations
age = age + 3
3 + 5 * 4
calculates according to math rules (order of operations), not read left to right3 + 5 * 4
=23
(3 + 5) * 4
=32
Challenge #3
Write the code for for the following: number1 is 22, number2 is 5, and number3 is 100. Multiple number1 by number3 then divid by number2. The answer calculation answer should be number4. Finally, output 'The answer is number4' - with the value displaying rather than the variable.
Built-in functions
index()
gives you a single character from a stringoutput is
h
index()
uses the variable name, then square brackets around the number of the index you want to obtaindatatype strings are text surrounded by single or double-quotes (pair single-quotes with single-quotes, don't interchange 'like this")
will result in error because
id_number
is an integer, and not a stringlist
output is
pear
slices
variable[start position: stop position(not including)]
output is
sod
output will be
['carbon', 'nitrogen', 'neon']
(notice how it outputs in a list format!)how long are things?
len()
output is
6
(counts number of characters)output is
5
(counts number of elements in list)Challenge #4
thing[:]
(just a colon) do?thing[number:some-negative-number
do?Solution #4
number
to the the negative count from the end of the variableoutput is
ca
atom_name[1:3] is: ar
Data types & type conversion
int()
float()
float()
type()
type(52)
will outputint
print(type(52))
will output<class 'int'>
output is
<class 'str'>
print(type(hair))
will throw an error, because Python is readinghair
as a variable name, which isn't defined.output is
<class 'float'>
will output
2
will throw an error because you can't subtract strings
You can use '+' and '*' on integers, floats, and strings, but operates differently on strings
output is
9
output is
AhmedWalch
output is
AhmedAhmedAhmedAhmedAhmedAhmedAhmedAhmedAhmedAhmed
will throw an error.
however,
will output
3
because'2'
is type cast as an integer, allowing math operations.will output
12
(which is actually a string, not a number!)will output
Gene23455685
, which allows easy labels!Variables only change values once the value is (re-)assigned
if you need to keep an original value of a variable, create a new variable name, otherwise you're overwriting the original value.
LIVE LESSON NOTES: https://drive.google.com/file/d/1TSm1bA55RwQu5-iqdnBNRU47U3os9x86/view?usp=sharing
End Day 1
Workshop Day 2
First name and Last Name/Organization/Dept./Email
Day 2 Questions:
Please enter any questions not answered during live session here:
1.
Day 2 Live Class Notes:
Gapminder data download: http://swcarpentry.github.io/python-novice-gapminder/files/python-novice-gapminder-data.zip
Lesson 5 Libraries
Most of the power of a programming language is in its libraries.
A library is a collection of files (called modules) that contains functions for use by other programs.
A program must import a library module before using it.
Use
import
to load a library module into a program’s memory.pi is 3.141592653589793 cos(pi) is -1.0
Have to refer to each item with the module’s name.
Use help to learn about the contents of a library module.
Import specific items from a library module to shorten programs.
cos(pi) is -1.0
Create an alias for a library module when importing it to shorten programs.
cos(pi) is -1.0
Challenge
import
withoutas
.Solution:
Lesson 6: Writing Functions
Define a function using def with a name, parameters, and a block of code.
def
hello!
2022/1/2
2019/1/23
Defining a function using the
return
call.2.6666666666666665
None
1871/3/19 result of print_date
None
Challenge
What is wrong with this example?
11:37:59 result of call is: None
Reading tabular data into data frames
stat info of your data
Print column names
Dataframes
Dataframes are a collection of columns. Within a column it has to be the same data type (e.g. float, int, str)
Challenge
gapminder_gdp_americas.csv
into a variable calledamericas
and display its summary statistics.help(americas.head)
andhelp(americas.head)
to find out whatDataFrame.head
andDataFrame.tail
do.solution:
Getting data out of your data frame
Get data subsets by position
Get data subsets by label
Get row by label
alt solution:
Filter data
LIVE LESSON NOTES:
Day 2 live notes A
Day 2 live notes B
End Day 2
Workshop Day 3
First name and Last Name/Organization/Dept./Email
Day 3 Questions:
Please enter any questions not answered during live session here:
1.
Day 3 Live Class Notes:
Challenges
Challeges #1
Fill in the blanks below to plot the minimum GDP per capita over time for all the countries in Europe. Modify it again to plot the maximum GDP per capita over time for Europe.
Challenge #1 solution
Challenge #2
Fill in the blanks so that the program below produces the output shown.
Challenge #2 solution
Challenge #3
Fill in the blanks in each of the programs below to produce the indicated result.
Challenge #3 solution
Challenge #4
Fill in the blanks so that this program creates a new list containing zeroes where the original list’s values were negative and ones where the original list’s values were positive.
Challenge #4 solution
LIVE Session Notes: https://drive.google.com/file/d/1y8A0xUEWSdSrAhS9Sbvx39Etb1Vnn4rM/view?usp=sharing
End Day 3