
Python Utilities - Google for Education In this section, we look at a few of Python's many standard utility modules to solve common problems. File System -- os, os.path, shutil The *os* and *os.path* modules include many functions to interact with the file system. os module docs filenames = os.listdir(dir) -- list of filenames in that directory path (not including . and ..). ## Example pulls filenames from a dir, prints their relative and absolute pathsdef printdir(dir): filenames = os.listdir(dir) for filename in filenames: print filename ## foo.txt print os.path.join(dir, filename) ## dir/foo.txt (relative to current dir) print os.path.abspath(os.path.join(dir, filename)) ## /home/nick/dir/foo.txt Exploring a module works well with the built-in python help() and dir() functions. Running External Processes -- commands The *commands* module is a simple way to run an external command and capture its output. Exceptions The try: section includes the code which might throw an exception. HTTP -- urllib and urlparse Exercise
Learn Python The Hard Way Warning If you skipped Exercise 0 then you are not doing this book right. You must read every single thing I write here and read it carefully. For example, are you trying to use Python 3 for this book? You should have spent a good amount of time in Exercise 0 learning how to install a text editor, run the text editor, run the Terminal, and work with both of them. Type the following text into a single file named ex1.py. If you are on Mac OS X then this is what your text editor might look like if you use TextWrangler: If you are on Windows using Notepad++ then this is what it would look like: Don't worry if your editor doesn't look exactly the same, it should be close though. I did not type the line numbers on the left. In Terminal run the file by typing: python ex1.py If you did it right then you should see the same output as I in the What You Should See section of this exercise. On Mac OS X in the Terminal you should see this: On Windows in PowerShell you should see this: Note Can I use IDLE?
Welcome - Learn Python - Free Interactive Python Tutorial Python Course Uta Priss This is a generic version of a class "Computer Programming for Information Management" that I taught twice a year at the School of Library and Information Science, Indiana University Bloomington. The materials in this course are mostly self-explanatory and can be used for self study. Notes: 1) The materials in this course are copyrighted. 2) Please only contact me in case you find errors in the text or have comments. 3) I will not be responsible for any possible damage that any of the scripts and exercises in this course may have. Week 1. ExercisesAnswers Week 2. Python Exercises Why Turing/Java/Python in grade 11? The choice of language for grade 11 is something I assume most schools take fairly seriously. I've been teaching 13 years and every year I review my choice of languages and ask myself if they are the best choices. Despite what you might think or hear around here I believe Turing is still a solid language in grade 11. I think most schools that teach Java in grade 11 do so because they want to use the same language for gr 11 and 12 so they can get past talking about syntax and focus on key concepts. I don't know of any other schools teaching Python in grade 11, but I wouldn't be shocked to find one. Please don't annoy/harass your teachers about their choice of programming languages, but a long as you approach them from a point of view of genuinely wanting to know why they teach language X, and have they considered language Y most teachers will be very honest with you.
Python - Operator Precedence Last updated: Sunday 5th May 2013, 16:28 PT, AD When writing Python expressions, it's usually best to indicate the order of operations by using parentheses (brackets). If parentheses are not used in an expression, the computer will perform the operations in an order determined by the precedence rules. . . See also Chapter 2.7 of this online pdf textbook. Highest precedence ( ) (anything in brackets is done first) ** (exponentiation) -x, +x relational operators: <, >, <=, >=, ! logical not logical and logical or Lowest precedence What the above means is explained in the examples below: Example 1 x = 17 / 2 * 3 + 2 print (x) In the expression to evaluate the value of x, the operators / and * share equal precedence that is higher than that of the + operator. Hence the division and multiply are done before the addition, resulting in an output of 27.5, as explained here... 17 / 2 is 8.5 8.5 * 3 is 25.5 25.5 + 2 is 27.5 Example 2 x = 2 + 17 / 2 * 3 Example 3 x = 19 % 4 + 15 / 2 * 3 19 % 4 is 3 15 / 2 is 7.5 Example 4