Editing Community Data Science Workshops (Core)/Day 0 Tutorial

From CommunityData

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

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:
{{Template:CDSW Header}}
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.


To get started, fire up a new Jupyter notebook. Name it as you like.
To get started, fire up a new Jupyter notebook. Name it as you like.  
 
[[File:Creating a python notebook in Jupyter.png|frame|From the Jupyter page, where you should look to create a python notebook!]]


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 11:
</syntaxhighlight>
</syntaxhighlight>


you should type the expression in a new cell of your Jupyter notebook (found in the 'Insert' menu), then hold Shift and hit Return (or Enter) after every line and check the output, making sure it matches your expectations.
you should type the expression in a cell of your Jupyter notebook, then hold Shift and hit Return (or Enter) after every line and check the output, making sure it matches your expectations.


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 28: Line 24:


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
  2 + 2</syntaxhighlight>
  2 + 2
<syntaxhighlight lang="python">
 
  1.5 + 2.25
  1.5 + 2.25
</syntaxhighlight>
</syntaxhighlight>
===Subtraction===
===Subtraction===
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
  4 - 2</syntaxhighlight>
  4 - 2
<syntaxhighlight lang="python">
  100 - .5
 
  100 - .5</syntaxhighlight>
<syntaxhighlight lang="python">
 
  0 - 2
  0 - 2
</syntaxhighlight>
</syntaxhighlight>
===Multiplication===
===Multiplication===
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Line 80: Line 68:
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)</syntaxhighlight>
  type(1)
<syntaxhighlight lang="python">
 
  type(1.0)
  type(1.0)
</syntaxhighlight>
</syntaxhighlight>
So now we've seen two data types: '''integers''' and '''floats'''. Python tags your variables with a data type to make better guesses about what you want and to prevent errors. Another data type you'll run into is strings (a sequence of letters, numbers, and symbols, like the ones you're reading right now -- we'll look at strings a bit more in a moment).
So now we've seen two data types: '''integers''' and '''floats'''.


What's a "function"? Here are the important ideas about functions:
By the way, what is a "function"? Here are the important ideas about functions:


* A function encapsulates (packages up) a useful bit of work and gives that work a name.
* 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 110: Line 96:
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)</syntaxhighlight>
  type(4)
<syntaxhighlight lang="python">
 
  x = 4
  x = 4
</syntaxhighlight>
<syntaxhighlight lang="python">
  x
  x
</syntaxhighlight>
<syntaxhighlight lang="python">
  type(x)
  type(x)
</syntaxhighlight>
<syntaxhighlight lang="python">
  2 * x
  2 * x
</syntaxhighlight>
</syntaxhighlight>
Line 152: Line 129:
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. What do you think will print if you type in:
You can reassign variables if you want:
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
  x = 4</syntaxhighlight>
  x = 4
<syntaxhighlight lang="python">
 
  x
  x
</syntaxhighlight>
<syntaxhighlight lang="python">
  x = 5
  x = 5
</syntaxhighlight>
<syntaxhighlight lang="python">
  x
  x
</syntaxhighlight>
</syntaxhighlight>
Line 168: Line 139:
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
  x = 3
  x = 3
</syntaxhighlight>
<syntaxhighlight lang="python">
  y = 4
  y = 4
</syntaxhighlight>
<syntaxhighlight lang="python">
  x * y
  x * y
</syntaxhighlight>
<syntaxhighlight lang="python">
  x * x
  x * x
</syntaxhighlight>
<syntaxhighlight lang="python">
  2 * x - 1 * y
  2 * x - 1 * y
</syntaxhighlight>
</syntaxhighlight>
Line 212: Line 175:
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
  "Hello"
  "Hello"
</syntaxhighlight>
<syntaxhighlight lang="python">
  "Python, I'm your #1 fan!"
  "Python, I'm your #1 fan!"
</syntaxhighlight>
</syntaxhighlight>
Line 219: Line 180:
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
  type("Hello")
  type("Hello")
</syntaxhighlight>
<syntaxhighlight lang="python">
  type(1)
  type(1)
</syntaxhighlight>
<syntaxhighlight lang="python">
  type("1")
  type("1")
</syntaxhighlight>
</syntaxhighlight>
Line 231: Line 188:
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
  "Hello" + "World"
  "Hello" + "World"
</syntaxhighlight>
<syntaxhighlight lang="python">


  name = "Jessica"
  name = "Jessica"
Line 282: Line 237:
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
  len("Hello")
  len("Hello")
</syntaxhighlight>
  len("")
<syntaxhighlight lang="python">
  len("")</syntaxhighlight>
<syntaxhighlight lang="python">
 
  fish = "humuhumunukunukuapua'a"
  fish = "humuhumunukunukuapua'a"
</syntaxhighlight>
<syntaxhighlight lang="python">
  name_length = len(fish)
  name_length = len(fish)
</syntaxhighlight>
<syntaxhighlight lang="python">
  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 268:
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</syntaxhighlight>
  "A" * 40
<syntaxhighlight lang="python">
  "ABC" * 12
 
  "ABC" * 12</syntaxhighlight>
<syntaxhighlight lang="python">
  h = "Happy"
  h = "Happy"
  b = "Birthday"
  b = "Birthday"
  (h + b) * 10
  (h + b) * 10
</syntaxhighlight>
</syntaxhighlight>
==Part 1 Practice==
==Part 1 Practice==


Line 341: Line 284:
  total = 1.5 - 1/2
  total = 1.5 - 1/2
  total
  total
  print(type(total))
  type(total)
</syntaxhighlight>
</syntaxhighlight>


Line 349: Line 292:
  b =  "brown"
  b =  "brown"
  c = "fox jumps over the lazy dog"
  c = "fox jumps over the lazy dog"
  print("The " +  a * 3 + " " +  b * 3 + " " + c)
  "The " +  a * 3 + " " +  b * 3 + " " + c
</syntaxhighlight>
</syntaxhighlight>
==End of Part 1==
==End of Part 1==


Line 385: Line 327:
&nbsp;&nbsp;&nbsp;&nbsp;<code>if 6 > 5:</code><br />
&nbsp;&nbsp;&nbsp;&nbsp;<code>if 6 > 5:</code><br />
<br />
<br />
part, and press Enter. <!-- The next line will have <code>...</code> as a prompt, instead of the usual <code>&gt;&gt;&gt;</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>
part, and press Enter. The next line will have <code>...</code> as a prompt, instead of the usual <code>&gt;&gt;&gt;</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>Press the spacebar 4 times to indent.</li>
<li>You'll notice that your text caret (|) will be indented by four spaces. This is important, and it tells python that you're telling it what to do with your if statement.</li>
<li>Type<br />
<li>Type<br />
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<code>print("Six is greater than five!")</code><br /><br /></li>
&nbsp;&nbsp;&nbsp;&nbsp;<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 Enter to end the line. The prompt will still be a <code>...</code></li>
<li>Press shift-enter to tell Jupyter to run that block of code.</li>
<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 581: Line 522:
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.


==Common Issues==
===I was expecting python to print lots of lines of text when I ran this cell, but it only prints one line!===
Jupyter only outputs the last line of a code chunk unless you explicitly wrap the code in print() statements. If you split your code into multiple chunks or print() the line, it should look like you expect.
===There are weird numbers next to my code and I didn't type them!===
Those are line numbers. You can turn them off in the View:Toggle Line Numbers option at the top of the notebook.
[[Category:Shared_Pages]]
[[Category:Shared_Pages]]
[[Category:CDSW]]
[[Category:CDSW]]
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see CommunityData:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:

Cancel Editing help (opens in new window)

Template used on this page: