background preloader

Google Python Class Day 2 Part 2

Google Python Class Day 2 Part 2
Related:  Google Python Class

Python Utilities - Educational Materials 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. The *shutil* module can copy files. 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. Exercise

s Python Class - Google's Python Class - Google Code Welcome to Google's Python Class -- this is a free class for people with a little bit of programming experience who want to learn Python. The class includes written materials, lecture videos, and lots of code exercises to practice Python coding. These materials are used within Google to introduce Python to people who have just a little programming experience. To get started, the Python sections are linked at the left -- Python Set Up to get Python installed on your machine, Python Introduction for an introduction to the language, and then Python Strings starts the coding material, leading to the first exercise. This material was created by Nick Parlante working in the engEDU group at Google. Tip: Check out the Python Google Code University Forum to ask and answer questions.

Apprendre à programmer avec Python Un livre de Wikilivres. Ce livre a été rédigé par Gérard Swinnen qui le met à disposition sur son site personnel [archive] en licence GFDL. À faire... formater tous les chapitres selon le livre imprimé importer les captures d'écran [1] demander à l'auteur la licence des schémas intégrer les exercices éradiquer les {{todo}}lier les pages ( Non : attendre la spécification et l'implémentation de css3)établir l'indexétablir un glossaire Sommaire[modifier | modifier le wikitexte] Voir aussi[modifier | modifier le wikitexte] Programmation Python

Free Classes. Awesome Instructors. Inspiring Community. When does the course begin? This class is self paced. You can begin whenever you like and then follow your own pace. It’s a good idea to set goals for yourself to make sure you stick with the course. How long will the course be available? This class will always be available! How do I know if this course is for me? Take a look at the “Class Summary,” “What Should I Know,” and “What Will I Learn” sections above. Can I skip individual videos? Yes! How much does this cost? It’s completely free! What are the rules on collaboration? Collaboration is a great way to learn. Why are there so many questions? Udacity classes are a little different from traditional courses. What should I do while I’m watching the videos? Learn actively!

Python Regular Expressions - Educational Materials Regular expressions are a powerful language for matching text patterns. This page gives a basic introduction to regular expressions themselves sufficient for our Python exercises and shows how regular expressions work in Python. The Python "re" module provides regular expression support. In Python a regular expression search is typically written as: match = re.search(pat, str) The re.search() method takes a regular expression pattern and a string and searches for that pattern within the string. str = 'an example word:cat!!' The code match = re.search(pat, str) stores the search result in a variable named "match". The 'r' at the start of the pattern string designates a python "raw" string which passes through backslashes without change which is very handy for regular expressions (Java needs this feature badly!). Basic Patterns The power of regular expressions is that they can specify patterns, not just fixed characters. a, X, 9, < -- ordinary characters just match themselves exactly. findall

Learn Python in 10 minutes | Stavros' Stuff NOTE: If you would like some Python development done, my company, Stochastic Technologies, is available for consulting. This tutorial is available as a short ebook. The e-book features extra content from follow-up posts on various Python best practices, all in a convenient, self-contained format. Preliminary fluff So, you want to learn the Python programming language but can't find a concise and yet full-featured tutorial. Properties Python is strongly typed (i.e. types are enforced), dynamically, implicitly typed (i.e. you don't have to declare variables), case sensitive (i.e. var and VAR are two different variables) and object-oriented (i.e. everything is an object). Getting help Help in Python is always available right in the interpreter. >>> help(5)Help on int object:(etc etc) >>> dir(5)['__abs__', '__add__', ...] >>> abs. Syntax Python has no mandatory statement termination characters and blocks are specified by indentation. Data types You can access array ranges using a colon (:).

Dive Into Python 3 You are here: • Dive Into Python 3 Dive Into Python 3 covers Python 3 and its differences from Python 2. Table of Contents (expand) Also available on dead trees! The book is freely licensed under the Creative Commons Attribution Share-Alike license. you@localhost:~$ git clone © 2001–11 Mark Pilgrim

Electrical Engineering and Computer Science | 6.00 Introduction to Computer Science and Programming, Fall 2008 | Video Lectures Python Dict and File - Educational Materials Dict Hash Table Looking up or setting a value in a dict uses square brackets, e.g. dict['foo'] looks up the value under the key 'foo'. Strings, numbers, and tuples work as keys, and any type can be a value. Other types may or may not work correctly as keys (strings and tuples work cleanly since they are immutable). Looking up a value which is not in the dict throws a KeyError -- use "in" to check if the key is in the dict, or use dict.get(key) which returns the value or None if the key is not present (or get(key, not-found) allows you to specify what value to return in the not-found case). print dict['a'] ## Simple lookup, returns 'alpha' dict['a'] = 6 ## Put new key/value into dict 'a' in dict ## True ## print dict['z'] ## Throws KeyError if 'z' in dict: print dict['z'] ## Avoid KeyError print dict.get('z') ## None (instead of KeyError) A for loop on a dictionary iterates over its keys by default. ## Get the .keys() list: print dict.keys() ## ['a', 'o', 'g'] Dict Formatting Del Files

The Lair of the Python Welcome to the home of the Voidspace Python pages. Not a snake, Python is a dynamic scripting language with a beautifully clean syntax. There's lots of code here to peruse, as well as the Techie Blog which explores life from the point of view of a Pythoneer. Python is a dynamic, object orientated, programming language. Its focus is on allowing the programmer the maximum freedom and expressiveness. Because it's easy to read, it is also easy to learn. In these pages you can find all my Python related stuff. I currently work for a firm called Resolver Systems. You can find more information about IronPython, including a tutorial series on IronPython and Windows Forms (the .NET GUI toolkit for rich client applications) on the IronPython Pages. A programming language is a medium of expression - Paul Graham The programs page contains links to, and descriptions of, the handful of complete programs that I've written. The modules page has links to all the python modules I've written.

regex search and replace example scripts - Python Testing Search and replace is such a common task that it should be a tool that’s in every command line script author’s toolbox. There are probably endless solutions to the problem. I’ve put together my standard methods for tackling the problem. I’ll also show similar Perl versions, mainly for comparisons. use models In most of the following discussion, I’m just replacing ‘foo’ with ‘bar’.However ‘foo’ can be ANY regular expression. The dummy situation I’m going to set up is this. foo foo fiddle foo and baz too, as well as foo I realize that I want to replace ‘foo’ with ‘bar’. I’d like to be able to have a script that runs in a way that I can either pipe the contents of the file into the script, or just give a file name to the script, like this: cat example.txt | python search_replace.pypython search_replace.py example.txt Now, I may or may not want to have the script modify the file in place. using Python Search and replace as a filter This handles the case where we don’t want to modify the file. #!

Python Sorting - Educational Materials The easiest way to sort is with the sorted(list) function, which takes a list and returns a new list with those elements in sorted order. The original list is not changed. a = [5, 1, 4, 3] print sorted(a) ## [1, 3, 4, 5] print a ## [5, 1, 4, 3] It's most common to pass a list into the sorted() function, but in fact it can take as input any sort of iterable collection. The sorted() function can be customized though optional arguments. strs = ['aa', 'BB', 'zz', 'CC'] print sorted(strs) ## ['BB', 'CC', 'aa', 'zz'] (case sensitive) print sorted(strs, reverse=True) ## ['zz', 'aa', 'CC', 'BB'] Custom Sorting With key= For more complex custom sorting, sorted() takes an optional "key=" specifying a "key" function that transforms each element before comparison. For example with a list of strings, specifying key=len (the built in len() function) sorts the strings by length, from shortest to longest. strs = ['ccc', 'aaaa', 'd', 'bb'] print sorted(strs, key=len) ## ['d', 'bb', 'ccc', 'aaaa'] Tuples

Related: