Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions Day_1/00_Intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ This unit provides a basic introduction to Python. By the end of the week, you s

> ## Learning Objectives
>
> * Explain the difference between knowing a programming language knowing how to program.
> * Explain the difference between knowing a programming language and knowing how to program.
> * Explain how programming languages can differ.
> * Give useful debugging tips.
> * Offer helpful resource websites.
> * Explain how to google an error.

### What it means to "know how to program"

Most programmers can program in more than one language. That's because they know *how to program* generally, as opposed to "knowing" Python, R, Ruby, or whatever.
Most programmers can program in more than one language. That's because they know *how to program* generally, as opposed to "knowing" Python, R, Ruby, or whatever.

In other words, programming is an extendible skill. Basic programming concepts -- conditionals, for loops, functions -- can be found in almost any programming language.
In other words, programming is an extendible skill. Basic programming concepts -- conditionals, for loops, functions -- can be found in almost any programming language.

That being said, programming languages differ from one anther in the following ways:
That being said, programming languages differ from one another in the following ways:

1. **Syntax**: whether to add a semicolon at the end of each line, etc.
2. **Usage**: javascript is for building websites, R is for statistics, Python is general purpose, etc.
3. **Level**: how close are you to the hardware. 'C' is often considered to be the lowest (or one of the lowest) level languages.
2. **Usage**: JavaScript is for building websites, R is for statistics, Python is general purpose, etc.
3. **Level**: how close you are to the hardware. 'C' is often considered to be the lowest (or one of the lowest) level languages.
4. **Object-oriented:** "objects" are data + functions. Some programming languages are object-oriented (e.g. Python) and some aren't (e.g. C).
5. **Many more**: Here's a [list](https://en.wikipedia.org/wiki/List_of_programming_languages_by_type) of all the types of programming languages out there.

Expand All @@ -43,7 +43,7 @@ Thus "knowing how to program" means learning how to *think* like a programmer, n

![xkcd](http://sslimgs.xkcd.com/comics/wisdom_of_the_ancients.png)

Here's the sad reality: When you're programming, 80% or more of you time will be spent debugging, looking stuff up (like program-specific syntax, documentation for packages, useful functions, etc.), or testing. This does not just apply to beginner or intermediate programmers, although you will grow more "fluent" over time.
Here's the sad reality: When you're programming, 80% or more of your time will be spent debugging, looking stuff up (like program-specific syntax, documentation for packages, useful functions, etc.), or testing. This does not just apply to beginner or intermediate programmers, although you will grow more "fluent" over time.

If you're a good programmer, you're a good detective!

Expand All @@ -61,4 +61,3 @@ If you're a good programmer, you're a good detective!
* google: name-of-program + text in error message
* Remove user- and data-specific information first!
* See if you can find examples that do and don’t produce the error

51 changes: 24 additions & 27 deletions Day_1/01_Running-Python.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,45 @@

> ## Questions
> How can I run Python programs?
>
>
> ## Learning Objectives
>
> * Explain how the shell relates to other programs like Python.
> * Open a Python interpreter from the shell.
> * Run a Python script from the shell.
> * Understand the importance of IDE's and program-specific tools.
> * Find which version of Python is used by the interpreter.
> * Start an ipython notebook server from the shell
> * Start an iPython notebook server from the shell

## Running Python in Shell: Interactive Mode

If you took Programming FUN!damentals, you used the shell as the interface between us and the UNIX operating system.

But we can also use shell to interact with other programs. For example, we can run a Python interpreter right in shell. An **interpreter** is a program that reads and executes code. This includes source code and scripts.
But we can also use shell to interact with other programs. For example, we can run a Python interpreter right in shell. An **interpreter** is a program that reads and executes code. This includes source code and scripts.

~~~python
$ python

Python 2.7.6 (default, Mar 22 2014, 22:59:56)
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
~~~

The `>>>` is Python's way of telling you that you are in **interactive mode**. In interactive mode, what you type is immediately run.
The `>>>` is Python's way of telling you that you are in **interactive mode**. In interactive mode, what you type is immediately run.

~~~python
>>> 5
5
>>> print (5*7)
>>> print(5*7)
35
>>> "hello" * 4
'hellohellohellohello'
>>> type("hello")
<type 'str'>
~~~

To escape the Python interpreter and go back to Shell, type
To escape the Python interpreter and go back to Shell, type

~~~python
>>> quit()
Expand All @@ -50,57 +50,55 @@ $
Notice that the terminal window will go back to bash, giving you a `$` prompt.

> #### Which Python?
>
>
> Python, like other programs, come in versions. And with each version, the
> features may change. That means that running code made for Python 2 might
> not work on Python 3. This is one of the motivations behind BCE -
> to standardize software versions for teaching, etc.
>
> To see which version of Python you have, enter the command `which python` in
>
> To see which version of Python you have, enter the command `which python` in
> bash. You can use the `which` command with other programs, too.

## Running Python in Shell: Normal Mode

Python has two basic modes: normal and interactive. Interactive mode allows you to test out and see what Python will do. If you ever feel the need to play with new Python statements, go into interactive mode and try them out.

If you quit from the Python interpreter and enter it again, the definitions you just made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead.
If you quit from the Python interpreter and enter it again, the definitions you just made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead.

This is known as creating a **script** (just like the shell scripts we created earlier). Python scripts have the ".py" extension to let everyone know (including the operating system) it is a Python program.
This is known as creating a **script** (just like the shell scripts we created in Programming FUN!damentals). Python scripts have the ".py" extension to let everyone know (including the operating system) it is a Python program.

The normal mode is the mode where the scripted and finished .py files are run in the Python interpreter. To run a program, make sure you're in bash mode in your terminal (the default mode), `cd` into the directory in which the Python program (.py file) is located, and then enter the command `python [name of file]`. Here's an example:

~~~bash
$ python madlib.py
~~~

Once the program ends, it will give a `$` prompt, returning to bash mode.
Once the program ends, it will give a `$` prompt, returning to bash mode.

## IDEs and other Tools

It's common to write python programs using either a text editor or an interactive/integrated development environment (IDE).An IDE is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, build automation tools and a debugger. Some of them come with package managers and other features, too.
It's common to write python programs using either a text editor or an interactive/integrated development environment (IDE). An IDE is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, build automation tools, and a debugger. Some of them come with package managers and other features, too.

There are many Python IDE's. You can see a comparison [here](https://en.wikipedia.org/wiki/Comparison_of_integrated_development_environments#Python)

> #### Editors v. IDEs
>
> Notepad++, Vim, SublimeText etc are text editors. They is not specific
> to Python, or to any other language. They are not an IDE. That said,
> these editors are extensible, and through the use of plugins they allow the
> user to implement IDE like functionality for as broad a range of languages
>
> Notepad++, Vim, SublimeText etc are text editors. They are not specific
> to Python, or to any other language. They are not an IDE. That said,
> these editors are extensible, and through the use of plugins they allow the
> user to implement IDE-like functionality for as broad a range of languages
> or syntaxes as plugin writers care to cover.

## Jupiter Notebooks
## Jupyter Notebooks

This course will be using a Jupyter Notebook to interact with Python. The bit of extra setup is well worth it because the Notebook provides code completion and other helpful features.

Jupyter Notebooks are included in the Anaconda distribution. Notebook files have the extension ".ipynb" to distinguish them from plain-text Python programs.
Jupyter Notebooks are included in the Anaconda distribution. Notebook files have the extension ".ipynb" to distinguish them from plain-text Python programs.

To start a notebook server, simply type

FIXME: ipython or jupyter in the code below?

~~~bash
$ ipython notebook
$ jupyter notebook
~~~

This will start a Jupyter Notebook server and open your default web browser.
Expand All @@ -114,10 +112,9 @@ This has several advantages:

- You can easily type, edit, and copy and paste blocks of code.
- Tab complete allows you to easily access the names of things you are using and learn more about them.
- It allows you to annotate your code with links, different sized text, bullets, etc to make it more accessible to you and your collaborators.
- It allows you to annotate your code with links, different sized text, bullets, etc., to make it more accessible to you and your collaborators.
- It allows you to display figures next to the code that produces them to tell a complete story of the analysis.
- The notebook is stored as JSON but can be saved as a .py file if you would like to run it from the bash shell or a python interpreter.
- Just like a webpage, the saved notebook looks different to what you see when it gets rendered by your browser.
- Just like a webpage, the saved notebook looks different than what you see when it gets rendered by your browser.

Let's get programming!

16 changes: 9 additions & 7 deletions Day_1/02_Jupyter Notebooks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"\n",
"**Don't panic!**\n",
"\n",
"*You can't break anything!* You can always get another copy of any notebook from the workshop on the github repo **FIXME**"
"*You can't break anything!* You can always get another copy of any notebook from the workshop on the [github repo](https://github.com/dlab-berkeley/python-intensive)."
]
},
{
Expand Down Expand Up @@ -152,7 +152,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also split cells by puting your cursor on the line you want to split, and clicking on Edit --> Split cell. Split the cell below after the `c = 2` line"
"You can also split cells by putting your cursor on the line you want to split, and clicking on Edit --> Split cell. Split the cell below after the `c = 2` line"
]
},
{
Expand Down Expand Up @@ -199,6 +199,8 @@
"* to create\n",
"* bullet lists.\n",
"\n",
"but\n",
"\n",
"1. Use numbers\n",
"2. to create\n",
"3. numbered lists.\n",
Expand All @@ -222,7 +224,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"By default, cells in jupyter notebook are code cells, which means it's expecting you to enter Python code. But you can change the cell format by changing the dropdown item on the top from \"Code\" to \"Markdown\"\n",
"By default, cells in jupyter notebook are code cells, which means it's expecting you to enter Python code. But you can change the cell format by changing the dropdown item on the top from \"Code\" to \"Markdown\".\n",
"\n",
"Enter some markdown text in the cell below, and execute it. Be sure to change the cell from code to markdown!"
]
Expand Down Expand Up @@ -285,7 +287,7 @@
"\n",
"Jupyter remembers everything it executed, **even if it's not currently displayed in the notebook**.\n",
"\n",
"To clear everything from Jupyter use Kernel->Restart in the menu."
"To clear everything from Jupyter use Kernel-->Restart in the menu."
]
},
{
Expand Down Expand Up @@ -313,7 +315,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now use Kernel->Restart in the menu!"
"Now use Kernel-->Restart in the menu!"
]
},
{
Expand Down Expand Up @@ -352,9 +354,9 @@
"source": [
"## Exiting Jupyter\n",
"\n",
"When you cose your Jupyter notebook window, all of your values will be lost. But you can save your code for a later time.\n",
"When you close your Jupyter notebook window, all of your values will be lost. But you can save your code for a later time.\n",
"\n",
"To end the Jupyter server, go back to your shell and type `CNTL+C`"
"To end the Jupyter server, go back to your shell and type `CTRL+C`"
]
},
{
Expand Down
10 changes: 5 additions & 5 deletions Day_1/03_Variables_Assignment.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"- Challenges: 5 min\n",
"\n",
"**Questions**\n",
"- How can I store data in programs?\n",
"- \"How can I store data in programs?\"\n",
"\n",
"**Learning Objectives**\n",
"- Write programs that assign scalar values to variables and perform calculations with those values.\n",
"- Correctly trace value changes in programs that use scalar assignment.\n",
"- \"Write programs that assign scalar values to variables and perform calculations with those values.\"\n",
"- \"Correctly trace value changes in programs that use scalar assignment.\"\n",
"\n",
"* * * * *"
]
Expand All @@ -33,7 +33,7 @@
"* Variables are names for values.\n",
"* In Python the `=` symbol assigns the value on the right to the name on the left.\n",
"* The variable is created when a value is assigned to it.\n",
"* Here's Python that assigns an age to a variable `age`\n",
"* Here's Python code that assigns an age to a variable `age`\n",
" and a name in quotation marks to a variable `first_name`.\n"
]
},
Expand Down Expand Up @@ -134,7 +134,7 @@
"metadata": {},
"source": [
"* The last line of an error message is usually the most informative.\n",
"* We will look at error messages in detail [later]({{ site.github.url }}/05-error-messages/).\n",
"* We will look at error messages in detail [later](https://github.com/dlab-berkeley/python-intensive/blob/master/Day_3/15_Errors.ipynb).\n",
"\n",
"## Python is case-sensitive.\n",
"\n",
Expand Down
11 changes: 10 additions & 1 deletion Day_1/04_Types_Conversion.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@
"source": [
"## Challenge 2: Division Types\n",
"\n",
"The `//` operator calcultaes the whole-number result of division, while the '%' operator calculates the remainder from division:"
"The `//` operator calculates the whole-number result of division, while the '%' operator calculates the remainder from division:"
]
},
{
Expand Down Expand Up @@ -464,6 +464,15 @@
"If `num_subjects` is the number of subjects taking part in a study, and `num_per_survey` is the number that can take part in a single survey, write an expression that calculates the number of surveys needed to reach everyone once."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
Loading