By the end of class, you should be able to
edit files
staging area
git add
git commit
exercise repo
your copy
git clone
your machine
fork
git push
git pull
Desktop
,
Documents
,
~
(Home), etc).
STUDENT.md
that contains your name:
echo
and redirects
>>
)
add
and
commit
your changes
push
your changes back to GitHub, and view the file on the website to confirm that everything worked!
# Student
These exercises were completed by YOUR NAME.
# IF YOU CLONE THE WRONG REPO: change the remote bookmark
git remote set-url origin https://github.com/USER_NAME/ch4-python-intro
what we're using
Jupyter Notebooks are interactive web applications that can be used to write and execute Python programs (as well as textual content). Notebooks can also be shared with others!
Start the Jupyter Notebook server (command line)
jupyter notebook
Create a new notebook or open an existing one (.ipynb
)
Type your code into a code cell.
shift + enter
to execute a cell (or use the
Cell menu)
You can add additional cells, including Markdown cells.
You can "restart" the interpreter through the Kernel menu.
# My first program
print("Hello world!")
Use
print()
to print whatever is in the parentheses to the console. This is an example of a
function.
# My first program
print "Hello world!"
# My first program
print("Hello world!)
# My first program
print("Hello wold!")
Python 2 syntax!
First computer bug (1946)
Admiral Grace Hopper
Read the error message!
"Play Computer" (how is it interpreting your code?)
Inspect the data (e.g., print it!)
Try one statement at a time (isolate the problem)
Explain your code to someone else ("rubber duck debugging")
Ask for help when stuck!
num_cups_coffee = 3
x = 4 # x refers to 4
y = x # y refers to the value of x (4)
z = y + 7 # y refers to sum of y and 4 (11)
z = z + 1 # take z, add 1, and store result
# back in z
When a label is on the left, it means the variable.
When a label is on the right, it means the value.
Assignment gives a label to a value. Changing which value a variable refers to doesn't change other variables.
x = 3
y = 4
x = y # assign the value of y (4) to x
print(x) # 4
y = 5
print(x) # 4;
# valid program, but what does it mean?
a = 35.0
b = 12.50
c = a * b
# much better!
hours = 35.0
pay_rate = 12.50 # needs a raise!
earnings = hours * pay_rate
# Get out.
x1q3z9ahd = 35.0
x1q3z9afd = 12.50
x1q3p9afd = x1q3z9ahd * x1q3z9afd
All values in Python have a particular data type or class ("classification"). Class determines how that data can be used.
type(7) # <class 'int'> (integer)
type(3.14) # <class 'float'> (decimal)
type("Hello") # <class 'str'> (string, text)
Python is dynamically typed, so variables (labels) can refer to any type of value.
my_var = "Hello" # value is a string
my_var = 42 # value is now an integer
Integers (whole numbers) and floats (decimals) are used to store numbers.
x = 2 # whole number
y = 3.5 # decimal (floating point) number
# can perform mathematical operations
z = x + y
# Use parentheses to enforce order of operations
z = 3*(x-y)**2
operator
operands
If an operand is a
float
or you use the division (/
) operator, the result will be a float.
Strings of characters (letters, punctuation, symbols, etc). Written in single or double quotes.
my_name = "Joel" # 'Joel' would be equivalent
# Can include any keyboard symbol (and more!)
course = "IMT/LIS 511: Intro to Programming!"
# Can concatenate strings together
greeting = "Hello" + " " + "World"
# Convert a number to a string to concatenate it
course_code = "IMT/LIS " + str(511)
f(x) = 4x - 3
print("Hello world")
# prints "Hello+++World"
print("Hello", "World", sep = "+++")
# rounds 5/7 to the nearest .01
round(5/7, 2) # 0.71
Function arguments are the "inputs".
Functions may return a value (the "output"). This value must be stored in a variable for the machine to use later!
# store min value in smallest_number variable
smallest_number = min(1, 6/8, 4/3, 5+9) # 0.75
# use the variable as normal, such as for math
twice_min = smallest_number * 2 # 1.5
# use functions directly in expressions
# (the returned value is "anonymous")
number = .5 * round(9.8) # 5.0
# pass the result of a function as an arg to another!
# (`abs` is "absolute value")
# watch out for where the parentheses close!
print(min(2.0, abs(-3))) # prints 2.0
All data values in Python are objects, which include both data (attributes) and behaviors (methods, a.k.a functions)
We can call methods (functions) on a particular data value in order to apply that behavior to that value.
(e.g., tell a particular person variable to say_name()).
Text
We call a method on a data value by using
dot notation, putting the data value, then a dot (
.
), then the function call.
message = "Hello World"
# call the lower() method on the message
# original string does not change
lower_message = message.lower() # "hello world"
# call the replace() method on the message
western_message = message.replace("Hello", "Howdy")
## "Howdy World"
The dot is like an
's in English: "execute message
's lower()
"
Built-in Functions
https://docs.python.org/3/library/functions.html
String Methods
https://docs.python.org/3/library/stdtypes.html#string-methods
Fork and clone
Open in Jupyter Notebook (command-line)
cd path/to/module/exercise
jupyter notebook exercise.ipynb
Next time: functions!