background preloader

Python Cheat Sheet

Python Cheat Sheet
String String Methods Array Indexes and Slices a=[0,1,2,3,4,5] 6 len(a) 0 a[0] 5 a[5] 5 a[-1] 4 a[-2] [1,2,3,4,5] a[1:] [0,1,2,3,4] a[:5] [0,1,2,3] a[:-2] [1,2] a[1:3] [1,2,3,4] a[1:-1] Shallow copy of a b=a[:] Math Constants math.pi The mathematical constant π = 3.141592..., to available precision. math.e The mathematical constant e = 2.718281..., to available precision. Random Functions Sys Sys Variables argv Command line args builtin_module_names Linked C modules byteorder Native byte order check_-interval Signal check frequency exec_prefix Root directory executable Name of executable exitfunc Exit function name modules Loaded modules path Search path platform Current platform stdin, stdout, stderr File objects for I/O version_info Python version info winver Version number sys.argv sys.argv[0] bar sys.argv[1] -c sys.argv[2] qux sys.argv[3] --h sys.argv[4] os Variables Class Special Methods String Formatting Formatting Operations 'd' Signed integer decimal. Date Formatting Date Formatting Ad

Related:  Getting started with PythonpythonProgrammation PythonHelp and Tips

[Resource] Learning Python A lot of people who join this course might not be that familiar with Python, or not be familiar with Python at all. So for those students this is a list of places where they can learn a bit about Python. Udacity CS101 - I took it in February & trust me it's worth it, even if you know Python.Learn Python The Hard Way - The hard way is the right way, and in the long run the easy way. galerie complete en ipython notebook Wiki This page is a curated collection of IPython notebooks that are notable for some reason. Feel free to add new content here, but please try to only include links to notebooks that include interesting visual or technical content; this should not simply be a dump of a Google search on every ipynb file out there. Important contribution instructions: If you add new content, please ensure that for any notebook you link to, the link is to the rendered version using nbviewer, rather than the raw file.

The definitive guide on how to use static, class or abstract methods in Python Doing code reviews is a great way to discover things that people might struggle to comprehend. While proof-reading OpenStack patches recently, I spotted that people were not using correctly the various decorators Python provides for methods. So here's my attempt at providing me a link to send them to in my next code reviews. :-) A method is a function that is stored as a class attribute. You can declare and access such a function this way:

10. Brief Tour of the Standard Library — Python v2.7.5 documentation 10.1. Operating System Interface The os module provides dozens of functions for interacting with the operating system: >>> import os>>> os.getcwd() # Return the current working directory'C:\\Python26'>>> os.chdir('/server/accesslogs') # Change current working directory>>> os.system('mkdir today') # Run the command mkdir in the system shell0 Be sure to use the import os style instead of from os import *. This will keep from shadowing the built-in open() function which operates much differently.

A Note on Python Paths This time I decided to share some knowledge about Python paths which seemed a little bit confusing to me in the beginning of diving into Python. I am working with Django in different platforms like Mac OS X, Windows, and Linux, therefore the common patterns how to activate new python modules in all of those environments should be familiar to me. Python modules are either *.py files or directories containing . When defining paths to python modules, you will usually need to deal with the latter ones. IDE Python Professional Edition Free 30-day trial Full-featured IDE for Python & Web development Supports Django, Flask, Google App Engine, Pyramid, web2py JavaScript, CoffeeScript, TypeScript, CSS, Cython, Template languages and more Remote development, Python Profiler, Databases and SQL support, UML & SQLAlchemy Diagrams PyCharm Professional Edition includes bundled evaluation license key for a free 30-day trial. Community Edition FREE

Closures In Python You might have lived a long and happy coding life without ever needing to know what closures are. For one, many languages like C or C++ don't even support those (edit: C++ does support them via lambdas, of course. Sorry for any misinformation). Software: Python Sidebar When programming Python, I tend to visit the most current reference documentation quite often. To get faster and more convenient access to the documentation, inspired by Mark Hammond’s sidebar, I wrote an updated sidebar for the Mozilla family of web browser. The sidebar also offers simple searching of related python websites and mailing lists. It might be semi-useful in other browsers as well, but the main target here is Mozilla.

Using .pth files for Python development Python's site module is responsible for setting up the interpreter's environment upon startup. One of the things it does during startup is scan your site directories (typically just site-packages , but framework builds for Mac OS X have an additional undocumented default location) for .pth files. .pth files are used to add additional locations sys.path , and they are typically created by distutils setup scripts that use the (still undocumented) extra_path argument. .pth files are a great alternative to using the PYTHONPATH environment variable: You don't have to screw with your environment (which can be difficult on Mac OS X and Windows). The effect is localized to a particular Python installation. You can toggle their usage simply by moving them around on the filesystem.

Related:  ProgrammingPython