Skip to content

Commit 216af3a

Browse files
committed
fixed it up a bit
1 parent b28a30b commit 216af3a

1 file changed

Lines changed: 93 additions & 26 deletions

File tree

slides_sources/source/session01.rst

Lines changed: 93 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ Windows provides the "DOS" command line, which is OK, but pretty old and limited
332332

333333
If you are comfortable with either of these -- go for it.
334334

335-
If not, you can use the "git Bash" shell -- which is much like the bash shell on OS-X and Linux.
335+
If not, you can use the "git Bash" shell -- which is much like the bash shell on OS-X and Linux. Or, on Windows 10, look into the "bash shell for Windows" otherwise known as the "Linux system" - - more info here: `PythonResources--Windows Bash <http://uwpce-pythoncert.github.io/PythonResources/DevEnvironment/windows_bash.html>`_
336336

337337

338338
LAB: Getting set up
@@ -356,8 +356,7 @@ http://uwpce-pythoncert.github.io/PythonResources/Installing/python_for_linux.ht
356356

357357
We'll run through some of that together.
358358

359-
If you already have a working environment, please feel free to help your neighbor
360-
or look at the Python Resources pages, particularly reviewing/learning the shell and git.
359+
If you already have a working environment, please feel free to help your neighbor or look at the Python Resources pages, particularly reviewing/learning the shell and git.
361360

362361
http://uwpce-pythoncert.github.io/PythonResources
363362

@@ -381,7 +380,7 @@ Step 1: Python 3
381380
.. code-block:: bash
382381
383382
$ python
384-
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
383+
Python 3.6.1 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
385384
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
386385
Type "help", "copyright", "credits" or "license" for more information.
387386
>>>
@@ -517,7 +516,7 @@ MS Word is **not** a text editor.
517516

518517
Nor is *TextEdit* on a Mac.
519518

520-
``Notepad`` is a text editor -- but a crappy one.
519+
``Notepad`` on Windows is a text editor -- but a crappy one.
521520

522521
You need a real "programmers text editor"
523522

@@ -548,6 +547,10 @@ If not, I suggest ``SublimeText``: http://www.sublimetext.com/
548547

549548
http://uwpce-pythoncert.github.io/PythonResources/DevEnvironment/sublime_as_ide.html
550549

550+
"Atom" is another good open source option.
551+
552+
And, of course, vi or Emacs on Linux, if you are familiar with those.
553+
551554
Why No IDE?
552555
-----------
553556

@@ -676,7 +679,19 @@ To run it, you have a couple options:
676679
677680
$ python the_name_of_the_script.py
678681
679-
2) run ``iPython``, and run it from within iPython with the ``run`` command
682+
2) On *nix (linux, OS-X, Windows bash), you can make the file "executable"::
683+
684+
chmod +x the_file.py
685+
686+
And make sur it has a "shebang" line at the top::
687+
688+
#!/usr/bin/env python
689+
690+
Then you can run it directly::
691+
692+
./the_file.py
693+
694+
3) run ``iPython``, and run it from within iPython with the ``run`` command
680695

681696
.. code-block:: ipython
682697
@@ -706,13 +721,13 @@ Values
706721

707722
All of programming is really about manipulating values.
708723

709-
* Values are pieces of unnamed data: ``42, 'Hello, world',``
724+
* Values are pieces of unnamed data: ``42``, ``'Hello, world'``
710725

711726
* In Python, all values are objects
712727

713728
- Try ``dir(42)`` - lots going on behind the curtain!
714729

715-
* Every value belongs to a type
730+
* Every value has a type
716731

717732
- Try ``type(42)`` - the type of a value determines what it can do
718733

@@ -739,8 +754,10 @@ Boolean values:
739754
- ``True``
740755
- ``False``
741756

742-
(There are intricacies to all of these that we'll get into later)
757+
The nothing object:
758+
- ``None``
743759

760+
(There are intricacies to all of these that we'll get into later)
744761

745762
Code structure
746763
--------------
@@ -885,6 +902,7 @@ Make sure your editor is set to use spaces only --
885902

886903
Even when you hit the <tab> key
887904

905+
[Python itself allows any number of spaces (and tabs), but you are just going to confuse yourself and others if you do anything else]
888906

889907
Expressions
890908
------------
@@ -900,8 +918,7 @@ An *expression* is made up of values and operators.
900918
* Integer vs. float arithmetic
901919

902920
* (Python 3 smooths this out)
903-
* Always use ``/`` when you want float results, ``//`` when you want
904-
floored (integer) results
921+
* Always use ``/`` when you want division with float results, ``//`` when you want floored (integer) results (no remainder).
905922

906923
* Type conversions
907924

@@ -963,6 +980,7 @@ it is bound.
963980
In [26]: type(a)
964981
Out[26]: float
965982
983+
*wait!* a has a different type?!? -- yes, because it's the type of teh value: "3.14", names don't actually have a type, they can refer to any type.
966984

967985
Assignment
968986
----------
@@ -975,7 +993,6 @@ A *name* is **bound** to a *value* with the assignment operator: ``=``
975993
* A value can have many names (or none!)
976994
* Assignment is a statement, it returns no value
977995

978-
979996
.. nextslide::
980997

981998
Evaluating the name will return the value to which it is bound
@@ -1093,7 +1110,7 @@ Multiple assignment and name swapping can be very useful in certain contexts
10931110
Deleting
10941111
--------
10951112

1096-
You can't actually delete anything in python...
1113+
You can't actually directly delete values in python...
10971114

10981115
``del`` only deletes a name (or "unbinds" the name...)
10991116

@@ -1186,6 +1203,7 @@ object** using the ``is`` operator:
11861203

11871204
[demo]
11881205

1206+
**NOTE:** checking the id of an object, or using "is" to check if two objects are the same is rarely used except for debugging and understanding what's going on under the hood. They are not used regularly in production code.
11891207

11901208
Equality
11911209
--------
@@ -1206,12 +1224,34 @@ You can test for the equality of certain values with the ``==`` operator
12061224
In [81]: val1 == val3
12071225
Out[84]: False
12081226
1227+
A string is never equal to a number!
1228+
12091229
.. ifslides::
12101230

12111231
.. rst-class:: centered
12121232

12131233
[demo]
12141234

1235+
For the numerical values, there is also::
1236+
1237+
>, <, >=, <=, !=
1238+
1239+
Singletons
1240+
----------
1241+
1242+
Python has three "singletons" -- value fro which there is only one instance:
1243+
1244+
``True``, ``False``, and ``None``
1245+
1246+
To check if a name is bound to one of these, you use ``is``::
1247+
1248+
a is True
1249+
1250+
b is False
1251+
1252+
x is None
1253+
1254+
Note that in contrast to english -- "is" is asking a question, not making an assertion -- ``a is True`` means "is a the True value?"
12151255

12161256
Operator Precedence
12171257
-------------------
@@ -1222,8 +1262,11 @@ Operator Precedence determines what evaluates first:
12221262
12231263
4 + 3 * 5 != (4 + 3) * 5
12241264
1225-
To force statements to be evaluated out of order, use parentheses.
1265+
To force statements to be evaluated out of order, use parentheses -- expressions in parentheses are always evaluated first:
1266+
1267+
(4 + 3) * 5 != 4 + (3 * 5)
12261268

1269+
Python follows the "usual" rules of algebra.
12271270

12281271
Python Operator Precedence
12291272
--------------------------
@@ -1318,6 +1361,10 @@ You define a "string" value by writing a string *literal*:
13181361
In [7]: r'a "raw" string, the \n comes through as a \n'
13191362
Out[7]: 'a "raw" string, the \\n comes through as a \\n'
13201363
1364+
Python3 strings are fully support Unicode, which means that it can suport literally all the languages in the world (and then some -- Klingon, anyone?)
1365+
1366+
Because Unicode is native, you can get very far without even thinking about it. Anything you can type in your editor will work fine.
1367+
13211368

13221369
Keywords
13231370
--------
@@ -1380,7 +1427,6 @@ Try this:
13801427
'BaseException',
13811428
'BufferError',
13821429
...
1383-
'unicode',
13841430
'vars',
13851431
'xrange',
13861432
'zip']
@@ -1404,7 +1450,7 @@ You are free to rebind these names:
14041450
14051451
TypeError: 'str' object is not callable
14061452
1407-
In general, this is a **BAD IDEA**.
1453+
In general, this is a **BAD IDEA** -- hopefully your editor will warn you.
14081454

14091455

14101456
Exceptions
@@ -1419,8 +1465,11 @@ There are several exceptions that you are likely to see a lot of:
14191465
.. rst-class:: build
14201466

14211467
* ``NameError``: indicates that you have tried to use a name that is not bound to a value.
1468+
14221469
* ``TypeError``: indicates that you have tried to use the wrong kind of object for an operation.
1470+
14231471
* ``SyntaxError``: indicates that you have mis-typed something.
1472+
14241473
* ``AttributeError``: indicates that you have tried to access an attribute or
14251474
method that an object does not have (this often means you have a different
14261475
type of object than you expect)
@@ -1438,15 +1487,15 @@ A function is a self-contained chunk of code
14381487
You use them when you need the same code to run multiple times,
14391488
or in multiple parts of the program.
14401489

1441-
(DRY)
1490+
(DRY) -- "Don't Repeat Yourself"
14421491

14431492
Or just to keep the code clean
14441493

14451494
Functions can take and return information
14461495

14471496
.. nextslide::
14481497

1449-
Minimal Function does nothing
1498+
The minimal Function has at least one statement
14501499

14511500
.. code-block:: python
14521501
@@ -1455,13 +1504,14 @@ Minimal Function does nothing
14551504
14561505
.. nextslide::
14571506

1458-
Pass Statement (Note the indentation!)
1507+
Pass Statement does nothing (Note the indentation!)
14591508

14601509
.. code-block:: python
14611510
14621511
def minimal():
14631512
pass
14641513
1514+
This, or course, is not useful -- you will generally have multiple statements in a function.
14651515

14661516
Functions: ``def``
14671517
------------------
@@ -1473,6 +1523,7 @@ Functions: ``def``
14731523
* it is executed
14741524
* it creates a local name
14751525
* it does *not* return a value
1526+
14761527
.. nextslide::
14771528

14781529
function defs must be executed before the functions can be called:
@@ -1506,17 +1557,20 @@ You **call** a function using the function call operator (parens):
15061557
15071558
In [2]: type(simple)
15081559
Out[2]: function
1560+
15091561
In [3]: simple
15101562
Out[3]: <function __main__.simple>
1563+
15111564
In [4]: simple()
15121565
I am a simple function
15131566
15141567
Calling a function is how you run the code in that function.
15151568

1569+
15161570
Functions: Call Stack
15171571
---------------------
15181572

1519-
functions call functions -- this makes an execution stack -- that's all a trace back is
1573+
functions can call other functions -- this makes an execution stack -- that's what a "trace back" is:
15201574

15211575
.. code-block:: ipython
15221576
@@ -1561,7 +1615,7 @@ Functions: Tracebacks
15611615
15621616
ZeroDivisionError: integer division or modulo by zero
15631617
1564-
1618+
The error occurred in the ``doer`` function -- but the traceback shows you where that was called from. In a more complex system, this can be VERY useful -- learn to read tracebacks!
15651619

15661620
Functions: ``return``
15671621
---------------------
@@ -1620,7 +1674,7 @@ However, functions *can* return multiple results:
16201674
.. code-block:: ipython
16211675
16221676
In [16]: def fun():
1623-
....: return (1, 2, 3)
1677+
....: return 1, 2, 3
16241678
....:
16251679
In [17]: fun()
16261680
Out[17]: (1, 2, 3)
@@ -1632,7 +1686,7 @@ Remember multiple assignment?
16321686

16331687
.. code-block:: ipython
16341688
1635-
In [18]: x,y,z = fun()
1689+
In [18]: x, y, z = fun()
16361690
In [19]: x
16371691
Out[19]: 1
16381692
In [20]: y
@@ -1670,6 +1724,19 @@ When you call a function, you pass values to the function parameters as
16701724
16711725
The values you pass in are *bound* to the names inside the function and used.
16721726

1727+
The name used outside the object is separete from the name used inside teh function:
1728+
1729+
.. code-block:: python
1730+
1731+
x = 5
1732+
1733+
def fun(a):
1734+
print(a)
1735+
1736+
fun(x)
1737+
1738+
The "a" printed inside the function is the *same* object as the "x" outside the function.
1739+
16731740
The ``if`` Statement
16741741
---------------------
16751742

@@ -1723,7 +1790,7 @@ Python 2-3 Differences
17231790

17241791
Much of the example code you'll find online is Python2, rather than Python3
17251792

1726-
For the most part, they are the same -- so you can sue those examples to learn from.
1793+
For the most part, they are the same -- so you can use those examples to learn from.
17271794

17281795
There are a lot of subtle differences that you don't need to concern yourself with just yet.
17291796

@@ -1732,7 +1799,7 @@ But a couple that you'll need to know right off the bat:
17321799
print()
17331800
-------
17341801

1735-
In python2, ``print`` is a "statement", rather than a function. That means it didn't require parenthes around what you want printed::
1802+
In python2, ``print`` is a "statement", rather than a function. That means it didn't require parentheses around what you want printed::
17361803

17371804
print something, something_else
17381805

@@ -1753,7 +1820,7 @@ So -- if you get this error, simply add the parentheses::
17531820

17541821
.. nextslide:: division
17551822

1756-
In python 3, the divsion operator is "smart" when you divide integers::
1823+
In python 3, the division operator is "smart" when you divide integers::
17571824

17581825
In [17]: 1 / 2
17591826
Out[17]: 0.5

0 commit comments

Comments
 (0)