background preloader

Python

Facebook Twitter

30 Python Language Features and Tricks You May Not Know About. 1 Introduction Since I started learning Python, I decided to maintain an often visited list of "tricks".

30 Python Language Features and Tricks You May Not Know About

Any time I saw a piece of code (in an example, on Stack Overflow, in open source software, etc.) that made me think "Cool! I didn't know you could do that! " I experimented with it until I understood it and then added it to the list. This post is part of that list, after some cleaning up. Each trick or language feature is demonstrated only through examples, with no explanation. The list is very roughly ordered by difficulty, with the easier and more commonly known language features and tricks appearing first. A table of contents is given at the end. Update - April 9th, 2014 As you can see the article has been growing with currently 38 items in it, mostly thanks to comments from readers. Update - March 14th, 2014 Roy Keyes made a great suggestion of turning this article into a GitHub repository to allow readers to make improvements or additions through pull requests.

Hello World in PyCharm. Bookshelf/ 438 pages pages, May 2010 56 reviews, 3 star 0 2 star 0 1 star 0 366 pages pages, Jan 2012 436 pages pages, Apr 2013 448 pages, Feb 2015 21 reviews, 435 pages pages, Dec 2013 228 pages pages, Feb 2014 1600 pages pages, Jul 2013 455 pages pages, Jan 2010.

bookshelf/

3.5. Formatting Strings. Python supports formatting values into strings.

3.5. Formatting Strings

Although this can include very complicated expressions, the most basic usage is to insert values into a string with the %s placeholder. Example 3.21. Introducing String Formatting >>> k = "uid">>> v = "sa">>> "%s=%s" % (k, v) 'uid=sa' Note that (k, v) is a tuple. You might be thinking that this is a lot of work just to do simple string concatentation, and you would be right, except that string formatting isn't just concatenation. Writing your first Django app, part 1. Let’s learn by example.

Writing your first Django app, part 1

Throughout this tutorial, we’ll walk you through the creation of a basic poll application. It’ll consist of two parts: A public site that lets people view polls and vote in them.An admin site that lets you add, change, and delete polls. We’ll assume you have Django installed already. You can tell Django is installed and which version by running the following command: $ python -c "import django; print(django.get_version())" If Django is installed, you should see the version of your installation. This tutorial is written for Django 1.9 and Python 3.4 or later. See How to install Django for advice on how to remove older versions of Django and install a newer one. Where to get help: If you’re having trouble going through this tutorial, please post a message to django-users or drop by #django on irc.freenode.net to chat with other Django users who might be able to help.

Creating a project¶ $ django-admin startproject mysite Note Where should this code live? These files are: Troubleshooting. This page contains some advice about errors and problems commonly encountered during the development of Django applications.

Troubleshooting

“command not found: django-admin.py” django-admin.py should be on your system path if you installed Django via python setup.py. If it’s not on your path, you can find it in site-packages/django/bin, where site-packages is a directory within your Python installation. Consider symlinking to django-admin.py from some place on your path, such as /usr/local/bin. Script name may differ in distribution packages If you installed Django using a Linux distribution’s package manager (e.g. apt-get or yum) django-admin.py may have been renamed to django-admin; use that instead.

Mac OS X permissions. Installing Python on Windows — The Hitchhiker. First, download the latest version of Python 2.7 from the official Website.

Installing Python on Windows — The Hitchhiker

If you want to be sure you are installing a fully up-to-date version, click the Downloads > Windows link from the home page of the Python.org web site . The Windows version is provided as an MSI package. To install it manually, just double-click the file. The MSI package format allows Windows administrators to automate installation with their standard tools. By design, Python installs to a directory with the version number embedded, e.g. Typing the full path name for a Python interpreter each time quickly gets tedious, so add the directories for your default Python version to the PATH. C:\Python27\;C:\Python27\Scripts\ You can do this easily by running the following in powershell: [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27\;C:\Python27\Scripts\", "User") The second (Scripts) directory receives command files when certain packages are installed, so it is a very useful addition.