Some of your majors: Biotech, Pharmacy, Biochemistry, Chemistry, Psychology, Public Health/Health Studies, Biology, Kinesiology, Life Physics, Geography, Environment/Environmental Management, Business, English, Hydrogeology
Because we need a programming language: Python is beginner-friendly
Widely used in industry and academia
It's free and there are plenty of free learning resources online
However:
We focus on teaching transferable knowledge/programming foundations
If everyone in your group is using Matlab/R/xyz, then it's probably better to use Matlab/R/xyz
https://github.com/uwpyb/materials/
http://goo.gl/N5Q025
Test everything works
Familiarize ourselves with the programming workflow
First programming steps
Telling your computer what do to!
Natural language:
Programming language:
if my_location=="grocery store" and ("mango" in icecream_selection):
buy(ice_cream)
print("Good evening!")
Good evening!
If you don't see this message or things look broken, put up a red sticky note.
10+5
15
10/20
0.5
10.5-(2+3)
5.5
What will happen if we type:
1/0.
a) 0
b) Python will crash and we will have to start it again
c) We will get an error
Hints and solutions to problems are available below (down arrow key)
1/0.
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) <ipython-input-7-45dd8a1a4700> in <module>() ----> 1 1/0. ZeroDivisionError: float division by zero
This worked:
2+4
6
What is the result of adding together text and numbers?
"Good evening" + 5
More on data types later!
print()
commandprint("Good evening everyone!")
Good evening everyone!
print("Good evening " + "at 8 pm!")
Good evening at 8 pm!
It is common to make mistakes when typing...
print("Hmmm)
File "<ipython-input-11-0324a32cf4e8>", line 1 print("Hmmm) ^ SyntaxError: EOL while scanning string literal
print("Hmmm"))
File "<ipython-input-12-2ba79029f2b4>", line 1 print("Hmmm")) ^ SyntaxError: invalid syntax
Syntax Error: Python tells us we did not respect the rules of the language
hello.py
print("Good evening everyone!")
print("Good evening at 8pm!")
Now we have to tell Python to locate hello.py
and to run it.
For that, we need to understand how our computer organizes files and folders...
Find the address of the workshop
directory
C:\Users\ivana\Desktop\workshop
Find the address of the hello.py
script
C:\Users\ivana\Desktop\workshop\hello.py
2 ways of navigating through the content of your disk:
In IPython/Qtconsole:
pwd
'/home/ivana/teaching/uw-programming-ws/lectures'
cd
/home/ivana
cd Desktop
/home/ivana/Desktop
cd workshop
/home/ivana/Desktop/workshop
ls
hello.py
Note: We can use cd
on its own, or with a directory name (such as cd workshop
). The former takes us home, the latter takes us where we specify
1.
Run pwd to check where you are. It should end with workshop:
pwd
'/home/ivana/Desktop/workshop'
2.
Check that there is a script called hello.py:
ls
hello.py
3.
Run the script:
run hello.py
Good evening everyone! Good evening at 8pm!
Modify hello.py
to to say good evening at the current time!
After you made the modifications, save the changes and run the script again by typing:
run hello.py
in IPython/Qtconsole.
In the workshop directory on your Desktop, create a new script called missing.py
. Inside that script type the following lines (feel free to edit them so they say something different):
"Away above the chimney tops."
"That's where you'll find me"
100
# print("Will you ever find me?")
Run the script with run missing.py
.
Is the output what you expected it to be?
Change the program so that first three lines are displayed when you run the program.
Note: difference between typing pure text in a script and in Ipython
We could not add text and numbers, as those are two different data types:
type("Hello!")
str
type(1)
int
type(1.1)
float
type("-2.0")
a) int
b) str
c) float
print("Hello" + 123)
a) Error
b) Hello123
c) Hello 123
Don't mix apples and oranges!
We can cast integers into strings (and sometimes strings into integers) in the following way:
str(4)
'4'
int("4")
4
Question: What happens if you try to convert the string 'abc' into an integer?
What would we change in this line:
print("Hello" + 4)
to make it print Hello4
instead of an error?
a) print(int("Hello")+4)
b) print(str("Hello")+4)
c) print("Hello"+str(4))
Variables let us save values and manipulate them
box = 3
print(box)
3
bigger_box = 100 + box * 2
print(bigger_box)
106
print(bigger_box - 6)
100
box = "kittens"
print(box)
kittens
print(box + box + box)
kittenskittenskittens
If I run the following code, what will be the final value of b
?
a = 1
b = a * 5
a = 2
a) 5
b) 1
c) 10
a = 0
print(a)
a = a + 1
print(a)
0 1
Notice: In programming a = a + 1
is different than a = a + 1
in math. In programming, this means take the value of a and increase it by one. In math, this would be an equation with one unknown, and it wouldn't make any sense since we would get 0=1
. In programming, we talk about variable assignment, and in math about equality.
What is the following print statement going to print?
x = 0
y = x
x = x + 1
print("The value of x is " + str(x))
print("The value of y is " + str(y))
whos
Variable Type Data/Info ------------------------------ a int 1 bigger_box int 106 box str kittens
animal = "123 snAKes"
print(animal)
123 snAKes
We can access inidividual characters, but we start counting at 0:
animal[0]
'1'
That number is also called an index. To access multiple characters we use a range of indices:
animal[2:9]
'3 snAKe'
Use -1
to get the last character:
animal[-1]
's'
To start from the beginning up to some character, we can drop 0
:
animal[:8] # equivalent to animal[0:8]
'123 snAK'
Similarly, to get a substring starting at some character, until the end:
animal[8:] # equivalent to animal[8:10]
'es'
To find out how many characters we have:
len(animal)
10
Notice that len
is used with brackets ()
, similar to print
. This is because both len
and print
are functions
fname = "Panda"
lname = "Spotty-Dotty"
len_fname = len(fname)
print(lname[len_fname:len_fname+3])
a) Spo
b) pot
c) y-D
What would you expect output to be for the following line:
print("AaAa" * 5)
a) AaAa5
b) AaAaAaAaAaAaAaAaAaAa
c) Error
print("-"*20)
print("<"*6 + " Hello! " + ">"*6)
print("-"*20)
-------------------- <<<<<< Hello! >>>>>> --------------------
Type in following lines in IPython:
import numpy as np
import matplotlib.pyplot as plt
Green sticky note: Nothing happened
Red sticky note: Python is shouting at me with some errors
Link to the exercises:
https://github.com/uwpyb/materials/blob/master/lectures/Day_0_Exercises.ipynb
Frontpage graphics, "First Steps" graphics: Vector Graphics by www.vecteezy.com