Popularity. You will likely encounter it. Maybe you already did and maybe that is why you are here?
Compared to many other scripting languages, it has a fairly simple syntax which encourages the writing of readable code and may be easier to learn.
Compared to other scripting languages, it has one of the most full-featured sets of tools for scientific computing: NumPy, SciPy, pandas, and matplotlib.
Comparisons and Contrasts
Matlab/Octave
- vs NumPy, SciPy, matplotlib, and scikits
R
- Can embed in IPython
- vs pandas, StatsModels, and matplotlib
Julia
- Can embed in IPython
Perl, Ruby, Scala, Clojure, Haskell, OCaml, etc...
Javascript
Consistency
- Warts in Ruby and Javascript: https://www.destroyallsoftware.com/talks/wat
Python
- Python 2 and Python 3
- CPython, IronPython, Jython, and Stackless Python
IPython
- IPython Terminal
- IPython QtConsole
- IPython Notebook
Interpreters on the Web
- repl.it: http://repl.it/languages/Python
- PythonAnywhere: https://www.pythonanywhere.com/
Running Programs
- Pitfall Warning: Explicit print vs implicit print.
Compiling Programs
- No explicit compilation. Performed on-the-fly from source into Python VM bytecode. (Note presence of .pyc and .pyo files.)
- PyPy and Nuitka
- Pyrex and Cython
- Numba
Almost everything is an object.
- Don’t worry too much about what an object is. Consider it to be a some kind of value which has some associated attributes.
- Attributes, themselves, are generally objects.
- Objects are created from types. Types themselves are objects.
- Don’t worry about object-oriented programming, if you’re not familiar with it. Existing types are flexible enough that you usually won’t have to create your own. (But, it is easy to do so if you have the need!)
Information about an object and its attributes can be found.
Important: Types are associated with values rather than variable names.
Variable names are references to values.
- References to values are created by assignment with = statement.
- References are likewise changed with = statement.
- References are deleted with del statement.
- Examples.
Pitfall Warning: Multiple named references to same sequence or mapping.
- Example.
- How to make a copy of a sequence? Several ways - more on that later.
Multiple assignment can be performed using commas as separators.
Multiple values can be swapped without explicit intermediate variables.
- Exercise: Try it!
+, -, *, /, //, %, **
Exercises:
- What happens if you add strings?
- What happens if you multiply a string, tuple, or list by an integer?
Notes on integer division vs true division.
Examples of string interpolation.
Examples of the assigning variants of these operators.
==, !=, <, <=, >=, >
is, is not, in, not in
- Notes on precedence and alternative keyword orders.
not, and, or
- Notes on “zeroish” vs “non-zeroish” values.
- Notes on short-circuiting evaluation.
~, &, |, ^, <<, >>
- Examples of the assigning variants of these operators.
Objects are instances of types.
- Instances can be created by calling types or factory functions.
- Examples.
Dot notation (.) is used to access attributes.
- Exercise: Try to add an attribute to an instance of object.
The class statement defines a new type.
- Inheritance. Old-style and new-style classes.
- Example of simple class.
- Exercise: Define a new class. Create an instance of it. Then, try to add a custom attribute to it. If successful, then try accessing that attribute.
Note on special methods with double underscores.
Indexing
- Note on zero-based indexing.
- Exercise: What happens if you use a negative index?
Slicing
- Colon notation (:) for range and stride.
- Examples.
str.split() and join()
- Examples.
str.__sizeof__()
- Notes on character width.
Creation of lists.
Length, indexing, and slicing like strings.
list.append() and list.insert()
- Exercise: Insert an item at the front of a list.
list.extend()
Item removal.
- Use del with an index or slice.
- list.pop()
- list.remove()
list.count()
list.reverse() and reversed()
list.sort() and sorted()
Creation of sets.
- Pitfall Warning: The empty set is not { }!
Length, but no indexing or slicing.
set.intersection(), set.union()
- Updating variants of these methods.
- Examples.
set.difference(), set.symmetric_difference()
- Updating variants of these methods.
- Examples.
Exercise: What do the -, &, |, and ^ operators do with sets?
Exercise: What about the assigning variants of the same?
Creation of dictionaries.
From a list of key-value pairs.
Dictionary comprehensions.
Examples.
Exercise: Create a dictionary, using a list of letters as keys and a list of numbers as values.
Indexing by key, but no slicing.
Value retrieval by indexing vs dict.get().
Testing for a key with the in operator.
Lists of keys, values, and key-value pairs.
- Views vs iterators.
frozendict
pass
def - yield - return
- Functions can return multiple values.
- Arbitrary numbers of arguments.
- Keyword arguments.
- Examples.
if - elif - else
- Examples.
for .. in - continue - break - else
- Really? An else clause with a loop? Yes.
- Examples.
while - continue - break - else
- Examples.
try - except - else - finally
- The exception hierarchy.
- Examples.
with
- Examples later.
open() and with context handler
- Modes
Iteration over lines of text file.
file.write(), file.writeline()
- division
- print_function
- absolute_import
- operator.itemgetter()
- operator.attrgetter()
- Functional forms of built-in operators.
Consider me to be a tour guide rather than an expert.
- Will highlight capabilities of various pacakges, bu have very little experience with most of them.
Copy and paste of example output.
Pop-up help.
Tab completions.
Persistent history.
Saving and restoring notebooks.
Pylab
- vs SAGE
NumPy arrays vs Python lists.
Creation of arrays.
- array
- arange
- linspace
- zeros, ones
Reshaping arrays.
eye
Element-wise operations.
Simple linear algebra.
Simple stats.
Running isympy.
Using SymPy from within an IPython GUI.
- Example.