|
| 1 | +title: Python Programming Language |
| 2 | +category: page |
| 3 | +slug: python-programming-language |
| 4 | +sortorder: 0202 |
| 5 | +toc: False |
| 6 | +sidebartitle: Core Language |
| 7 | +meta: The core Python programming language includes a combination of features not found in many other languages. |
| 8 | + |
| 9 | + |
| 10 | +# Python Programming Language |
| 11 | +The Python programming language is an |
| 12 | +[open source](https://www.python.org/downloads/source/), |
| 13 | +[widely-used](/why-use-python.html) tool for |
| 14 | +creating software applications. |
| 15 | + |
| 16 | + |
| 17 | +## What is Python used for? |
| 18 | +Python is often used to [build](/web-frameworks.html) and [deploy](/deployment.html) |
| 19 | +[web applications](/web-development.html) and |
| 20 | +[web APIs](/application-programming-interfaces.html). Python |
| 21 | +can also analyze and visualize [data](/data.html) |
| 22 | +and [test software](/testing.html), even if the software being |
| 23 | +tested was not written in Python. |
| 24 | + |
| 25 | + |
| 26 | +## Language concepts |
| 27 | +Python has several useful programming language concepts that are less |
| 28 | +frequently found in other languages. These concepts include: |
| 29 | + |
| 30 | +* generators |
| 31 | +* comprehensions |
| 32 | +* [application dependency](/application-dependencies.html) handling via |
| 33 | + the built-in [venv](https://www.python.org/dev/peps/pep-0405/) |
| 34 | + ([as of Python 3.3](https://docs.python.org/3/whatsnew/3.3.html)) and |
| 35 | + [pip](https://www.python.org/dev/peps/pep-0453/) |
| 36 | + ([as of Python 3.4](https://docs.python.org/3/whatsnew/3.4.html)) |
| 37 | + commands |
| 38 | + |
| 39 | + |
| 40 | +## Generators |
| 41 | +Generators are a Python core language construct that allow a function's return |
| 42 | +value to behave as an iterator. A generator can allow more efficient |
| 43 | +memory usage by allocating and deallocating memory during the context of a |
| 44 | +large number of iterations. Generators are defined in |
| 45 | +[PEP255](https://www.python.org/dev/peps/pep-0255/) and included in the |
| 46 | +language as of Python 2.2 in 2001. |
| 47 | + |
| 48 | + |
| 49 | +## Comprehensions |
| 50 | +Comprehensions are a Python language construct for concisely creating data |
| 51 | +in lists, dictionaries and sets. List comprehensions are included in Python 2 |
| 52 | +while dictionary and set comprehensions were introduced to the language in |
| 53 | +Python 3. |
| 54 | + |
| 55 | + |
| 56 | +## Why are comprehensions important? |
| 57 | +Comprehensions are a more clear syntax for populating conditional data in the |
| 58 | +core Python data structures. Creating data without comprehensions often |
| 59 | +involves nested loops with conditionals that can be difficult for code |
| 60 | +readers to properly evaluate. |
| 61 | + |
| 62 | + |
| 63 | +## Example code |
| 64 | +List comprehension: |
| 65 | + |
| 66 | + >>> double_digit_evens = [e*2 for e in range(5, 50)] |
| 67 | + >>> double_digit_evens |
| 68 | + [10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98] |
| 69 | + |
| 70 | + |
| 71 | +Set comprehension: |
| 72 | + |
| 73 | + >>> double_digit_odds = {e*2+1 for e in range(5, 50)} |
| 74 | + {11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99} |
| 75 | + |
| 76 | +Dictionary comprehension: |
| 77 | + |
| 78 | + >>> {e: e*10 for e in range(1, 11)} |
| 79 | + {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 7: 70, 8: 80, 9: 90, 10: 100} |
| 80 | + |
| 81 | + |
| 82 | +## General Python language resources |
| 83 | +* The [online Python tutor](http://www.pythontutor.com/) visually walks |
| 84 | + through code and shows how it executes on the Python interpreter. |
| 85 | + |
| 86 | +* [Python Module of the Week](http://pymotw.com/2/index.html) is a tour |
| 87 | + through the Python standard library. |
| 88 | + |
| 89 | +* [A Python interpreter written in Python](http://aosabook.org/en/500L/a-python-interpreter-written-in-python.html) |
| 90 | + is incredibly meta but really useful for wrapping your head around some |
| 91 | + of the lower level stuff going on in the language. |
| 92 | + |
| 93 | +* [A few things to remember while coding in Python](http://satyajit.ranjeev.in/2012/05/17/python-a-few-things-to-remember.html) |
| 94 | + is a nice collection of good practices to use while building programs |
| 95 | + with the language. |
| 96 | + |
| 97 | +* [Python internals: adding a new statement to Python](http://eli.thegreenplace.net/2010/06/30/python-internals-adding-a-new-statement-to-python/) |
| 98 | + |
| 99 | +* [Python tricks that you can't live without](http://www.slideshare.net/audreyr/python-tricks-that-you-cant-live-without) |
| 100 | + is a slideshow by Audrey Roy that goes over code readability, linting, |
| 101 | + dependency isolation, and other good Python practices. |
| 102 | + |
| 103 | +* [Python innards introduction](http://tech.blog.aknin.name/2010/04/02/pythons-innards-introduction/) |
| 104 | + explains how some of Python's internal execution happens. |
| 105 | + |
| 106 | +* [What is a metaclass in Python](http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python) |
| 107 | + is one of the best Stack Overflow answers about Python. |
| 108 | + |
| 109 | +* Armin Roacher presented [things you didn't know about Python](https://speakerdeck.com/mitsuhiko/didntknow) |
| 110 | + at PyCon South Africa in 2012. |
| 111 | + |
| 112 | +* [Writing idiomatic Python](http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/) |
| 113 | + is a guide for writing Pythonic code. |
| 114 | + |
| 115 | +* [The thing that runs your Python](http://ashfall.github.io/blog/2012/10/23/the-thing-that-runs-your-python/) |
| 116 | + is a summary of what one developer learned about PyPy while researching it. |
| 117 | + |
| 118 | + |
| 119 | +## Python ecosystem resources |
| 120 | +There's an entire page on [best Python resources](/best-python-resources.html) |
| 121 | +with links but the following resources are a better fit for when you're past |
| 122 | +the very beginner topics. |
| 123 | + |
| 124 | +* [The Python Ecosystem: An Introduction](http://mirnazim.org/writings/python-ecosystem-introduction/) |
| 125 | + provides context for virtual machines, Python packaging, pip, virtualenv |
| 126 | + and many other topics after learning the basic Python syntax. |
| 127 | + |
| 128 | +* The [Python Subreddit](http://www.reddit.com/r/python) rolls up great |
| 129 | + Python links and has an active community ready to answer questions from |
| 130 | + beginners and advanced Python developers alike. |
| 131 | + |
| 132 | +* The blog [Free Python Tips](http://freepythontips.wordpress.com/) provides |
| 133 | + posts on Python topics as well as news for the Python ecosystem. |
| 134 | + |
| 135 | +* [Python Books](http://pythonbooks.revolunet.com/) is a collection of freely |
| 136 | + available books on Python, Django, and data analysis. |
| 137 | + |
| 138 | +* [Python IAQ: Infrequently Asked Questions](http://norvig.com/python-iaq.html) |
| 139 | + is a list of quirky queries on rare Python features and why certain syntax |
| 140 | + was or was not built into the language. |
| 141 | + |
| 142 | +* [A practical introduction to Functional Programming for Python coders](https://codesachin.wordpress.com/2016/04/03/a-practical-introduction-to-functional-programming-for-python-coders/) |
| 143 | + is a good starter for developers looking to learn the functional |
| 144 | + programming paradigm side of the language. |
| 145 | + |
| 146 | +* [Getting Started with the Python Internals](http://akaptur.com/blog/2014/08/03/getting-started-with-python-internals/) |
| 147 | + takes a slice of the huge CPython codebase and deconstructs some of |
| 148 | + it to see what we can learn about how Python itself is built. |
| 149 | + |
| 150 | + |
| 151 | +### Comprehension resources |
| 152 | +* [Comprehending Python’s Comprehensions](https://dbader.org/blog/list-dict-set-comprehensions-in-python#intro) |
| 153 | + is an awesome post by Dan Bader with a slew of examples that explain |
| 154 | + how list, dictionary and set comprehensions should be used. |
| 155 | + |
| 156 | +* [Python List Comprehensions: Explained Visually](http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/) |
| 157 | + explains how the common idiom for iteration became syntactic sugar in |
| 158 | + the language itself and how you can use it in your own programs. |
| 159 | + |
| 160 | +* The Python 3 Patterns and Idioms site has an overview of |
| 161 | + [comprehensions](http://python-3-patterns-idioms-test.readthedocs.org/en/latest/Comprehensions.html) |
| 162 | + including code examples and diagrams to explain how they work. |
| 163 | + |
| 164 | +* [Comprehensions in Python the Jedi way](https://gist.github.com/bearfrieze/a746c6f12d8bada03589) |
| 165 | + shows off comprehensions with a Star Wars theme to walk through the nuts |
| 166 | + and bolts. All examples use Python 3.5. |
| 167 | + |
| 168 | +* [Idiomatic Python: Comprehensions](https://blogs.msdn.microsoft.com/pythonengineering/2016/03/14/idiomatic-python-comprehensions/) |
| 169 | + explains how Python's comprehensions were inspired by Haskell's list |
| 170 | + comprehensions. It also provides clear examples that show how comprehensions |
| 171 | + are shorthand for common iteration code, such as copying one list into |
| 172 | + another while performing some operation on the contained elements. |
| 173 | + |
| 174 | +* [Learning Python by example: list comprehensions](http://blog.cdleary.com/2010/04/learning-python-by-example-list-comprehensions/) |
| 175 | + gives an example of an incorrect list comprehension then shows how to |
| 176 | + correct its issues. |
| 177 | + |
| 178 | +* [List comprehensions in Python](http://www.pythonforbeginners.com/basics/list-comprehensions-in-python) |
| 179 | + covers what the code for list comprehensions looks like and gives some |
| 180 | + example code to show how they work. |
| 181 | + |
| 182 | +* [An Introduction to Python Lists](http://effbot.org/zone/python-list.htm) |
| 183 | + is a solid overview of Python lists in general and tangentially covers |
| 184 | + list comprehensions. |
| 185 | + |
| 186 | + |
| 187 | +### Python generator resources |
| 188 | +* This blog post entitled |
| 189 | + [Python Generators](http://rdrewd.blogspot.com/2014/02/python-generators.html) |
| 190 | + specifically focuses on generating dictionaries. It provides a good |
| 191 | + introduction for those new to Python. |
| 192 | + |
| 193 | +* [Generator Expressions in Python: An Introduction](https://dbader.org/blog/python-generator-expressions#intro) |
| 194 | + is the best all-around introduction to how to use generators and |
| 195 | + provides numerous code examples to learn from. |
| 196 | + |
| 197 | +* [Python 201: An Intro to Generators](http://www.blog.pythonlibrary.org/2014/01/27/python-201-an-intro-to-generators/) |
| 198 | + is another short but informative read with example generators code. |
| 199 | + |
| 200 | +* [Iterators & Generators](http://anandology.com/python-practice-book/iterators.html) |
| 201 | + provides code examples for these two constructs and some simple explanations |
| 202 | + for each one. |
| 203 | + |
| 204 | +* [Python: Generators - How to use them and the benefits you receive](https://www.youtube.com/watch?v=bD05uGo_sVI) |
| 205 | + is a screencast with code that walks through generators in Python. |
| 206 | + |
| 207 | +* The question to [Understanding Generators in Python?](http://stackoverflow.com/questions/1756096/understanding-generators-in-python) |
| 208 | + on Stack Overflow has an impressive answer that clearly lays out the |
| 209 | + code and concepts involved with Python generators. |
| 210 | + |
| 211 | +* [Generator Tricks for Systems Programmers](http://www.dabeaz.com/generators/) |
| 212 | + provides code examples for using generators. The material was originally |
| 213 | + presented in a PyCon workshop for systems programmers but is relevant to |
| 214 | + all Python developers working to understand appropriate ways to use |
| 215 | + generators. |
| 216 | + |
0 commit comments