Editing Community Data Science Workshops (Core)/Day 0 Tutorial
From CommunityData
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
Welcome to the Friday tutorial! | |||
Welcome to the tutorial! | |||
This tutorial covers several core programming concepts that we'll build upon during an interactive lecture tomorrow morning. It will take 1-2 hours to complete. There's a break in the middle, and exercises at the middle and end to help review the material. | This tutorial covers several core programming concepts that we'll build upon during an interactive lecture tomorrow morning. It will take 1-2 hours to complete. There's a break in the middle, and exercises at the middle and end to help review the material. | ||
This is an interactive tutorial! As you go through this tutorial, any time you see something that looks like this: | This is an interactive tutorial! As you go through this tutorial, any time you see something that looks like this: | ||
Line 15: | Line 9: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
you should type the expression | you should type the expression at a '''Python''' prompt, hitting Return after every line and noting the output. | ||
No copying and pasting! You'll learn the concepts better if you type them out yourself. | No copying and pasting! You'll learn the concepts better if you type them out yourself. | ||
Line 23: | Line 17: | ||
[[File:Calculator.png|100px]] | [[File:Calculator.png|100px]] | ||
Math in Python looks a lot like math you type into a calculator. A | Math in Python looks a lot like math you type into a calculator. A Python prompt makes a great calculator if you need to crunch some numbers and don't have a good calculator handy. | ||
===Addition=== | ===Addition=== | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
2 + 2 | 2 + 2 | ||
1.5 + 2.25 | 1.5 + 2.25 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===Subtraction=== | ===Subtraction=== | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
4 - 2 | 4 - 2 | ||
100 - .5 | |||
100 - .5 | |||
0 - 2 | 0 - 2 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===Multiplication=== | ===Multiplication=== | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
Line 80: | Line 66: | ||
There's a helpful '''function''' (more on what a function is in a second) called <code>type</code> that tells you what kind of thing -- what '''data type''' -- Python thinks something is. We can check for ourselves that Python considers '1' and '1.0' to be different data types: | There's a helpful '''function''' (more on what a function is in a second) called <code>type</code> that tells you what kind of thing -- what '''data type''' -- Python thinks something is. We can check for ourselves that Python considers '1' and '1.0' to be different data types: | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
type(1) | type(1) | ||
type(1.0) | type(1.0) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
So now we've seen two data types: '''integers''' and '''floats''' | So now we've seen two data types: '''integers''' and '''floats'''. | ||
By the way, what is a "function"? Here are the important ideas about functions: | |||
* A function encapsulates | * A function encapsulates a useful bit of work and gives that work a name. | ||
* You provide input to a function and it produces output. For example, the <code>type</code> function takes data as an input, and produces what type of data the data is (e.g. an integer or a float) as output. | * You provide input to a function and it produces output. For example, the <code>type</code> function takes data as an input, and produces what type of data the data is (e.g. an integer or a float) as output. | ||
* To use a function, write the name of the function, followed by an open parenthesis, then what the function needs as input (we call that input the '''arguments''' to the function), and then a close parenthesis. | * To use a function, write the name of the function, followed by an open parenthesis, then what the function needs as input (we call that input the '''arguments''' to the function), and then a close parenthesis. | ||
Line 100: | Line 84: | ||
[[File:Function_diagram.png]] | [[File:Function_diagram.png]] | ||
=== | ===Command history=== | ||
Stop here and try hitting the Up arrow on your keyboard a few times. The Python '''interpreter''' saves a history of what you've entered, so you can arrow up to old commands and hit Return to re-run them! | |||
==Variables== | ==Variables== | ||
Line 110: | Line 94: | ||
A lot of work gets done in Python using variables. Variables are a lot like the variables in math class, except that in Python variables can be of any data type, not just numbers. | A lot of work gets done in Python using variables. Variables are a lot like the variables in math class, except that in Python variables can be of any data type, not just numbers. | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
type(4) | type(4) | ||
x = 4 | x = 4 | ||
x | x | ||
type(x) | type(x) | ||
2 * x | 2 * x | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Giving a name to something, so that you can refer to it by that name, is called '''assignment'''. Above, we assigned the name 'x' to 4, and after that we can use <code>x</code> wherever we want to use the number 4 | Giving a name to something, so that you can refer to it by that name, is called '''assignment'''. Above, we assigned the name 'x' to 4, and after that we can use <code>x</code> wherever we want to use the number 4. | ||
Variables can't have spaces or other special characters, and they need to start with a letter. Here are some valid variable names: | Variables can't have spaces or other special characters, and they need to start with a letter. Here are some valid variable names: | ||
Line 152: | Line 127: | ||
You can think of it as that something needs to get the output. Without an assignment, the winner is the screen. With assignment, the output goes to the variable. | You can think of it as that something needs to get the output. Without an assignment, the winner is the screen. With assignment, the output goes to the variable. | ||
You can reassign variables if you want | You can reassign variables if you want: | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
x = 4 | x = 4 | ||
x | x | ||
x = 5 | x = 5 | ||
x | x | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 168: | Line 137: | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
x = 3 | x = 3 | ||
y = 4 | y = 4 | ||
x * y | x * y | ||
x * x | x * x | ||
2 * x - 1 * y | 2 * x - 1 * y | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 212: | Line 173: | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
"Hello" | "Hello" | ||
"Python, I'm your #1 fan!" | "Python, I'm your #1 fan!" | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 219: | Line 178: | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
type("Hello") | type("Hello") | ||
type(1) | type(1) | ||
type("1") | type("1") | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 231: | Line 186: | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
"Hello" + "World" | "Hello" + "World" | ||
name = "Jessica" | name = "Jessica" | ||
Line 241: | Line 194: | ||
"Hello" + 1 | "Hello" + 1 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Hey now! The output from the previous example was really different and interesting; let's break down exactly what happened: | Hey now! The output from the previous example was really different and interesting; let's break down exactly what happened: | ||
<pre> | <pre> | ||
>>> "Hello" + 1 | |||
Traceback (most recent call last): | Traceback (most recent call last): | ||
File "<stdin>", line 1, in <module> | File "<stdin>", line 1, in <module> | ||
Line 282: | Line 235: | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
len("Hello") | len("Hello") | ||
len("") | |||
len("") | |||
fish = "humuhumunukunukuapua'a" | fish = "humuhumunukunukuapua'a" | ||
name_length = len(fish) | name_length = len(fish) | ||
fish + " is a Hawaiian fish whose name is " + str(name_length) + " characters long." | fish + " is a Hawaiian fish whose name is " + str(name_length) + " characters long." | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 321: | Line 266: | ||
One fun thing about strings in Python is that you can multiply them: | One fun thing about strings in Python is that you can multiply them: | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
"A" * 40 | "A" * 40 | ||
"ABC" * 12 | |||
"ABC" * 12 | |||
h = "Happy" | h = "Happy" | ||
b = "Birthday" | b = "Birthday" | ||
(h + b) * 10 | (h + b) * 10 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Part 1 Practice== | ==Part 1 Practice== | ||
[[File:Detective.png|100px]] | [[File:Detective.png|100px]] | ||
Read the following expressions, but don't execute them. Guess what the output will be. After you've made a guess, copy and paste the expressions | Read the following expressions, but don't execute them. Guess what the output will be. After you've made a guess, copy and paste the expressions at a Python prompt and check your guess. | ||
1. | 1. | ||
Line 341: | Line 282: | ||
total = 1.5 - 1/2 | total = 1.5 - 1/2 | ||
total | total | ||
type(total) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 349: | Line 290: | ||
b = "brown" | b = "brown" | ||
c = "fox jumps over the lazy dog" | c = "fox jumps over the lazy dog" | ||
"The " + a * 3 + " " + b * 3 + " " + c | |||
</syntaxhighlight> | </syntaxhighlight> | ||
==End of Part 1== | ==End of Part 1== | ||
Line 359: | Line 299: | ||
[[File:Fireworks.png|200px]] | [[File:Fireworks.png|200px]] | ||
== Part 2: Printing== | |||
So far we've been learning at the interactive '''Python interpreter'''. When you are working at the interpreter, any work that you do gets printed to the screen. For example: | |||
<syntaxhighlight lang="python"> | |||
h = "Hello" | |||
w = "World" | |||
h + w | |||
</syntaxhighlight> | |||
will display "HelloWorld". | |||
Another place that we will be writing Python code is in a file. When we run Python code from a file instead of interactively, we don't get work printed to the screen for free. We have to tell Python to print the information to the screen. The way we do this is with the '''print''' function. Here's how it works: | |||
<syntaxhighlight lang="python"> | |||
h = "Hello" | |||
w = "World" | |||
print(h + w) | |||
my_string = "Alpha " + "Beta " + "Gamma " + "Delta" | |||
print(my_string) | |||
</syntaxhighlight> | |||
The string manipulate is exactly the same as before. The only difference is that you need to use '''print''' to print results to the screen: | |||
<code>h + w</code> | |||
becomes | |||
<code>print(h + w)</code> | |||
We'll see more examples of the print function in the next section. | |||
==Python scripts== | |||
[[File:Treasure_map.png|100px]] | |||
Until now we've been using the interactive Python interpreter. This is great for learning and experimenting, but you can't easily save or edit your work. For bigger projects, we'll write our Python code in a file. Let's get some practice with this! | |||
* Download the file http://mako.cc/teaching/2015/cdsw-spring/nobel.py by right-clicking on it and saying to save it as a ".py" file to your Desktop. The ".py" extension hints that this is a Python script. | |||
* Open a terminal prompt, and use the navigation commands (<code>dir</code> and <code>cd</code> on Windows, <code>ls</code>, <code>pwd</code>, and <code>cd</code> on OS X and Linux) to navigate to your Desktop directory. See [[Community_Data_Science_Workshops_(Spring_2015)/Day_0_setup_and_tutorial#Goal_.234:_Practice_navigating_the_computer_from_a_terminal|navigating from a terminal]] for a refresher on those commands. | |||
* Once you are in your Desktop directory, execute the contents of <code>nobel.py</code> by typing | |||
python nobel.py | |||
at the terminal prompt. | |||
<code>nobel.py</code> introduces two new concepts: comments and multiline strings. | |||
* Open <code>nobel.py</code> in your text editor (see [[Community_Data_Science_Workshops_(Spring_2015)/Day_0_setup_and_tutorial#Goal_.232:_Prepare_a_text_editor|preparing your text editor]] for a refresher on starting the editor). | |||
* Read through the file in your text editor carefully and check your understanding of both the comments and the code. | |||
Study the script until you can answer these questions: | |||
* How do you comment code in Python? | |||
* How do you print just a newline? | |||
* How do you print a multi-line string so that whitespace is preserved? | |||
==Making choices== | ==Making choices== | ||
Line 373: | Line 367: | ||
<code>print("Six is greater than five!")</code> | <code>print("Six is greater than five!")</code> | ||
That is our first multi-line piece of code, and the way to type it | That is our first multi-line piece of code, and the way to type it at a Python prompt is a little different. Let's break down how to do this (type this out step by step): | ||
<ol> | <ol> | ||
<li>First, type the<br /> | <li>First, type the<br /> | ||
Line 385: | Line 374: | ||
<code>if 6 > 5:</code><br /> | <code>if 6 > 5:</code><br /> | ||
<br /> | <br /> | ||
part, and press Enter. | part, and press Enter. The next line will have <code>...</code> as a prompt, instead of the usual <code>>>></code>. This is Python telling us that we are in the middle of a '''code block''', and so long as we indent our code it should be a part of this code block.</li> | ||
< | <li>Press the spacebar 4 times to indent.</li> | ||
<li>Type<br /> | <li>Type<br /> | ||
<br /> | <br /> | ||
<code>print("Six is greater than five!")</code><br /><br /></li> | <code>print("Six is greater than five!")</code><br /><br /></li> | ||
< | <li>Press Enter to end the line. The prompt will still be a <code>...</code></li> | ||
<li>Press | <li>Press Enter one more time to tell Python you are done with this code block. The code block will now execute.</li> | ||
</ol> | </ol> | ||
Line 399: | Line 387: | ||
<pre> | <pre> | ||
if 6 > 5: | >>> if 6 > 5: | ||
print("Six is greater than five!") | ... print("Six is greater than five!") | ||
... | |||
Six is greater than five! | Six is greater than five! | ||
</pre> | </pre> | ||
Line 581: | Line 569: | ||
Take a break, stretch, meet some neighbors, and ask the staff if you have any questions about this material. | Take a break, stretch, meet some neighbors, and ask the staff if you have any questions about this material. | ||
[[Category:Shared_Pages]] | [[Category:Shared_Pages]] | ||
[[Category:CDSW]] | [[Category:CDSW]] |