Skip to content

Commit

Permalink
Reorganize basics/README.md to align content with related notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
zstumgoren committed Mar 6, 2024
1 parent 4446f24 commit 7551824
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 171 deletions.
249 changes: 136 additions & 113 deletions basics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,211 +34,234 @@ Before we get started, this is meant to be an overview of Python and show you so

> While you are studying programming, I'm studying how to play guitar. I practice it every day for at least two hours a day. I play scales, chords, and arpeggios for an hour and then learn music theory, ear training, songs, and anything else I can. Some days I study guitar and music for eight hours because I feel like it and it's fun. To me repetitive practice is natural and just how to learn something. I know that to get good at anything you have to practice every day, even if I suck that day (which is often) or it's difficult. Keep trying and eventually it'll be easier and fun.
Anyways, let's get started by looking at some key components of any programming language - variables, strings, numbers and comparisons. All of this is contained within the [official Python tutorial](https://docs.python.org/3/tutorial/introduction.html#) to the standard library. We'll consider Python lists as a container that we can fill and we'll work toward using a couple core libraries - [urllib](https://docs.python.org/3/library/urllib.html) and [csv](https://docs.python.org/3/library/csv.html) - to download a CSV file and read the contents.
Anyways, let's get started by looking at some key components of any programming language - numbers, text, comparisons and variables.

**The files**
All of the following is contained within the [official Python tutorial](https://docs.python.org/3/tutorial/introduction.html#).

* ```basics.py``` and ```complete/basics_complete.py```
We'll work on the below exercises in [basics_reference_notebook.ipynb](basics_reference_notebook.ipynb).

* Indentation
> Solutions to the exercises can be found in
[completed/basics_reference_completed.ipynb]([../completed/basics_reference_completed.ipynb]).

* As mentioned in the introduction, Python uses indentation to structure its code blocks instead of braces, brackets, or keywords. This comes into play later on in the session when we begin to write ```if``` statements, ```for``` loops and ```define``` functions. For example:
## Side note: Comments

Throughout the day you will see - and hear us refer to - something called comments. We're not referring to that wasteland of negativity that you find at the bottom of articles on news websites. But just the same, ideally these comments are meant to be constructive.

def my_first_function(input):
if input == None:
output = "I have nothing"
else:
output = "I have something"
* Comments are annotations; a method for programmers to offer notes, advice or justification for why the did something in a script.

Python has a couple ways to comment code:

One is to use the pound symbol, aka hashtag or [octothorp](https://en.wikipedia.org/wiki/Number_sign). Here's an example:

```
# this part of the code is where I make the magic happen
```

* Lines are indented by four spaces.
Another method for multiline comments is to use a series of three quote marks or three apostrophes.

* Comments
```
"""
this is a multiline comment
so i can pack more information
about what i'm doing
"""
```

* Throughout the day you will see - and hear us refer to - something called comments. We're not referring to that wasteland of negativity that you find at the bottom of articles on news websites. But just the same, ideally these comments are meant to be constructive.
## Variables

* Comments are annotations; a method for programmers to offer notes, advice or justification for why the did something in a script.
* A variable is a named container for a value.

* Python has a couple ways to comment code
* A backbone of any programming language

* One is to use the pound symbol, aka hashtag or [octothorp](https://en.wikipedia.org/wiki/Number_sign). Here's an example
* Variables have a scope

# this part of the code is where I make the magic happen
* Variables have a value

* Another method for multiline comments is to use a series of three quote marks or three apostrophes.
* Can be None

"""
this is a multiline comment
so i can pack more information
about what i'm doing
"""
* Can be True or False

* Variables
* Or it could be something else... a number or a string

* A variable is a named container for a value
* When declaring the value of for a variable in Python the format is 'variable = value'

* A backbone of any programming language
* ```my_variable = "the value of my variable"```

* Variables have a scope
## Text

* Variables have a value
[Text](https://docs.python.org/3/tutorial/introduction.html#text) (or more formally in Python, "strings").

* Can be None
* Generally, synonymous with characters.

* Can be True or False
* You can use double quotes or single quotes to create strings

* Or it could be something else... a number or a string
* If using single quotes, apostrophes and single quotes within string must be escaped

* When declaring the value of for a variable in Python the format is 'variable equals value'
* Learning about strings

* ```my_variable = "my value of the variable"```
* double quotes vs. single quotes

* [Numbers](https://docs.python.org/3/tutorial/introduction.html#numbers)
# double quotes
double_quotes_string = "We're going to learn Python at #NICAR16"
print double_quotes_string

* Whole numbers have a type and that type is integer
# single quotes
single_quotes_string = 'We\'re going to learn Python at #NICAR16'
print single_quotes_string

* Fractions have a type and that type is float
* get its type

* Learning about numbers
* get its length

* get the [type](https://docs.python.org/3/library/functions.html#type)
* [lowercase](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.lower)

* addition
* [uppercase](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.upper)

* subtraction
* [titlecase](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.title)

* multiplication
* [split](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.split)

* division
* join

* order of operations
* [replace a character](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.replace)

* used when we want to determine percent change right?
* strip whitespace

```(new - old) / old```
* [all](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.strip)

* [Text](https://docs.python.org/3/tutorial/introduction.html#text) (or more formally in Python, "strings")
* [leading whitespace](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.lstrip)

* Generally, synonymous with characters.
* [trailing whitespace](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.rstrip)

* You can use double quotes or single quotes to create strings
## Numbers

[Numbers](https://docs.python.org/3/tutorial/introduction.html#numbers)

* If using single quotes, apostrophes and single quotes within string must be escaped
* Whole numbers have a type and that type is integer

* Learning about strings
* Fractions have a type and that type is float

* double quotes vs. single quotes
* Learning about numbers

# double quotes
double_quotes_string = "We're going to learn Python at #NICAR16"
print double_quotes_string
* get the [type](https://docs.python.org/3/library/functions.html#type)

# single quotes
single_quotes_string = 'We\'re going to learn Python at #NICAR16'
print single_quotes_string
* addition

* get its type
* subtraction

* get its length
* multiplication

* [lowercase](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.lower)
* division

* [uppercase](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.upper)
* order of operations

* [titlecase](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.title)
* used when we want to determine percent change right?
```
(new - old) / old
```

* [split](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.split)
## Lists

* join
[Lists](https://docs.python.org/3/tutorial/introduction.html#lists)

* [replace a character](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.replace)
* We learned that integers and strings are data types

* strip whitespace
* Python has specific types that allow you to group items. The list is one of these collections.

* [all](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.strip)
* A list is sortable and a list has an index

* [leading whitespace](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.lstrip)
* Index starts at 0

* [trailing whitespace](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.rstrip)
* You can add and remove content

* [Lists](https://docs.python.org/3/tutorial/introduction.html#lists)
* You can add and remove items from specific indexes

* We learned that integers and strings are data types
* Lists might contain items of different types, but usually the items all have the same type.

* Python has specific types that allow you to group items. The list is one of these collections.
* append() allows you to add items to a list

* A list is sortable and a list has an index
* pop() returns the element you want to remove. Useful to remove and keep an item from a list

* Index starts at 0
* By default, pop without any arguments removes the last item

* You can add and remove content
* del deletes the item at the specified index

* You can add and remove items from specific indexes
## Dicts

* Lists might contain items of different types, but usually the items all have the same type.
TK

* append() allows you to add items to a list

* pop() returns the element you want to remove. Useful to remove and keep an item from a list
## Indentation

* By default, pop without any arguments removes the last item
As mentioned in the introduction, Python uses indentation to structure "blocks" of code instead of braces, brackets, or keywords. For example:

* del deletes the item at the specified index
```python
if user_input is None:
output = "I have nothing"
else:
output = "I have something"
```

* Conditionals & Comparisons
* Lines are indented by four spaces.

* Conditional statements

* "[A conditional statement](https://en.wikipedia.org/wiki/Conditional_(computer_programming)), conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false."
## Conditionals & Comparisons

* [for](https://docs.python.org/3/tutorial/controlflow.html#for-statements)
### Conditional statements

* The ```for``` statement iterates through a list or a string in the order they appear.
* "[A conditional statement](https://en.wikipedia.org/wiki/Conditional_(computer_programming)), conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false."

my_list = [1, 2, 3, 4, 5, 6]
for x in my_list:
print x
* [for](https://docs.python.org/3/tutorial/controlflow.html#for-statements)

* [if / elif / else](https://docs.python.org/3/tutorial/controlflow.html#if-statements)
* The ```for``` statement iterates through a list or a string in the order they appear.
```python
my_list = [1, 2, 3, 4, 5, 6]
for x in my_list:
print x
```

* The ```for``` statement iterates through a list or a string in the order they appear.
* [if / elif / else](https://docs.python.org/3/tutorial/controlflow.html#if-statements)

* The `for` statement iterates through a list or a string in the order they appear.

value = 4
if 4 == value:
print "it's the same"
else:
print "it's not the same"

value = 4
if 4 == value:
print "it's the same"
else:
print "it's not the same"
### Comparisons

* [Comparisons](https://docs.python.org/3/library/stdtypes.html#comparisons)
[Comparisons (python docs)](https://docs.python.org/3/library/stdtypes.html#comparisons)

* equals (==)
* equals (`==`)

* are two values the same?
* are two values the same?

* not equals (!=)
* not equals (`!=`)

* are two values different?
* are two values different?

* greater than (>)
* greater than (`>`)

* is value larger than the other?
* is value larger than the other?

* greater than equal to (>=)
* greater than equal to (`>=`)

* is value larger or equal to the other?
* is value larger or equal to the other?

* less than (<)
* less than (`<`)

* is value smaller than the other?
* is value smaller than the other?

* less than equal to (<=)
* less than equal to (`<=`)

* is value smaller or equal to the other?
* is value smaller or equal to the other?

* is
* `is`

* Are two items the exact same thing?
* Are two items the exact same thing?

* is not
* `is not`

* Are two items not the exact same thing?
* Are two items not the exact same thing?
Loading

0 comments on commit 7551824

Please sign in to comment.