Up: Lectures

Python

This lecture is a fast-paced introduction to Python, a scripting language that is increasingly popular in computational science.

Requires: some previous experience of programming, and know at least a little about loops, lists, if statements, functions, and file I/O.

Introduces: Python language basics

  • Introduction (pdf, ppt)
    • Human time vs. machine time
    • Why Python
  • Basics (pdf, ppt)
    • Running Python
    • Variables and values
    • Numbers and arithmetic
    • In-place operators
    • Comparisons
  • Control Flow (pdf, ppt)
    • While loops
    • Conditionals
  • Lists (pdf, ppt)
    • Mutable and heterogeneous
    • Creating, getting, and setting
    • Negative indices
    • For loops
    • Deleting and appending
    • Methods
    • Ranges
  • Input and Output (pdf, ppt)
    • Opening and reading files
    • Looping over file contents
    • Writing to files
    • Several ways to copy a file
  • Strings (pdf, ppt)
    • Text vs. binary data
    • Indexing
    • Quoting
    • Escape characters
    • Immutability
    • Concatenation
    • Formatting
    • Methods
  • Aliasing (pdf, ppt)
    • Definition
    • Irrelevant for immutable values
    • List example
    • Example aliasing bug
  • Functions (pdf, ppt)
    • Defining functions
    • Parameters
    • The call stack and stack frames
    • The return statement
    • Copying and aliasing
    • Default parameter values
    • When and why to write functions
  • First-Class Functions (pdf, ppt)
    • Binding functions to variables
    • Passing functions to functions
    • Re-using control flow
    • Programming as creating and combining abstractions
  • Libraries (pdf, ppt)
    • Files as modules
    • The import statement
    • Namespaces
    • The math library
    • The sys library
    • Docstrings
    • if __name__ == '__main__'
  • Tuples (pdf, ppt)
    • Have to wait to see why they’re necessary
    • Creating and indexing
    • Multi-valued assignment
    • Unpacking lists
  • Slicing (pdf, ppt)
    • Slicing vs. indexing
    • Silent truncation
    • Aliasing
  • Text (pdf, ppt)
    • How lines are stored
    • How characters are stored
    • Unicode and UTF-8
    • Why you care

Exercises

Reading

  1. Matt Savoie
    November 10th, 2010 at 16:19 | #1

    Great quick little python overview. Thanks.

  2. Tanya
    January 27th, 2011 at 13:47 | #2

    first = first.append(‘newton’) – slide #52 in the pdf file
    This should produce “None”?

  3. luis
    January 27th, 2011 at 14:19 | #3

    @Tanya
    Heh, which of the pdf files?
    I’m assuming is the “aliasing” episode – if not, please correct me.
    You are correct. As written there, first should have the value of None, and most operations over that variable would fail. And there would be no aliasing, with first and second referring to different objects (second to a list, and first to None).

    I believe that (those) slide(s) should read:

    first = ['isaac']
    second = first
    first.append(‘newton’)

    With no assignment after the append()

    Thanks for noticing.

    Luis.

  4. Tanya
    January 28th, 2011 at 18:06 | #4

    @luis
    Thanks! You are right – it’s the pdf for the “Aliasing” lecture.

  5. Tobias
    February 9th, 2011 at 17:22 | #5

    Wow! Just had a look at your site. I must say, these tutorials are as good as it gets.
    Keep it up!

  6. Marden
    March 28th, 2012 at 02:18 | #6

    Would it be possible to add the length of each video lecture next to the title of each lecture. It would help in many ways, as Mark mentioned it allows us to know how much time will we need to go over a lecture and to plan according to that, also I think it would help motivate people.

    Thank you for this great website

  7. Cae
    May 6th, 2012 at 12:28 | #7

    Any chance of updating the lectures in the near future with python 3?

    • Greg Wilson
      May 6th, 2012 at 12:52 | #8

      Hi Cae,
      Software Carpentry is an open source project: we would welcome updates from you or anyone else.
      Thanks,
      Greg

  1. October 22nd, 2010 at 14:54 | #1
  2. January 31st, 2012 at 21:45 | #2
  3. February 27th, 2012 at 18:14 | #3
  4. April 13th, 2012 at 13:41 | #4