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
247
votes
5 answers

How often does python flush to a file?

How often does Python flush to a file? How often does Python flush to stdout? I'm unsure about (1). As for (2), I believe Python flushes to stdout after every new line. But, if you overload stdout to be to a file, does it flush as often?
Tim McJilton
  • 5,634
  • 6
  • 22
  • 27
247
votes
8 answers

What is the problem with shadowing names defined in outer scopes?

I just switched to PyCharm and I am very happy about all the warnings and hints it provides me to improve my code. Except for this one which I don't understand: This inspection detects shadowing names defined in outer scopes. I know it is bad…
Framester
  • 27,123
  • 44
  • 121
  • 183
247
votes
6 answers

Why isn't my Pandas 'apply' function referencing multiple columns working?

I have some problems with the Pandas apply function, when using multiple columns with the following dataframe df = DataFrame ({'a' : np.random.randn(6), 'b' : ['foo', 'bar'] * 3, 'c' : np.random.randn(6)}) and the…
Andy
  • 7,399
  • 10
  • 33
  • 37
247
votes
19 answers

How do I check that multiple keys are in a dict in a single pass?

I want to do something like: foo = { 'foo': 1, 'zip': 2, 'zam': 3, 'bar': 4 } if ("foo", "bar") in foo: #do stuff How do I check whether both foo and bar are in dict foo?
user131465
247
votes
8 answers

Convert python datetime to epoch with strftime

I have a time in UTC from which I want the number of seconds since epoch. I am using strftime to convert it to the number of seconds. Taking 1st April 2012 as an example. >>>datetime.datetime(2012,04,01,0,0).strftime('%s') '1333234800' 1st of April…
Noel
  • 3,132
  • 3
  • 18
  • 18
247
votes
5 answers

How to check if all elements of a list match a condition?

I have a list consisting of like 20000 lists. I use each list's 3rd element as a flag. I want to do some operations on this list as long as at least one element's flag is 0, it's like: my_list = [["a", "b", 0], ["c", "d", 0], ["e", "f", 0],…
alwbtc
  • 23,407
  • 50
  • 123
  • 177
247
votes
8 answers

How to import a Python class that is in a directory above?

I want to inherit from a class in a file that lies in a directory above the current one. Is it possible to relatively import that file?
user129975
  • 2,835
  • 3
  • 16
  • 16
246
votes
9 answers

Storing Python dictionaries

I'm used to bringing data in and out of Python using CSV files, but there are obvious challenges to this. Are there simple ways to store a dictionary (or sets of dictionaries) in a JSON or pickle file? For example: data = {} data ['key1'] =…
mike
  • 19,373
  • 28
  • 72
  • 93
246
votes
4 answers

Programmatically stop execution of python script?

Is it possible to stop execution of a python script at any line with a command? Like some code quit() # quit at this point some more code (that's not executed)
Joan Venge
  • 269,545
  • 201
  • 440
  • 653
246
votes
5 answers

Django - what is the difference between render(), render_to_response() and direct_to_template()?

Whats the difference (in language a python/django noob can understand) in a view between render(), render_to_response() and direct_to_template()? e.g. from Nathan Borror's basic apps examples def comment_edit(request, object_id,…
Ryan
  • 22,869
  • 23
  • 81
  • 126
246
votes
12 answers

How to determine the encoding of text?

I received some text that is encoded, but I don't know what charset was used. Is there a way to determine the encoding of a text file using Python? How can I detect the encoding/codepage of a text file deals with C#.
Nope
  • 29,782
  • 41
  • 91
  • 115
246
votes
18 answers

error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

https://github.com/affinelayer/pix2pix-tensorflow/tree/master/tools An error occurred when compiling "process.py" on the above site. python tools/process.py --input_dir data -- operation resize --outp ut_dir data2/resize data/0.jpg ->…
pie
  • 2,687
  • 2
  • 7
  • 6
246
votes
9 answers

pip install - locale.Error: unsupported locale setting

Full stacktrace: ➜ ~ pip install virtualenv Traceback (most recent call last): File "/usr/bin/pip", line 11, in sys.exit(main()) File "/usr/lib/python3.4/site-packages/pip/__init__.py", line 215, in main …
ericn
  • 10,647
  • 12
  • 60
  • 105
246
votes
5 answers

Why use Abstract Base Classes in Python?

Because I am used to the old ways of duck typing in Python, I fail to understand the need for ABC (abstract base classes). The help is good on how to use them. I tried to read the rationale in the PEP, but it went over my head. If I was looking for…
Muhammad Alkarouri
  • 21,347
  • 16
  • 59
  • 94
246
votes
10 answers

Safest way to convert float to integer in python?

Python's math module contain handy functions like floor & ceil. These functions take a floating point number and return the nearest integer below or above it. However these functions return the answer as a floating point number. For example: import…
Boaz
  • 21,983
  • 21
  • 65
  • 76
1 2 3
99
100