You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: slides_sources/source/session01.rst
+93-26Lines changed: 93 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -332,7 +332,7 @@ Windows provides the "DOS" command line, which is OK, but pretty old and limited
332
332
333
333
If you are comfortable with either of these -- go for it.
334
334
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>`_
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.
361
360
362
361
http://uwpce-pythoncert.github.io/PythonResources
363
362
@@ -381,7 +380,7 @@ Step 1: Python 3
381
380
.. code-block:: bash
382
381
383
382
$ 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)
385
384
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
386
385
Type "help", "copyright", "credits" or "license"for more information.
387
386
>>>
@@ -517,7 +516,7 @@ MS Word is **not** a text editor.
517
516
518
517
Nor is *TextEdit* on a Mac.
519
518
520
-
``Notepad`` is a text editor -- but a crappy one.
519
+
``Notepad`` on Windows is a text editor -- but a crappy one.
521
520
522
521
You need a real "programmers text editor"
523
522
@@ -548,6 +547,10 @@ If not, I suggest ``SublimeText``: http://www.sublimetext.com/
And, of course, vi or Emacs on Linux, if you are familiar with those.
553
+
551
554
Why No IDE?
552
555
-----------
553
556
@@ -676,7 +679,19 @@ To run it, you have a couple options:
676
679
677
680
$ python the_name_of_the_script.py
678
681
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
680
695
681
696
.. code-block:: ipython
682
697
@@ -706,13 +721,13 @@ Values
706
721
707
722
All of programming is really about manipulating values.
708
723
709
-
* Values are pieces of unnamed data: ``42, 'Hello, world',``
724
+
* Values are pieces of unnamed data: ``42``, ``'Hello, world'``
710
725
711
726
* In Python, all values are objects
712
727
713
728
- Try ``dir(42)`` - lots going on behind the curtain!
714
729
715
-
* Every value belongs to a type
730
+
* Every value has a type
716
731
717
732
- Try ``type(42)`` - the type of a value determines what it can do
718
733
@@ -739,8 +754,10 @@ Boolean values:
739
754
- ``True``
740
755
- ``False``
741
756
742
-
(There are intricacies to all of these that we'll get into later)
757
+
The nothing object:
758
+
- ``None``
743
759
760
+
(There are intricacies to all of these that we'll get into later)
744
761
745
762
Code structure
746
763
--------------
@@ -885,6 +902,7 @@ Make sure your editor is set to use spaces only --
885
902
886
903
Even when you hit the <tab> key
887
904
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]
888
906
889
907
Expressions
890
908
------------
@@ -900,8 +918,7 @@ An *expression* is made up of values and operators.
900
918
* Integer vs. float arithmetic
901
919
902
920
* (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).
905
922
906
923
* Type conversions
907
924
@@ -963,6 +980,7 @@ it is bound.
963
980
In [26]: type(a)
964
981
Out[26]: float
965
982
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.
966
984
967
985
Assignment
968
986
----------
@@ -975,7 +993,6 @@ A *name* is **bound** to a *value* with the assignment operator: ``=``
975
993
* A value can have many names (or none!)
976
994
* Assignment is a statement, it returns no value
977
995
978
-
979
996
.. nextslide::
980
997
981
998
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
1093
1110
Deleting
1094
1111
--------
1095
1112
1096
-
You can't actually delete anything in python...
1113
+
You can't actually directly delete values in python...
1097
1114
1098
1115
``del`` only deletes a name (or "unbinds" the name...)
1099
1116
@@ -1186,6 +1203,7 @@ object** using the ``is`` operator:
1186
1203
1187
1204
[demo]
1188
1205
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.
1189
1207
1190
1208
Equality
1191
1209
--------
@@ -1206,12 +1224,34 @@ You can test for the equality of certain values with the ``==`` operator
1206
1224
In [81]: val1 == val3
1207
1225
Out[84]: False
1208
1226
1227
+
A string is never equal to a number!
1228
+
1209
1229
.. ifslides::
1210
1230
1211
1231
.. rst-class:: centered
1212
1232
1213
1233
[demo]
1214
1234
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?"
1215
1255
1216
1256
Operator Precedence
1217
1257
-------------------
@@ -1222,8 +1262,11 @@ Operator Precedence determines what evaluates first:
1222
1262
1223
1263
4+3*5!= (4+3) *5
1224
1264
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)
1226
1268
1269
+
Python follows the "usual" rules of algebra.
1227
1270
1228
1271
Python Operator Precedence
1229
1272
--------------------------
@@ -1318,6 +1361,10 @@ You define a "string" value by writing a string *literal*:
1318
1361
In [7]: r'a "raw" string, the \n comes through as a \n'
1319
1362
Out[7]: 'a "raw" string, the \\n comes through as a \\n'
1320
1363
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
+
1321
1368
1322
1369
Keywords
1323
1370
--------
@@ -1380,7 +1427,6 @@ Try this:
1380
1427
'BaseException',
1381
1428
'BufferError',
1382
1429
...
1383
-
'unicode',
1384
1430
'vars',
1385
1431
'xrange',
1386
1432
'zip']
@@ -1404,7 +1450,7 @@ You are free to rebind these names:
1404
1450
1405
1451
TypeError: 'str' object is not callable
1406
1452
1407
-
In general, this is a **BAD IDEA**.
1453
+
In general, this is a **BAD IDEA** -- hopefully your editor will warn you.
1408
1454
1409
1455
1410
1456
Exceptions
@@ -1419,8 +1465,11 @@ There are several exceptions that you are likely to see a lot of:
1419
1465
.. rst-class:: build
1420
1466
1421
1467
* ``NameError``: indicates that you have tried to use a name that is not bound to a value.
1468
+
1422
1469
* ``TypeError``: indicates that you have tried to use the wrong kind of object for an operation.
1470
+
1423
1471
* ``SyntaxError``: indicates that you have mis-typed something.
1472
+
1424
1473
* ``AttributeError``: indicates that you have tried to access an attribute or
1425
1474
method that an object does not have (this often means you have a different
1426
1475
type of object than you expect)
@@ -1438,15 +1487,15 @@ A function is a self-contained chunk of code
1438
1487
You use them when you need the same code to run multiple times,
1439
1488
or in multiple parts of the program.
1440
1489
1441
-
(DRY)
1490
+
(DRY) -- "Don't Repeat Yourself"
1442
1491
1443
1492
Or just to keep the code clean
1444
1493
1445
1494
Functions can take and return information
1446
1495
1447
1496
.. nextslide::
1448
1497
1449
-
Minimal Function does nothing
1498
+
The minimal Function has at least one statement
1450
1499
1451
1500
.. code-block:: python
1452
1501
@@ -1455,13 +1504,14 @@ Minimal Function does nothing
1455
1504
1456
1505
.. nextslide::
1457
1506
1458
-
Pass Statement (Note the indentation!)
1507
+
Pass Statement does nothing (Note the indentation!)
1459
1508
1460
1509
.. code-block:: python
1461
1510
1462
1511
defminimal():
1463
1512
pass
1464
1513
1514
+
This, or course, is not useful -- you will generally have multiple statements in a function.
1465
1515
1466
1516
Functions: ``def``
1467
1517
------------------
@@ -1473,6 +1523,7 @@ Functions: ``def``
1473
1523
* it is executed
1474
1524
* it creates a local name
1475
1525
* it does *not* return a value
1526
+
1476
1527
.. nextslide::
1477
1528
1478
1529
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):
1506
1557
1507
1558
In [2]: type(simple)
1508
1559
Out[2]: function
1560
+
1509
1561
In [3]: simple
1510
1562
Out[3]: <function __main__.simple>
1563
+
1511
1564
In [4]: simple()
1512
1565
I am a simple function
1513
1566
1514
1567
Calling a function is how you run the code in that function.
1515
1568
1569
+
1516
1570
Functions: Call Stack
1517
1571
---------------------
1518
1572
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:
1520
1574
1521
1575
.. code-block:: ipython
1522
1576
@@ -1561,7 +1615,7 @@ Functions: Tracebacks
1561
1615
1562
1616
ZeroDivisionError: integer division or modulo by zero
1563
1617
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!
1565
1619
1566
1620
Functions: ``return``
1567
1621
---------------------
@@ -1620,7 +1674,7 @@ However, functions *can* return multiple results:
0 commit comments