Questions tagged [python]

Python is a multi-paradigm, dynamically typed, multipurpose programming language. It is designed to be quick to learn, understand, and use, and enforce a clean and uniform syntax. Please note that Python 2 is officially out of support as of 01-01-2020. Still, for version-specific Python questions, add the [python-2.7] or [python-3.x] tag. When using a Python variant (e.g., Jython, PyPy) or library (e.g., Pandas and NumPy), please include it in the tags.

Python is an interpreted, interactive, object-oriented (using classes), dynamic and strongly typed programming language that is used for a wide range of applications. It incorporates modules, exceptions, dynamic typing, very high-level dynamic data types, and classes. Python combines remarkable power with very clear syntax. It has interfaces to many system calls and libraries, as well as to various window systems, and is extensible in or . It is also usable as an extension language for applications that need a programmable interface. Finally, Python is portable: it runs on many Unix variants, on the Mac, and on Windows 2000 and later.

The language comes with a large standard library that covers areas such as string processing (regular expressions, Unicode, calculating differences between files), Internet protocols (HTTP, FTP, SMTP, XML-RPC, POP, IMAP, and CGI programming), software engineering (unit testing, logging, profiling, and parsing Python code), and operating system interfaces (system calls, filesystems, and TCP/IP sockets). Look at the table of contents for The Python Standard Library to get an idea of what’s available. A wide variety of third-party extensions are also available. Consult the Python Package Index to find packages of interest to you.

Python allows programmers to express concepts in fewer lines of code than would be possible in many other languages, such as C, and the language has constructs intended to be used to create clear programs in a variety of domains.

Example:

Python program

print("Hello, Stack Overflow!")

versus

C program

#include <stdio.h>
int main(void) {
    printf("Hello, Stack Overflow!");
    return 0;
}

Python was originally created by Guido van Rossum and first released in 1991. Guido Van Rossum chose Python as a working title for the project, being in a slightly irreverent mood (and a big fan of Monty Python's Flying Circus).

Python 2 (16 October 2000 - 01 January 2020) has officially been sunset, and Python 3 (3 December 2008) is the only supported, maintained and improved major version as of 01 January 2020. We have a community of people from both worlds, and if you have a question that relates to a specific version, please consider mentioning the version and implementation that you are using, when asking a question about Python (see Tagging Recommendation section below).

Python supports multiple programming paradigms, including object-oriented, imperative, and functional programming styles. It features a fully dynamic type system and automatic memory management, similar to that of Scheme, Ruby, Perl, and Tcl.

Like other dynamic languages, Python is often used as a scripting language but is also used in a wide range of non-scripting contexts. Using third-party tools, Python code can be packaged into standalone executable programs. Python interpreters are available for many operating systems.

CPython, the reference implementation of Python, is free and open-source software. It has a community-based development model, as do nearly all of its alternative implementations. There are a wide variety of implementations more suited for specific environments or tasks (see Python implementations on the Python wiki).

The philosophy of Python is succinctly formulated in The Zen of Python, written by Tim Peters, which one can read by issuing this command, in the interactive python interpreter:

>>> import this

Unlike many other languages, Python uses an indentation-based syntax (in which tabs and spaces are noninterchangeable). This may take some getting used to for programmers who are familiar with using braces.

>>> from __future__ import braces
  File "<stdin>", line 1
SyntaxError: not a chance
>>>

To help with the transition, using a properly configured text-editor or IDE is recommended. Python comes with a basic IDE called IDLE (), to get you started. Other popular examples are the charityware Vim, the free GNU Emacs, Eclipse+PyDev, or PyCharm. Take a look at this IDE comparison list for many other alternatives.

There is also a style guide for Python, named PEP 8, which aims to make Python code more readable and consistent. This guide is (should be) followed all across the Python development community.


Tagging Recommendation:

Use the tag, for all Python-related questions. If you believe your question includes issues specific to individual versions, use or , in addition to the main tag. If you believe your question maybe even more specific, you can include a version-specific tag such as or , etc.

Also, consider including the tag for the specific implementation (, , etc.), if you are using one other than — the use of is assumed unless explicitly stated otherwise.


FAQ:

These are some of the common questions many beginners face and can serve as canonical duplicate targets:

User input:

Value testing:

Common errors:

Dealing with lists:

Dealing with dicts:

General:


References:


Installation of External Packages:

  • Pip

    Most of the Python libraries used for simple and advanced scripts are downloaded using pip , the Python Package Installer.It allows you to install and manage additional packages that are not part of the Python standard library.Most distributions of Python come with pip pre-installed. General syntax:

$ pip install SomePackage
[...]
Successfully installed SomePackage
  • easy_install

    easy_install was released in 2004, as part of setuptools. It was notable at the time for installing packages from PyPI using requirement specifiers, and automatically installing dependencies. Easy Install is deprecated. Do not use it. Instead use pip.


Popular general use Python libraries:

  • Requests

    A simple Python library for making HTTP requests. Requests are marketed as being "For Humans". The library is meant to simplify and universalize Python's many methods for making HTTP requests in a way that is readable and easy to use. Functionality such as keep-alive and connection pooling are automatically handled to provide ultimate simplicity.

  • Pillow

    Pillow is described as being a "friendly fork" of the Python PIL module, an unmaintained but useful imaging library. The library uses C APIs to provide an easy Python interface to modify and manipulate image files in many different ways.

  • Scrapy

    Scrapy is a fast high-level web crawling and web scraping framework used to crawl websites and extract structured data from their pages. It can be used for a wide range of purposes, from data mining to monitoring and automated testing.

  • Beautiful Soup

    Beautiful Soup is a Python package for parsing HTML and XML documents. It creates a parse tree for parsed pages that can be used to extract data from HTML, which is useful for web scraping. It is available for Python 2.7 and Python 3.

  • nltk

    The Natural Language Toolkit, or NLTK, is a platform for building Python applications to work with human language data and the processing of sentences. It provides easy-to-use interfaces to over 50 corpora and lexical resources such as WordNet, along with a suite of text processing libraries for classification, tokenization, stemming, tagging, parsing, and semantic reasoning, and providing wrappers for industrial-strength NLP libraries.


Popular web frameworks based on Python:

If your question has anything to do with any of these frameworks, please ensure you include the appropriate tag.

  • Django

    The Web framework for perfectionists (with deadlines). Django makes it easier to build better Web apps more quickly and with less code. Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. It lets you build high-performance, elegant Web applications quickly. Django focuses on automating as much as possible and adhering to the DRY (Don't Repeat Yourself) principle.

  • Flask

    Flask is a lightweight micro-framework and third party library for Python based on Werkzeug, Jinja 2 and good intentions. It provides monolithic structure and does not enforce dependencies which allows for finer control and higher freedom for development.

  • Quart

    The quart is an evolution of the Flask API to work with Asyncio and to provide a number of features not present or possible in Flask.

  • Tornado

    Tornado is a Python web framework and asynchronous networking library. By using the non-blocking network I/O, Tornado can scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and other applications that require a long-lived connection to each user.

  • CherryPy

    CherryPy is a Pythonic, object-oriented web framework that enables developers to build web applications, in much the same way they would build any other object-oriented Python program. This results in a smaller amount of source code which is developed in less time. CherryPy has been in use for over 17 years and it is being used in production by many sites, from the simplest to the most demanding.

  • Pyramid

    A lightweight web framework emphasizing flexibility and rapid development. It combines the very best ideas from the worlds of Ruby, Python and Perl, providing a structured but extremely flexible Python web framework. It's also one of the first projects to leverage the emerging WSGI standard, which allows extensive re-use and flexibility, but only if you need it.

  • TurboGears

    TurboGears is a scalable web framework, which can go from a minimal mode setup to a full-stack web application. It was created in 2005 by Kevin Dangoor, and the current development of TurboGears2 () is being led by Mark Ramm. The current stable release of TurboGears is TurboGears 2.4.1, released September 3rd 2019

  • web.py

    web.py is a web framework for Python that is as simple as it is powerful. web.py is in the public domain: you can use it for whatever purpose with absolutely no restrictions. web.py lets you write web apps in Python.

  • Grok

    Built on the existing Zope 3 libraries but aims to provide an easier learning curve and a more agile development experience. Grok does this by placing an emphasis on convention over configuration and DRY (Don't Repeat Yourself).

  • Bottle

    The bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library.

  • web2py

    web2py is a free open source full-stack framework for rapid development of fast, scalable, secure and portable database-driven web-based applications.

  • Falcon

    Falcon is a minimal Python web framework for building microservices, app backends, and higher-level frameworks and encourages the REST architectural style. It has both community and commercial versions.

  • Twisted

    Twisted is an open-source event-driven networking engine. It is useful for implementing both clients and servers and scales up to large websites and down to embedded devices. Twisted makes it easy to implement custom network applications.


Popular Python GUI Frameworks based on Python

  • Kivy

    Kivy is an OpenGL ES 2 accelerated framework for the creation of new user interfaces. It supports multiple platforms namely Windows, Mac OS X, Linux, Android iOS, and Raspberry Pi. It is open source and comes with over 20 widgets in its toolkit.Additional Material Objects are available via KivyMD.

  • PyQT

    PyQT is one of the favoured cross-platform Python bindings implementing the Qt library for the Qt (owned by Nokia) application development framework. Currently, PyQT is available for Unix/Linux, Windows, Mac OS X, and Sharp Zaurus. It combines the best of Python and Qt and it up to the programmer to decide whether to create a program by coding or using Qt Designer to create visual dialogs.

    It is available in both commercial as well as GPL license. Although some features may not be available in the free version, if your application is open source, then you can use it under the free license.

    The latest iteration of PyQt is v5

  • Tkinter

    Tkinter is commonly bundled with Python, using Tk and is Python’s standard GUI framework. It is popular for its simplicity and graphical user interface. It is open-source and available under the Python License. One of the advantages of choosing Tkinter is that since it comes by default, there is an abundance of resources, both codes and reference books. Also with the community being old and active, there are many users who can help you out in case of questions.

  • PyGUI

    PyGUI is a graphical application cross-platform framework for Unix, Macintosh, and Windows. Compared to some other GUI frameworks, PyGUI is by far the simplest and lightweight of them all, as the API is purely in sync with Python. PyGUI inserts very little code between the GUI platform and your Python application, hence the display of the application usually displays the natural GUI of the platform.


Popular Mathematical/Scientific computing libraries in Python

  • NumPy

    NumPy is the fundamental package for scientific computing with Python. It contains among other things:

    • a powerful N-dimensional array object
    • sophisticated (broadcasting) functions
    • tools for integrating C/C++ and Fortran code
    • useful linear algebra, Fourier transform, and random number capabilities

    These features also make it possible to use NumPy in general-purpose database applications.

  • SciPy

    SciPy is an open-source library for the Python programming language, consisting of mathematical algorithms and functions often used in science and engineering. SciPy includes algorithms and tools for tasks such as optimization, clustering, discrete Fourier transforms, linear algebra, signal processing, and multi-dimensional image processing. SciPy is closely related to NumPy and depends on many NumPy functions, including a multidimensional array that is used as the basic data structure in SciPy.

  • matplotlib

    matplotlib is a plotting library for the Python programming language and its NumPy numerical mathematics extension. It provides an object-oriented API for embedding plots into applications, using general-purpose GUI toolkits like wxPython, Qt, or GTK. There is also a procedural "pylab" interface, based on a state machine (like OpenGL), designed to closely resemble that of MATLAB.

  • Pandas

    Pandas is an open-source BSD-licensed library providing high-performance, easy to use data structures and data analysis tools for the Python programming language. Pandas integrates many other libraries' features, namely NumPy's matrix operations, and Matplotlib's plotting capabilities. 10 Minutes to Pandas is a good tutorial for first exposure to Pandas.

  • theano

    Theano is a Python-C-based widely-used library suitable for highly computational mathematical tasks due to the optimizations it does on the interface Python code making it highly optimized using its C-based routines. It is a very popular library for machine-learning researchers as well. It features a highly optimized automatic differentiation, easing the implementations of highly complicated functions and computing their gradients without any errors.

  • Blender

    Blender is a free and open-source 3D animation suite. It supports the entirety of the 3D pipeline—modeling, rigging, animation, simulation, rendering, compositing and motion tracking, even video editing and game creation.

  • scikit-learn

    scikit-learn is a free and open-source machine learning library written in Python. It supports training and testing many different kinds of machine learning models, along with some basic data processing techniques.

  • TensorFlow

    TensorFlow is an open-source software library, developed by the Google Brain team. It is a symbolic math library, used mostly for machine learning applications, such as neural networks.


Popular C extension solutions:

With C extension, you can make your python code faster. If your question has anything to do with any of the next solutions, please ensure you include the appropriate tag.

  • ctypes

    ctypes is a Python package that wraps C .dll/.so libraries in pure Python.

  • SWIG

    SWIG is an interface compiler that connects programs written in C and C++ with scripting languages such as Python.

  • Cython

    Cython is an optimising static compiler for both the Python programming language and the extended Cython programming language (based on Pyrex). It makes writing C extensions for Python as easy as Python itself.


Community

Chat Rooms

  • Chat at the dedicated IRC channel #python on Freenode for all things Python. Look at Python IRC listing for a specific alternative channel, if interested.

  • Chat about Python with other Stack Overflow users in the Python chat room.

Other Sites


Free Python Programming Books


Interactive Python Learning

  • Codecademy - Learn the fundamentals of Python and dynamic programming
  • CodeSkulptor - Interactive online IDE for Python 2 programming
  • CodeSkulptor 3 - Interactive online IDE for Python 3 programming
  • Coursera - Online course for introduction to interactive Python programming
  • CheckiO - A game world you can explore, using your Python programming skills
  • Dataquest - Interactive Python courses for data science
  • PyCharm Edu - A desktop application that offers interactive Python learning
  • Interactive Python - Includes a modified, interactive version of How to Think Like a Computer Scientist
  • Python Tutor - Visualization and/or live coding in Python
  • Computer Science Circles - Learn basic Python 3 in a semi-interactive fashion.
  • CodingBat (Python) - After learning some basics, refine and hone your Python skills with live coding problems.

Python Online Courses


Python Video Tutorials


Python for Scientists


Python Online IDE

  • ideone - An online IDE, with other popular language support.
  • repl.it - Online interpreter for Python 2 and 3 that simplifies saving and sharing code.
  • python shell - Online console from PythonAnywhere.
  • pythonfiddle - Python Cloud IDE.
  • pyfiddle - Python 2.7/3.6 online console.

Code Quality

  • Codacy - Automated Code Review to ship better code, faster.
  • Codecov - Code coverage dashboard.
  • CodeFactor - Automated Code Review for Git.
  • Landscape - Hosted continuous Python code metrics.

Official Logo

Python logo


Active Podcasts

Inactive Podcasts

1729243 questions
11167
votes
38 answers

What does the "yield" keyword do?

What is the use of the yield keyword in Python, and what does it do? For example, I'm trying to understand this code1: def _get_child_candidates(self, distance, min_dist, max_dist): if self._leftchild and distance - max_dist < self._median: …
Alex. S.
  • 126,349
  • 16
  • 50
  • 61
6822
votes
37 answers

What does if __name__ == "__main__": do?

Given the following code, what does the if __name__ == "__main__": do? # Threading example import time, thread def myfunction(string, sleeptime, lock, *args): while True: lock.acquire() time.sleep(sleeptime) …
Devoted
  • 90,341
  • 41
  • 85
  • 110
6670
votes
27 answers

Does Python have a ternary conditional operator?

If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?
Devoted
  • 90,341
  • 41
  • 85
  • 110
6178
votes
21 answers

What are metaclasses in Python?

In Python, what are metaclasses and what do we use them for?
e-satis
  • 515,820
  • 103
  • 283
  • 322
6149
votes
44 answers

How do I check whether a file exists without exceptions?

How do I check whether a file exists or not, without using the try statement?
spence91
  • 67,587
  • 8
  • 25
  • 19
5650
votes
49 answers

How do I merge two dictionaries in a single expression (taking union of dictionaries)?

I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged (i.e. taking the union). The update() method would be what I need, if it returned its result instead of modifying a dictionary…
Carl Meyer
  • 105,276
  • 18
  • 102
  • 113
5310
votes
62 answers

How to execute a program or call a system command from Python

How do you call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?
freshWoWer
  • 54,641
  • 10
  • 32
  • 33
4737
votes
27 answers

How can I safely create a nested directory?

What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried: import os file_path = "/my/directory/filename.txt" directory =…
Parand
  • 91,328
  • 43
  • 147
  • 182
4124
votes
22 answers

Accessing the index in 'for' loops?

How do I access the index in a for loop like the following? ints = [8, 23, 45, 12, 78] for i in ints: print('item #{} = {}'.format(???, i)) I want to get this output: item #1 = 8 item #2 = 23 item #3 = 45 item #4 = 12 item #5 = 78 When I loop…
Joan Venge
  • 269,545
  • 201
  • 440
  • 653
4074
votes
51 answers

How to make a flat list out of a list of lists?

Is there a shortcut to make a simple list out of a list of lists in Python? I can do it in a for loop, but maybe there is some cool "one-liner"? I tried it with functools.reduce() from functools import reduce l = [[1, 2, 3], [4, 5, 6], [7], [8,…
Emma
  • 41,365
  • 4
  • 17
  • 10
3949
votes
32 answers

Difference between staticmethod and classmethod

What is the difference between a function decorated with @staticmethod and one decorated with @classmethod?
Daryl Spitzer
  • 121,723
  • 75
  • 151
  • 166
3827
votes
33 answers

Understanding slice notation

I need a good explanation (references are a plus) on Python's slice notation. To me, this notation needs a bit of picking up. It looks extremely powerful, but I haven't quite got my head around it.
Simon
  • 70,546
  • 25
  • 83
  • 117
3629
votes
33 answers

Finding the index of an item in a list

Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index (1) in Python?
Eugene M
  • 40,509
  • 14
  • 35
  • 43
3596
votes
10 answers

Does Python have a string 'contains' substring method?

I'm looking for a string.contains or string.indexof method in Python. I want to do: if not somestring.contains("blah"): continue
Blankman
  • 236,778
  • 296
  • 715
  • 1,125
3520
votes
13 answers

Iterating over dictionaries using 'for' loops

I am a bit puzzled by the following code: d = {'x': 1, 'y': 2, 'z': 3} for key in d: print (key, 'corresponds to', d[key]) What I don't understand is the key portion. How does Python recognize that it needs only to read the key from the…
TopChef
  • 36,799
  • 10
  • 25
  • 27
1
2 3
99 100