Python

Python is a dynamic, strongly typed, object-oriented, multipurpose programming language, designed to be quick (to learn, to use, and to understand), and to enforce a clean and uniform syntax. Two similar but incompatible versions of Python are in use (Python 2.7 or 3.x). For version-specific Python questions, please also use the [python-2.7] or [python-3.x] tags. When using a Python variant (Jython, Pypy, Iron-python, etc.) - please also tag the variant.

63 votes
13 answers
2.6K views
How to comment out a block of Python code in Vim

I was wondering if there was any key mapping in Vim to allow me to indent certain lines of code (whether those

avatar
Luis
0 1 0 9
asked 07 September, 2021
52 votes
3 answers
1.2K views
Why does Python's hash of infinity have the digits of π?

The hash of infinity in Python has digits matching pi: >>> inf = float('inf') >>>

avatar
Xavier
0 1 0 9
asked 07 September, 2021
58 votes
11 answers
507 views
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

avatar
Karolyn
0 1 0 9
asked 07 September, 2021
61 votes
13 answers
1.2K views
Calculating arithmetic mean (one type of average) in Python

Is there a built-in or standard library method in Python to calculate the arithmetic mean (one type of average

avatar
Juliane
0 1 0 9
asked 07 September, 2021
56 votes
18 answers
807 views
Convert a number range to another range, maintaining ratio

I'm trying to convert one range of numbers to another, maintaining ratio. Maths is not my strong point.

avatar
Shanita
0 1 0 9
asked 07 September, 2021
67 votes
30 answers
1.2K views
Fastest way to list all primes below N

This is the best algorithm I could come up. def get_primes(n): numbers = set(range(n, 1, -1)) primes

avatar
Alba
0 1 0 9
asked 07 September, 2021
59 votes
10 answers
1.6K views
What is the standard way to add N seconds to datetime.time in Python?

Given a datetime.time value in Python, is there a standard way to add an integer number of seconds to it, so t

avatar
Janella
0 1 0 9
asked 07 September, 2021
66 votes
16 answers
2.9K views
What is the difference between '/' and '//' when used for division?

Is there a benefit to using one over the other? In Python 2, they both seem to return the same results: >&

avatar
Thurman
0 1 0 9
asked 07 September, 2021
53 votes
18 answers
786 views
How can I check for NaN values?

float('nan') results in Nan (not a number). But how do I check for it? Should be very easy, but I ca

avatar
Dwain
0 1 0 9
asked 07 September, 2021
58 votes
9 answers
1.9K views
How to state in requirements.txt a direct github source

I've installed a library using the command pip install git+git://github.com/mozilla/elasticutils.git whi

avatar
Keila
0 1 0 9
asked 07 September, 2021
51 votes
26 answers
319 views
How do I connect to a MySQL Database in Python?

How do I connect to a MySQL database using a python program?

avatar
Dorinda
0 1 0 9
asked 07 September, 2021
61 votes
11 answers
521 views
Installing specific package versions with pip

I'm trying to install version 1.2.2 of the MySQL_python adaptor, using a fresh virtualenv created with th

avatar
Leonardo
0 1 0 9
asked 07 September, 2021
60 votes
17 answers
3.1K views
open() in Python does not create a file if it doesn't exist

What is the best way to open a file as read/write if it exists, or if it does not, then create it and open it

avatar
Benton
0 1 0 9
asked 07 September, 2021
55 votes
13 answers
1.8K views
Call Python function from MATLAB

I need to call a Python function from MATLAB. how can I do this?

avatar
Juliane
0 1 0 9
asked 07 September, 2021
50 votes
9 answers
2.6K views
reading v 7.3 mat file in python

I am trying to read a matlab file with the following code import scipy.io mat = scipy.io.loadmat('test.m

avatar
Sena
0 1 0 9
asked 07 September, 2021
66 votes
3 answers
629 views
Why does numpy std() give a different result to matlab std()?

I try to convert matlab code to numpy and figured out that numpy has a different result with the std function.

avatar
Patsy
0 1 0 9
asked 07 September, 2021
54 votes
7 answers
734 views
What is the equivalent of MATLAB's repmat in NumPy

I would like to execute the equivalent of the following MATLAB code using NumPy: repmat([1; 1], [1 1 1]). How

avatar
Hallie
0 1 0 9
asked 07 September, 2021
59 votes
12 answers
2K views
What is the Python equivalent of Matlab's tic and toc functions?

What is the Python equivalent of Matlab's tic and toc functions?

avatar
Dorinda
0 1 0 9
asked 07 September, 2021
55 votes
10 answers
759 views
Representing and solving a maze given an image

What is the best way to represent and solve a maze given an image? Given an JPEG image (as seen above), wha

avatar
Serina
0 1 0 9
asked 07 September, 2021